1 changed files with 39 additions and 0 deletions
@ -0,0 +1,39 @@
|
||||
using Elight.Utility.Extensions; |
||||
using Microsoft.AspNetCore.Mvc.Filters; |
||||
using System.Diagnostics; |
||||
|
||||
namespace _24Hour.Filter |
||||
{ |
||||
public class RequestLoggingFilter : IActionFilter |
||||
{ |
||||
private readonly ILogger<RequestLoggingFilter> logger;//注入serilog |
||||
private Stopwatch _stopwatch;//统计程序耗时 |
||||
public RequestLoggingFilter(ILogger<RequestLoggingFilter> logger) |
||||
{ |
||||
this.logger = logger; |
||||
_stopwatch = Stopwatch.StartNew(); |
||||
} |
||||
public void OnActionExecuted(ActionExecutedContext context) |
||||
{ |
||||
_stopwatch.Stop(); |
||||
var request = context.HttpContext.Request; |
||||
var response = context.HttpContext.Response; |
||||
var info = $"End Required metod:[{request.Method}],Path:[{request.Path}],StatusCode:[{response.StatusCode}],Times[{_stopwatch.Elapsed.TotalMilliseconds}],QueryString[{request.QueryString}],Result[{context.Result.ConvertToJsonStr()}]"; |
||||
if (info.Length>1024) |
||||
{ |
||||
info = info.Substring(0, 1024); |
||||
} |
||||
logger.LogInformation(info); |
||||
} |
||||
public void OnActionExecuting(ActionExecutingContext context) |
||||
{ |
||||
var request = context.HttpContext.Request; |
||||
var info = $"Start request method:[{request?.Method}] path:[{request?.Path}]"; |
||||
if (info.Length > 1024) |
||||
{ |
||||
info = info.Substring(0, 1024); |
||||
} |
||||
logger.LogInformation(info); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue