| | |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using System.Diagnostics; |
| | | using System.Text; |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using Microsoft.AspNetCore.Mvc.Controllers; |
| | | using Microsoft.AspNetCore.Mvc.Filters; |
| | | using Newtonsoft.Json; |
| | | using System.Diagnostics; |
| | | using System.Text; |
| | | |
| | | namespace MESApplication |
| | | namespace MESApplication; |
| | | |
| | | /// <summary> |
| | | /// 方法过滤器 |
| | | /// </summary> |
| | | public class ActionFilter : IActionFilter |
| | | { |
| | | private Stopwatch _stopwatch; |
| | | |
| | | /// <summary> |
| | | /// 方法过滤器 |
| | | /// 监控日志 |
| | | /// </summary> |
| | | public class ActionFilter : IActionFilter |
| | | public static ILogger LoggerMonitor { get; set; } |
| | | |
| | | /// <summary> |
| | | /// 错误日志 |
| | | /// </summary> |
| | | public static ILogger LoggerError { get; set; } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 方法执行前 |
| | | /// </summary> |
| | | /// <param name="context"></param> |
| | | /// <exception cref="NotImplementedException"></exception> |
| | | public async void OnActionExecuting(ActionExecutingContext context) |
| | | { |
| | | /// <summary> |
| | | /// 监控日志 |
| | | /// </summary> |
| | | public static ILogger LoggerMonitor { get; set; } |
| | | |
| | | /// <summary> |
| | | /// 错误日志 |
| | | /// </summary> |
| | | public static ILogger LoggerError { get; set; } |
| | | |
| | | private Stopwatch _stopwatch; |
| | | |
| | | /// <summary> |
| | | /// 创建请求日志文本 |
| | | /// </summary> |
| | | /// <param name="method"></param> |
| | | /// <param name="controllerName"></param> |
| | | /// <param name="actionName"></param> |
| | | /// <param name="actionArgs"></param> |
| | | /// <returns></returns> |
| | | private static string CreateRequestLogText(string method, string controllerName, string actionName, string requestHead, string requestBody) |
| | | // ErrorLog.Write("=================================================================================================================================="); |
| | | _stopwatch = new Stopwatch(); |
| | | _stopwatch.Start(); |
| | | //throw new NotImplementedException(); |
| | | if (LoggerMonitor != null) |
| | | { |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.Append($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 请求{method}/{controllerName}/{actionName}接口,请求Head:{requestHead}\n"); |
| | | sb.Append($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 请求{method}/{controllerName}/{actionName}接口,请求Body:{requestBody}\n"); |
| | | return sb.ToString(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建响应日志文本 |
| | | /// </summary> |
| | | /// <param name="method"></param> |
| | | /// <param name="controllerName"></param> |
| | | /// <param name="actionName"></param> |
| | | /// <param name="result"></param> |
| | | /// <returns></returns> |
| | | private static string CreateResponseLogText(string method, string controllerName, string actionName, object result) |
| | | { |
| | | StringBuilder sb = new StringBuilder(); |
| | | sb.Append($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 完成请求{method}/{controllerName}/{actionName}接口,返回结果:"); |
| | | if (result != null) |
| | | //记录请求参数日志 |
| | | var desc = context.ActionDescriptor as ControllerActionDescriptor; |
| | | if (desc != null) |
| | | { |
| | | sb.Append($"{JsonConvert.SerializeObject(result)}"); |
| | | } |
| | | else |
| | | { |
| | | sb.Append($"无"); |
| | | } |
| | | return sb.ToString(); |
| | | } |
| | | var headers = new Dictionary<string, object>(); |
| | | var requestHeaders = context.HttpContext.Request.Headers; |
| | | |
| | | // 访问请求中的 header 信息 |
| | | foreach (var header in requestHeaders) |
| | | headers.Add(header.Key, header.Value); |
| | | var requestHead = JsonConvert.SerializeObject(headers); |
| | | |
| | | /// <summary> |
| | | /// 方法执行前 |
| | | /// </summary> |
| | | /// <param name="context"></param> |
| | | /// <exception cref="NotImplementedException"></exception> |
| | | public async void OnActionExecuting(ActionExecutingContext context) |
| | | { |
| | | // ErrorLog.Write("=================================================================================================================================="); |
| | | _stopwatch = new Stopwatch(); |
| | | _stopwatch.Start(); |
| | | //throw new NotImplementedException(); |
| | | if (LoggerMonitor != null) |
| | | { |
| | | //记录请求参数日志 |
| | | ControllerActionDescriptor desc = context.ActionDescriptor as ControllerActionDescriptor; |
| | | if (desc != null) |
| | | var bodys = new Dictionary<string, object>(); |
| | | var actionArguments = context.ActionArguments; |
| | | // 访问请求中的参数 |
| | | foreach (var argument in actionArguments) |
| | | { |
| | | Dictionary<string, object> headers = new Dictionary<string, object>(); |
| | | var requestHeaders = context.HttpContext.Request.Headers; |
| | | |
| | | // 访问请求中的 header 信息 |
| | | foreach (var header in requestHeaders) |
| | | { |
| | | headers.Add(header.Key, header.Value); |
| | | } |
| | | var requestHead = JsonConvert.SerializeObject(headers); |
| | | |
| | | Dictionary<string, object> bodys = new Dictionary<string, object>(); |
| | | var actionArguments = context.ActionArguments; |
| | | // 访问请求中的参数 |
| | | foreach (var argument in actionArguments) |
| | | { |
| | | //dic.Add(argument.Key, argument.Value); |
| | | var parameter = JsonConvert.DeserializeObject<Dictionary<string, object>>(argument.Value.ToString()); |
| | | foreach (var item in parameter) |
| | | { |
| | | bodys.Add(item.Key, item.Value); |
| | | } |
| | | } |
| | | var requestBody = JsonConvert.SerializeObject(bodys); |
| | | |
| | | var logText = CreateRequestLogText(context.HttpContext.Request.Method, desc.ControllerName, desc.ActionName, requestHead, requestBody); |
| | | //LoggerMonitor.LogDebug(logText); |
| | | //ErrorLog.Write(logText); |
| | | //dic.Add(argument.Key, argument.Value); |
| | | var parameter = |
| | | JsonConvert |
| | | .DeserializeObject<Dictionary<string, object>>( |
| | | argument.Value.ToString()); |
| | | foreach (var item in parameter) |
| | | bodys.Add(item.Key, item.Value); |
| | | } |
| | | |
| | | var requestBody = JsonConvert.SerializeObject(bodys); |
| | | |
| | | var logText = CreateRequestLogText( |
| | | context.HttpContext.Request.Method, desc.ControllerName, |
| | | desc.ActionName, requestHead, requestBody); |
| | | //LoggerMonitor.LogDebug(logText); |
| | | //ErrorLog.Write(logText); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | public void OnActionExecuted(ActionExecutedContext context) |
| | | { |
| | | //throw new NotImplementedException(); |
| | | _stopwatch.Stop(); |
| | | long elaspsedMillisedconds = _stopwatch.ElapsedMilliseconds; |
| | | string msg = ""; |
| | | //string msg = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 接口执行时间:{elaspsedMillisedconds}毫秒"; |
| | | //ErrorLog.Write(msg); |
| | | |
| | | if (context.Exception != null) |
| | | { |
| | | // 记录异常日志 |
| | | if (LoggerError != null) |
| | | { |
| | | LoggerError.LogError(context.Exception, context.Exception.Message); |
| | | |
| | | ErrorLog.Write($@"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 接口异常:{JsonConvert.SerializeObject(context.Exception)}"); |
| | | ErrorLog.Write($@"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 异常提示信息:{JsonConvert.SerializeObject(context.Exception.Message)}"); |
| | | ErrorLog.Write("=================================================================================================================================="); |
| | | } |
| | | } |
| | | |
| | | if (LoggerMonitor != null) |
| | | { |
| | | // 记录请求结果日志 |
| | | ControllerActionDescriptor desc = context.ActionDescriptor as ControllerActionDescriptor; |
| | | if (desc != null) |
| | | { |
| | | ObjectResult rst = context.Result as ObjectResult; |
| | | object rstValue = rst != null ? rst.Value : null; |
| | | var logText = CreateResponseLogText( |
| | | context.HttpContext.Request.Method, |
| | | desc.ControllerName, |
| | | desc.ActionName, |
| | | rstValue); |
| | | LoggerMonitor.LogDebug(logText); |
| | | ErrorLog.Write(logText); |
| | | } |
| | | } |
| | | // ErrorLog.Write(msg); |
| | | // ErrorLog.Write("=================================================================================================================================="); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public void OnActionExecuted(ActionExecutedContext context) |
| | | { |
| | | //throw new NotImplementedException(); |
| | | _stopwatch.Stop(); |
| | | var elaspsedMillisedconds = _stopwatch.ElapsedMilliseconds; |
| | | var msg = ""; |
| | | //string msg = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 接口执行时间:{elaspsedMillisedconds}毫秒"; |
| | | //ErrorLog.Write(msg); |
| | | |
| | | if (context.Exception != null) |
| | | // 记录异常日志 |
| | | if (LoggerError != null) |
| | | { |
| | | LoggerError.LogError(context.Exception, |
| | | context.Exception.Message); |
| | | |
| | | ErrorLog.Write( |
| | | $@"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 接口异常:{JsonConvert.SerializeObject(context.Exception)}"); |
| | | ErrorLog.Write( |
| | | $@"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 异常提示信息:{JsonConvert.SerializeObject(context.Exception.Message)}"); |
| | | ErrorLog.Write( |
| | | "=================================================================================================================================="); |
| | | } |
| | | |
| | | if (LoggerMonitor != null) |
| | | { |
| | | // 记录请求结果日志 |
| | | var desc = context.ActionDescriptor as ControllerActionDescriptor; |
| | | if (desc != null) |
| | | { |
| | | var rst = context.Result as ObjectResult; |
| | | var rstValue = rst != null ? rst.Value : null; |
| | | var logText = CreateResponseLogText( |
| | | context.HttpContext.Request.Method, |
| | | desc.ControllerName, |
| | | desc.ActionName, |
| | | rstValue); |
| | | LoggerMonitor.LogDebug(logText); |
| | | ErrorLog.Write(logText); |
| | | } |
| | | } |
| | | // ErrorLog.Write(msg); |
| | | // ErrorLog.Write("=================================================================================================================================="); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建请求日志文本 |
| | | /// </summary> |
| | | /// <param name="method"></param> |
| | | /// <param name="controllerName"></param> |
| | | /// <param name="actionName"></param> |
| | | /// <param name="actionArgs"></param> |
| | | /// <returns></returns> |
| | | private static string CreateRequestLogText(string method, |
| | | string controllerName, string actionName, string requestHead, |
| | | string requestBody) |
| | | { |
| | | var sb = new StringBuilder(); |
| | | sb.Append( |
| | | $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 请求{method}/{controllerName}/{actionName}接口,请求Head:{requestHead}\n"); |
| | | sb.Append( |
| | | $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 请求{method}/{controllerName}/{actionName}接口,请求Body:{requestBody}\n"); |
| | | return sb.ToString(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建响应日志文本 |
| | | /// </summary> |
| | | /// <param name="method"></param> |
| | | /// <param name="controllerName"></param> |
| | | /// <param name="actionName"></param> |
| | | /// <param name="result"></param> |
| | | /// <returns></returns> |
| | | private static string CreateResponseLogText(string method, |
| | | string controllerName, string actionName, object result) |
| | | { |
| | | var sb = new StringBuilder(); |
| | | sb.Append( |
| | | $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")} 完成请求{method}/{controllerName}/{actionName}接口,返回结果:"); |
| | | if (result != null) |
| | | sb.Append($"{JsonConvert.SerializeObject(result)}"); |
| | | else |
| | | sb.Append("无"); |
| | | return sb.ToString(); |
| | | } |
| | | } |