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

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

相关推荐
再学一点就睡7 小时前
前端网络实战手册:15个高频工作场景全解析
前端·网络协议
C_心欲无痕8 小时前
有限状态机在前端中的应用
前端·状态模式
C_心欲无痕8 小时前
前端基于 IntersectionObserver 更流畅的懒加载实现
前端
candyTong8 小时前
深入解析:AI 智能体(Agent)是如何解决问题的?
前端·agent·ai编程
柳杉8 小时前
建议收藏 | 2026年AI工具封神榜:从Sora到混元3D,生产力彻底爆发
前端·人工智能·后端
我是唐青枫8 小时前
C#.NET ConcurrentDictionary<TKey, TValue> 深度解析:原理与实践
c#·.net
weixin_462446238 小时前
使用 Puppeteer 设置 Cookies 并实现自动化分页操作:前端实战教程
运维·前端·自动化
CheungChunChiu8 小时前
Linux 内核动态打印机制详解
android·linux·服务器·前端·ubuntu
GIS之路9 小时前
GDAL 创建矢量图层的两种方式
前端
小目标一个亿10 小时前
Windows平台Nginx配置web账号密码验证
linux·前端·nginx