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

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

相关推荐
迅猛龙办公室1 小时前
Python输出当前计算机的系统日期和时间
开发语言·前端·python
小狼154541 小时前
浏览器扩展脚本为什么有时候不生效:注入时机、iframe 和单页路由,多多开票助手
前端·chrome
じòぴé南冸じょうげん2 小时前
油猴脚本突然发现变成灰色了,无法使用?页面不生效?刷新页面没反应?
前端
Csvn3 小时前
组合式 API(Composition API)
前端
火柴就是我4 小时前
Android 打包报错 25.0.3
android·前端
BD_Marathon4 小时前
消息对象中字段的说明
java·前端·python
八荒启·交互动画4 小时前
Web特效025—用 Canvas 2D 做Web特效的定义与边界:这支画笔能做到哪一步
前端·webgl·网页特效·八荒启-交互动画·八荒启
Ai-_Man4 小时前
您您这可以把Dola的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。用AI导出鸭,答案是可以的
开发语言·前端·人工智能·小程序
excel5 小时前
Nuxt 中使用 useHead 优化 SEO 与 GEO
前端
IT_陈寒6 小时前
SpringBoot启动慢得像蜗牛?原来是这个配置在捣鬼
前端·人工智能·后端