Sergey # Blog

Few thoughts about the current.

Diagnostic of ApplicationInsights SDK

This blog post was written by Anastasia Baranchenkova.

Currently, Application Insights SDK is in public beta stage, so you cannot expect it to be absolutely stable. Application Insights uses ETW to log its own verbose traces. If you lost any hope to understand why you do not see anything in the portal you may want to get that traces. Often Application Insights developement team would want to take a look at its internal traces in order to diagnose the specific behavior.

There are several ways how you could collect ETW traces:

  • Use logman to create and capture ETW session to etl file. In this case you could use any tools to visualize captured traces such as Tx (LINQ to Logs and Traces) LINQpad driver, SvcPerf, PerfView, etc.
  • Use PerfView to create and capture ETW session directly.
  • Use logman to create session then use Tx (LINQ to Logs and Traces) LINQpad driver to write standing LINQ query.

But there is also a handy nuget package that can simplify that job. Let’s look into all that in order.

Use PerfView to collect traces

PerfView is a well known tool for CPU and Memory investigations. You can read more on Vance Morrison’s blog. To collect AI traces using perfview you would run this PerfView.exe /onlyProviders=*Microsoft-ApplicationInsights-Extensibility-Web,*Microsoft-ApplicationInsights-Web,*Microsoft-ApplicationInsights-Core,*Microsoft-ApplicationInsights-Extensibility-DependencyCollector,*Microsoft-ApplicationInsights-Extensibility-Rtia-SharedCore,*Microsoft-ApplicationInsights-Extensibility-WindowsServer,*Microsoft-ApplicationInsights-WindowsServer-TelemetryChannel collect

You will see something like that:

Events are split into multiple tables and it is hard to filter that or get a general picture what and when was happening if you’re using it for the first time.

Use ApplicationInsights.Diagnostics to collect traces

This is why we built dedicated easy-to-use ApplicationInsights.Diagnostics package (which is built on top of the same library that enpowers PerfView itself).

To get the list of all the verbose traces in LINQPad you would do the following:

  • Run LINQpad as administartor
  • Create new query in LINQpad
  • Add ApplicationInsights.Diagnostics nuget package (or just open this linq file if you do not have premium features enabled)
  • Add the following namespaces: System.Reactive.Linq, ApplicationInsights.Diagnostics.
  • Write the following query: DiagnosticObservable.Create().Select(i => i.FormattedMessage)

You will see something like that:

If you want to filter the results you can use regular LINQ like this:

1
2
3
4
5
6
7
8
9
10
11
DiagnosticObservable
  .Create()
  .Where(i => i.FormattedMessage.Contains("localhost"))
  .Select(i => new 
    {
      i.TimeStamp, 
        i.FormattedMessage, 
        i.EventName, 
        i.ProviderName, 
        i.Level})
  .Dump();

The same in Console application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using(DiagnosticObservable
  .Create()
  .Where(i => i.FormattedMessage.Contains("localhost"))
  .Select(i => new 
    {
      i.TimeStamp, 
        i.FormattedMessage, 
        i.EventName, 
        i.ProviderName, 
        i.Level})
  .Subscribe(i => Console.WriteLine("{0} {1} {2} {3} {4}",
      i.TimeStamp, 
        i.FormattedMessage, 
        i.EventName, 
        i.ProviderName, 
        i.Level))
 {
  Console.ReadKey();
 }

Summary

You could find more samples here.

Comments