Skip to content

2. How to enable NanoProfiler in Web application?

Teddy edited this page Nov 17, 2015 · 3 revisions

For profiling a web application:

First, you need to reference NanoProfiler.Web package. You could add custom tags or data fields to your profisling session or steps wherever ProfilingSession.Current is accessible.

Second, in your application code, you could add profiling steps by calling the Step() method:

public override async Task ProcessRequestAsync(HttpContext context)
{
    using (ProfilingSession.Current.Step("ProcessRequestAsync"))
    {
        await ExecuteTask(context);

        // it supports to use a delegate for getting the step name
        // so that, in case profiling is disabled, there is no string concat cost
        var data = GetDataForProcessing();
        using (ProfilingSession.Current.Step(() =>BuildStepNameFromData(data)))
        {
            // do something with data
            //...
        }
    }
}

To understand more configuration options, please check the Global.asax.cs file of the simple demo project in source code.

For profiling a console or windows service application:

You only need to reference to the NanoProfiler package. And the main difference is you need to call ProfilingSession.Start() and ProfilingSession.Stop() explicitly before and after your execution code for profiling. (What happens for web application when they are not necessary is that they are actually called internally in the NanoProfiler http module.) For other options, please check the console demo application in source code.

Clone this wiki locally