How to track every ASP.NET Web API request using NLog

I created a simple REST API with ASP.NET API. For testing purposes, I would like to add some traces. So I added NLog to my project. At the moment, my journal is as follows:

// POST api/values
public void Post([FromBody]string value)
{
    logger.Trace("Request: {0} api/values", Request.Method); 
    _repository.insert(value);
    logger.Trace("Response: {0} api/values", Request.Method); 
}

In each method, I added logger.Trace at the top and then at the bottom of my methods. I have 2 questions with this method:

  • I should not add this line to each of my methods.
  • I do not know how to add a JSON body to my trace

Point 1 is not a real problem at the moment (see below), but I need to quickly check something so that every JSON authority receives my API.

I already tried this

// POST api/values
public void Post([FromBody]string value)
{
    logger.Trace("Request: {0} api/values {1}", Request.Method, Request.Body); 
    _repository.insert(value);
    logger.Trace("Response: {0} api/values", Request.Method); 
}

But the request does not have a Body property.

1: http://weblogs.asp.net/fredriknormen/log-message-request-and-response-in-asp-net-webapi

+4
2

Leo MVC, API Http REST http://www.c-sharpcorner.com/UploadFile/1492b1/restful-day-sharp6-request-logging-and-exception-handingloggin/

public class HttpLoggingFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext)
    {
        //Do something here
    }

    public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
    {
        //Do something here
    }
}

, Leo , REST.

+1

... - / - /

public class MyCustomFilter : System.Web.Http.Filters.ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        //Do something here before an action method starts executing
    }

    public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext context)
    {
        //Do something here after an action method finished executing
    }
}

asp.net... , owin/katana global.asax, ...

GlobalConfiguration.Configuration.Filters.Add(new MyCustomFilter());

. , / , ...

public class MyCustomFilter : System.Web.Http.Filters.ActionFilterAttribute
{
    public bool DisableTracing{get;set;}

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if(!DisableTracing){
               //Do something here before an action method starts executing
        }
    }

    public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext context)
    {
        if(!DisableTracing){
               //Do something here before an action method starts executing
        }
    }
}

...

[MyCustomFilter(DisableTracing = true)]
public IHttpActionResult MyAction(int id){}

Update

JSON , , ...

 request.Content.ReadAsStringAsync().Result;
+7

Source: https://habr.com/ru/post/1651747/


All Articles