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