12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using Microsoft.AspNetCore.Mvc.Filters;
- using ServiceCenter.Logs;
- using System.Diagnostics;
- namespace ServiceCenter.Attributes
- {
- public class LogAttribute : ActionFilterAttribute
- {
- private string LogFlag { get; set; }
- private string ActionArguments { get; set; }
- /// <summary>
- /// 请求体中的所有值
- /// </summary>
- private string RequestBody { get; set; }
- private Stopwatch Stopwatch { get; set; }
- public LogAttribute(string logFlag)
- {
- LogFlag = logFlag;
- }
- public override void OnActionExecuting(ActionExecutingContext context)
- {
- //ActionArguments = Newtonsoft.Json.JsonConvert.SerializeObject(context.ActionArguments);
- //Stopwatch = new Stopwatch();
- //Stopwatch.Start();
- }
- public override void OnActionExecuted(ActionExecutedContext context)
- {
- //string msg = "";
- //try
- //{
- // base.OnActionExecuted(context);
- // Stopwatch.Stop();
- // string url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString;
- // string method = context.HttpContext.Request.Method;
- // string qs = ActionArguments;
- // string res = "在返回结果前发生了异常";
- // if (context.Result == null)
- // {
- // res = "无返回结果";
- // }
- // else
- // {
- // dynamic result = context.Result.GetType().Name == "EmptyResult" ? new { Value = "无返回结果" } : context.Result as dynamic;
- // try
- // {
- // if (result != null)
- // {
- // res = Newtonsoft.Json.JsonConvert.SerializeObject(result.Value);
- // }
- // }
- // catch (Exception)
- // {
- // res = "日志未获取到结果,返回的数据无法序列化";
- // }
- // }
- // msg = $"方法:{LogFlag}-地址:{url}-方式:{method}-耗时:{Stopwatch.Elapsed.TotalMilliseconds}毫秒(指控制器内对应方法执行完毕的时间)\n" +
- // $"参数:{qs}\n " +
- // $"结果:{res}";
- //}
- //catch (Exception ex)
- //{
- // msg = ex.Message + ex.StackTrace;
- //}
- //LogHub.InterfacePublish(LogFlag, msg);
- }
- }
- }
|