C# 用ActionFilter给WebAPI增加请求日志

 编写 ActionFilterAttribute

using System;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace Provider.WebFilters
{
    /// <summary>
    /// WebAPI Action监控
    /// </summary>
    public class ActionMonitorAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 全局请求ID号
        /// </summary>
        private static Int64 ActionId { get; set; }
        /// <summary>
        /// 客户端IP地址
        /// </summary>
        private String ClientIp { get; set; }
        /// <summary>
        /// 请求ID号
        /// </summary>
        private Int64 RequestId { get; set; }

        /// <summary>
        /// 请求的接口
        /// </summary>
        private String controllername { get; set; }


        /// <summary>
        /// 请求处理前
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            RequestId = ActionId++;
            var strary = actionContext.Request.RequestUri.AbsolutePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            controllername = strary[1];
            dynamic RemoteEndpoint = null;
            if (actionContext.Request.Properties.TryGetValue("System.ServiceModel.Channels.RemoteEndpointMessageProperty", out RemoteEndpoint))
            {
                ClientIp = RemoteEndpoint.Address;
                if (ClientIp == "::1")
                {
                    ClientIp = "localhost";
                }
            }
            String Message = String.Format("[Requestd].[{1}].[{4}].[{0}].[{2}]=>{3}", RequestId, ClientIp, actionContext.Request.Method, actionContext.Request.RequestUri, controllername);
            LogHelper.WebRequest.Info(Message);
            base.OnActionExecuting(actionContext);
        }

        /// <summary>
        /// 请求返回前
        /// </summary>
        /// <param name="actionExecutedContext"></param>
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Exception == null)
            {
                LogHelper.WebRequest.Info(String.Format("[Response].[{0}].[{3}].[{1}].[{2}]", ClientIp, RequestId, actionExecutedContext.Response.ReasonPhrase, controllername));
                if (LogHelper.WebRequest.IsDebugEnabled)
                {
                    LogHelper.WebRequest.Debug(String.Format("    [Data]\t{0}\t{1}", RequestId, actionExecutedContext.Response.Content.ReadAsStringAsync().Result));
                }
            }
            else
            {
                LogHelper.WebRequest.Info(String.Format("[Response].[{0}].[{3}].[{1}].[{2}]", ClientIp, RequestId, "Exception", controllername));
                LogHelper.Default.Info(String.Format("[WebApiRequest].[Exception].[{0}].[{1}]\r\n{2}", RequestId, actionExecutedContext.Request.RequestUri, actionExecutedContext.Exception.StackTrace));
            }
            base.OnActionExecuted(actionExecutedContext);
        }
    }
}

 

 

实际使用 

在 WEBAPI的 Controller类加上  [ActionMonitor] 属性即可

using System.Net;
using System.Net.Http;
using System.Web.Http;
using Provider.WebFilters;

namespace Provider.WebController
{
    [ActionMonitor]
    public class TestController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage Get()
        {
            return this.Request.CreateResponse(HttpStatusCode.OK, "OK");
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值