You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.5 KiB
41 lines
1.5 KiB
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 async 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); |
|
} |
|
} |
|
}
|
|
|