Max_dev
Max_dev

Reputation: 508

How to log each method Execution time and parameters on .Net Web Api

We are facing performance issues sometime on the web application(.NET Web Api2) and every time user reports performance issue then we need to debug the application to know what happen and which method took more time.

Is there a best approach to handle this by logging all the method calls including time and it's parameter on demand, so that when it's slow we can enable it and get logs to see instead of debugging the application.

It's not only controller method, all the methods in the call which i like to log.

Thanks for any comments or good example related to this.

Upvotes: 4

Views: 5439

Answers (2)

Kishan Vaishnav
Kishan Vaishnav

Reputation: 2631

NLog can provide this feature.

Refer NLog Tutorial for installation and setup.

Is there the best approach to handle this by logging all the method calls including time and its parameter on demand

You need to create an action filter, it will provide time taken by the request.

The ActionFilter: (You may have to change code to your requirement)

   public class LogHttpRequestAttribute : ActionFilterAttribute
   {
    private static Logger _logger = LogManager.GetCurrentClassLogger();

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
       _logger.Info("action execution started");
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        _logger.Info("action execution finished");
   }

The Global.asax:

protected void Application_Start()
{
    //Some code.

    // Register global filter
    GlobalFilters.Filters.Add(new LogHttpRequestAttribute());
}

You have to add a target for this filter in NLog.config file.

<targets>
    <target name="logfile" xsi:type="File" fileName="file.txt" />
</targets>
<rules>
    <logger name="*.LogHttpRequestAttribute" writeTo="logfile" />
</rules>

The example given above is far from complete but I hope it will direct you in the right direction.

There are many useful Layout renderers given here look for Web, ASP.NET and ASP.NET Core section. Few of the useful are

  • ${aspnet-mvc-action}
  • ${aspnet-mvc-controller}
  • ${aspnet-request-form}
  • ${aspnet-request-querystring}
  • ${aspnet-user-identity}

Enable logging and get logs to see instead of debugging the application.

To disable logging set enabled attribute of the rule to false or disable in code.

It's not only the controller method, all the methods in the call which I like to log.

You have to create a static variable for this in every class you want to log.

private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

Then use this member to log the data.

_logger.Info("data");

Edit: To log from any method.

using NLog;
public class DemoClass
{
    private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

    public void DemoMethod()
    {
        try
        {
            //some code
            _logger.Info("Some information");
        }
        catch (Exception ex)
        {
            _logger.Error(ex, $"Something went wrong");
            throw;
        }
    }
}

Upvotes: 4

kkafkas
kkafkas

Reputation: 342

You can monitor the performance of your application by exposing a metric endpoint and use a dashboard to visualize the metrics. A very popular approach is to use the App-Metrics library for exposing the metrics along with Prometheus and Grafana

https://www.app-metrics.io/web-monitoring/aspnet-core/

Upvotes: 2

Related Questions