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

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

相关推荐
维克兜率天2 小时前
【维克】RSI相对强弱指数:判断多空力量的温度计
前端
FRing039 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
YIAN9 小时前
我踩完 DeepAgents 记忆模块的坑,才搞懂长期记忆和摘要不是一回事
前端·javascript·agent
23957304910 小时前
从 GUI 彻底转向 CLI:我用 .NET + Avalonia + NativeAOT 打造了 Agent 终端托管工具 Octopus
开源·.net·agent·avalonia·ai coding
map_3d_vis11 小时前
百度地图 JSAPI 4.0 是什么:版本说明与能力概览
前端·javascript·webgl·地图·百度地图
码云之上12 小时前
微信小程序设备指纹实践,删了重装还能认出这台手机吗?
前端·安全·微信小程序
JavaGuide12 小时前
刚刚,GLM-5.4 正式发布,实测来了:看图用 Blender 建凉亭、做技术视频,再画一辆真能传动的自行车
前端·后端
乌鲁木齐德润恒圣信息科技有限公司14 小时前
GEO生成式引擎优化:技术原理与43引擎自动测试系统
大数据·前端·人工智能
泯泷14 小时前
第一章:Lit简介 - 现代Web组件标准
前端·javascript·web components
Moment15 小时前
AI 写代码越来越快,程序员为什么反而更累了
前端·后端·面试