Skip to content

Using Application Insights Data Output

Saar Shen edited this page Apr 7, 2023 · 2 revisions

If you want to output data to application insights, you can use NuGet Package OpenDotNetDiagnostics.Counters.Sinks.ApplicationInsights to do so.

For example, this is how it looks like of the memory usage (working set) at the beginning of an application

image

And this is another instance of querying the last 24 hours for the working set, get the max value every 30 minutes:

image

Here's how to use it.

  1. Enable application insights for your application

  2. Add NuGet package for the sink

  3. Add the sink in Program.cs:

    // Application insights and its sink
    builder.Services.AddApplicationInsightsTelemetry(); // Use `AddApplicationInsightsTelemetryWorkerService();` instead in worker
    builder.Services.AddDotNetCounterApplicationInsightsSink();
  4. Run the application

    It usually takes a couple of minutes for the data to show up.

  5. Query the data

    For example, run the following query for working-set:

    customEvents
    | where name == "OpenDotnetDiagnosticsCounter"  // All dotnet-counter events are named `OpenDotnetDiagnosticsCounter`
    | extend Value_ = todouble(customMeasurements.Value)
    | extend DisplayName_ = tostring(customDimensions.DisplayName)
    | where DisplayName_ == "Working Set"   // Focus on 1 counter at a time
    | order by timestamp desc
    | project timestamp, DisplayName_, Value_   // Project fields to prepare for rendering the chart
    | render timechart                          // Render a timechart