NanoProfiler 有許多的套件。

如果是 Web 專案,安裝 NanoProfiler.Web 即可 (會連帶安裝 NanoProfiler)。

套件安裝完後要設定 CircularBuffer,可透過程式設定…

protected void Application_Start(object sender, EventArgs e)
{
    ...
    ProfilingSession.CircularBuffer = new CircularBuffer(200, session => false);
    ...
}

也可以透過設定檔設定…


  ...

CircularBuffer 設定完後,就可以設定要 Profile 的部分,像是每個 Request 的進出。

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            ProfilingSession.Start("root");
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            ProfilingSession.Stop();
        }

以及 Request 中想要監測的部分。

using (var step = ProfilingSession.Current.Step("[StepName]"))
{
    ...
}

將程式運行起來,訪問 http://[Domain]/nanoprofiler/view 即可看到 profile 的結果。
1.png

這邊如果要將資料保存下來,可以加裝 NanoProfiler.Storages.Json,並修改設定去指定使用 Storage。


    ...
  
  ...

如果 Profile 要過濾掉一些位置,可以透過程式設定 filter。

        protected void Application_Start()
        {
            ...
            // register profiling filters to exclude some URLs from profiling
            ProfilingSession.ProfilingFilters.Add(new NameContainsProfilingFilter("_tools/"));
            ProfilingSession.ProfilingFilters.Add(new FileExtensionProfilingFilter("jpg", "js", "css"));
            ...
        }

或是透過設定檔設定也可以。


    ...
  
  ...