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

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

相关推荐
Hilaku18 小时前
做中后台业务,为什么我不建议你用 Tailwind CSS?
前端·css·代码规范
有意义18 小时前
【面试复盘】前端底层原理与 React 核心机制深度梳理
前端·react.js·面试
二十一_18 小时前
LangChain 教程 05|模型配置:AI 的大脑与推理引擎
前端·langchain
kerli19 小时前
Compose 组件:BoxWithConstraints作用及其原理
android·前端
LovroMance19 小时前
消息总线 + 可插拔的消息插件管理系统
前端
Lee川19 小时前
React Router 实战指南:构建现代化前端路由系统
前端·react.js·架构
薛纪克19 小时前
前端自动化测试的 Spec 模式:用 Kiro Power 实现从需求到脚本的全链路自动化
前端·自动化测试·ai·kiro
YAwu1119 小时前
HTML语义化渲染与CSS优先级机制的技术解析
前端
MgArcher19 小时前
Python高级特性:filter() 函数完全指南
前端·后端
~plus~19 小时前
C# 内存管理深度剖析:从 Span<T> 到 Memory<T> 再到 ArrayPool
开发语言·c#