csharp 使用Log4net日志记录的自定义Web API过滤器。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 使用Log4net日志记录的自定义Web API过滤器。相关的知识,希望对你有一定的参考价值。

public class LogActionWebApiFilter : ActionFilterAttribute
{
    /// <summary>
    /// Instance of the Log4Net log.
    /// </summary>
    [Dependency]  //Part 1
     public ILog Log { get; set; }
        
    //This function will execute before the web api controller
    //Part 2
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
            //This is where you will add any custom logging code
            //that will execute before your method runs.
      		Log.DebugFormat(string.Format("Request {0} {1}"
           	, actionContext.Request.Method.ToString()
                  , actionContext.Request.RequestUri.ToString());
    }

    //This function will execute after the web api controller
    //Part 3
     public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
     {
        //This is where you will add any custom logging code that will
        //execute after your method runs.
        Log.DebugFormat(string.Format("{0} Response Code: {1}"
               	, actionExecutedContext.Request.RequestUri.ToString()
                      , actionExecutedContext.Response.StatusCode.ToString());
     }
}

以上是关于csharp 使用Log4net日志记录的自定义Web API过滤器。的主要内容,如果未能解决你的问题,请参考以下文章

log4net日志系统使用详解

csharp log4net记录器扩展

C# 使用Log4Net记录程序日志

(转)Log4Net 详解

非常完善的Log4net详细说明

如何使用 Log4net 进行日志记录?