(C #) Log Interface Request Response time

Source: Internet
Author: User

Log interface response time, logging interface request information, response results, and response time. Can clearly analyze and understand the interface now.

If we do a log under the interface one by one, that's not the result we want. Therefore, we choose to do a feature to control the interface to record the request response log.

The key code is as follows:

public class Logrequestandresponseattribute:actionfilterattribute
{
private static string Requestlogswitch = String. IsNullOrEmpty (configurationsettings.appsettings["Requestlogswitch"])? " 0 ": configurationsettings.appsettings[" requestlogswitch "];//request log switch
<summary>
Rewrite request start
</summary>
<param name= "Actioncontext" ></param>
public override void OnActionExecuting (Httpactioncontext actioncontext)
{
if (Requestlogswitch.trim ()! = "0")
{
Try
{
Requestrecord item = new Requestrecord ();
String reqarguments = String. empty;//Request Parameters
if (ActionContext.Request.Method.Method = = "GET")
{
Reqarguments = ActionContext.Request.RequestUri.Query;
}
else if (ActionContext.Request.Method.Method = = "POST")
{
Reqarguments = (actioncontext.actionarguments!=null&& actionContext.ActionArguments.Count > 0)? ActionContext.ActionArguments.JSONSerializeV3 (): String. Empty;
}
Item. RequestID = ActionContext.Request.GetCorrelationId ();//Request Unique identity
Item. Type = 0;
Item. BASEURL = actioncontext.request.requesturi.absolutepath;//Request Address
Item. Requestmethod = actioncontext.request.method.method;//Request mode
Item. Reqarguments = reqarguments;
Item. Beginrequesttime = DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss.fff");
String jsonstr = Item. JSONSerializeV3 ();
var path = @ "{0}log\logrequest\{1}\log.txt". Formatstr (AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString ("Yyyy-mm-dd"));
Staticfunctions.writestringtofile (Path, Jsonstr, True, Encoding.UTF8);
}
catch (Exception ex) {
var path = @ "{0}log\logrequesterr\{1}\log.txt". Formatstr (AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString ("Yyyy-mm-dd"));
Staticfunctions.writestringtofile (Path, "{0}==> has an exception, exception information is: {1}\r\n, exception description: {2}\r\n, exception location: {3}\r\n". Formatstr (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss"), ex. Message, ex. Innerexception.message, ex. StackTrace), True, Encoding.UTF8);
}

}

Base. OnActionExecuting (Actioncontext);
}

<summary>
Rewrite request end
</summary>
<param name= "ActionExecutedContext" ></param>
public override void OnActionExecuted (Httpactionexecutedcontext actionexecutedcontext)
{
if (Requestlogswitch.trim ()! = "0")
{
String jsonstr = String. Empty;
Try
{
Requestrecord item = new Requestrecord ();
Item. Type = 1;
Item. RequestID = ActionExecutedContext.Request.GetCorrelationId ();
Item. Endrequesttime = DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss.fff");
Item. Responsecode = ActionExecutedContext.Response.StatusCode.GetInt ();
JSONSTR = Item. JSONSerializeV3 ();
var path = @ "{0}log\logrequest\{1}\log.txt". Formatstr (AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString ("Yyyy-mm-dd"));
Staticfunctions.writestringtofile (Path, Jsonstr, True, Encoding.UTF8);
}
catch (Exception ex)
{
var path = @ "{0}log\logrequesterr\{1}\log.txt". Formatstr (AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString ("Yyyy-mm-dd"));
Staticfunctions.writestringtofile (Path, "{0}==> has an exception, exception information is: {1}\r\n, exception description: {2}\r\n, exception location: {3}\r\n". Formatstr (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss"), ex. Message,ex. Innerexception.message,ex. StackTrace), True, Encoding.UTF8);
}

}

Base. OnActionExecuted (ActionExecutedContext);
}

}

(C #) Log Interface Request Response time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.