C# .Net Framework webapi 全局日志

1.创建一个类名字叫做CustomActionFilter.cs

cs 复制代码
/// <summary>
    /// 
    /// </summary>
    public class CustomActionFilter : System.Web.Http.Filters.ActionFilterAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="actionExecutedContext"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            
                HttpRequest request = HttpContext.Current.Request;
                string token = request.Headers["token"];
            string strUserName = null;
            try
            {
                 strUserName = AesUtil.Decrypt(token, UserInformation.strGuid);
            }
            catch (Exception ex)
            {

                throw new Exception($"token:【{token}】无效");
            }
            if (string.IsNullOrWhiteSpace(strUserName))
                throw new Exception($"token:【{token}】无效");
            var obj=  DependencyInjection.Container.ICustomTableOperations.userInformation(strUserName);
            if (obj.IntCode == ReturnState.失败)
                throw new Exception(obj.strErr);
                
                // 获取request提交的参数
                var Paramaters = GetRequestValues(actionExecutedContext);
                // 获取response响应的结果
                var ExecuteResult = GetResponseValues(actionExecutedContext);
                dynamic data = JsonConvert.DeserializeObject(ExecuteResult);
                // 获取访问的ip
                var UserHostAddress = request.UserHostAddress;
                //请求地址
                var path = request.AppRelativeCurrentExecutionFilePath;
               
                
           
            return base.OnActionExecutedAsync(actionExecutedContext, cancellationToken);
        }

        public string GetRequestValues(HttpActionExecutedContext actionExecutedContext)
        {
            Stream stream = actionExecutedContext.Request.Content.ReadAsStreamAsync().Result;
            stream.Position = 0;
            Encoding encoding = Encoding.UTF8;
            /*
                这个StreamReader不能关闭,也不能dispose
                因为你关掉后,后面的管道  或拦截器就没办法读取了
            */
            var reader = new StreamReader(stream, encoding);

            string result = reader.ReadToEnd();
            /*
            这里也要注意:   stream.Position = 0;
            当你读取完之后必须把stream的位置设为开始
            因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。
            */
            stream.Position = 0;
            return result;
        }

        public string GetResponseValues(HttpActionExecutedContext actionExecutedContext)
        {
            Stream stream = actionExecutedContext.Response.Content.ReadAsStreamAsync().Result;
            stream.Position = 0;
            Encoding encoding = Encoding.UTF8;
            /*
            这个StreamReader不能关闭,也不能dispose,
            因为你关掉后,后面的管道  或拦截器就没办法读取了
            */
            var reader = new StreamReader(stream, encoding);
            string result = reader.ReadToEnd();
            /*
            这里也要注意:   stream.Position = 0; 
            当你读取完之后必须把stream的位置设为开始
            因为request和response读取完以后Position到最后一个位置,交给下一个方法处理的时候就会读不到内容了。
            */
            stream.Position = 0;
            return result;
        }
    }

2.在控制器上方加上

CustomActionFilter

这样就可以方便打印日志啦

相关推荐
咸鱼加辣3 分钟前
【前端的crud】DOM 就是前端里的“数据库”
前端·数据库
kong79069287 分钟前
环境搭建-运行前端工程(Nginx)
前端·nginx·前端工程
成都证图科技有限公司15 分钟前
Bus Hound概述
前端
PythonFun29 分钟前
WPS中表格行高无法手动调整怎么办?
前端·html·wps
云中飞鸿32 分钟前
C# params 关键字详解
开发语言·c#
IT_陈寒35 分钟前
JavaScript性能优化:7个V8引擎内部原理帮你减少90%内存泄漏的实战技巧
前端·人工智能·后端
Poetinthedusk39 分钟前
设计模式-模板方法模式
windows·设计模式·c#·wpf·模板方法模式
narukeu1 小时前
聊下 rewriteRelativeImportExtensions 这个 TypeScript 配置项
前端·javascript·typescript
开压路机1 小时前
模拟实现反向迭代器
前端·c++