Search code examples
c#asp.net-web-apiasp.net-core.net-corelog4net

How to auto log every request in .NET Core WebAPI?


I'd like to have every request logged automatically. In previous .Net Framwork WebAPI project, I used to register a delegateHandler to do so.

WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    config.MessageHandlers.Add(new AutoLogDelegateHandler());
}

AutoLogDelegateHandler.cs

public class AutoLogDelegateHandler : DelegatingHandler
{

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var requestBody = request.Content.ReadAsStringAsync().Result;

        return await base.SendAsync(request, cancellationToken)
            .ContinueWith(task =>
            {
                HttpResponseMessage response = task.Result;

                //Log use log4net
                _LogHandle(request, requestBody, response);

                return response;
            });
    }
}

an example of the log content:

------------------------------------------------------
2017-08-02 19:34:58,840
uri: /emp/register
body: {
    "timeStamp": 1481013427,
    "id": "0322654451",
    "type": "t3",
    "remark": "system auto reg"
}
response: {"msg":"c556f652fc52f94af081a130dc627433","success":"true"}
------------------------------------------------------

But in .NET Core WebAPI project, there is no WebApiConfig , or the register function at Global.asax GlobalConfiguration.Configure(WebApiConfig.Register);

So is there any way to achieve that in .NET Core WebAPI?


Solution

  • You can create your own filter attribute...

    public class InterceptionAttribute : ActionFilterAttribute
    {
      public override void OnActionExecuting(HttpActionContext actionContext)
      {
        var x = "This is my custom line of code I need executed before any of the controller actions, for example log stuff";
        base.OnActionExecuting(actionContext);
      }
    }
    

    ... and you would register it with GlobalFilters, but since you said you're using .NET Core, this is how you can try proceeding...

    From learn.microsoft.com:

    You can register a filter globally (for all controllers and actions) by adding it to the MvcOptions.Filters collection in the ConfigureServices method in the Startup class:

    Let us know if it worked.

    P.S. Here's a whole tutorial on intercepting requests with WebAPI, in case someone needs more details.