Explore .NET/.NET Core trace logs in Application Insights

Send diagnostic tracing logs for your ASP.NET/ASP.NET Core application from ILogger, NLog, log4Net, or System.Diagnostics.Trace to Azure Application Insights. You can then explore and search them. Those logs are merged with the other log files from your application, so you can identify traces that are associated with each user request and correlate them with other events and exception reports.

Note

Do you need the log-capture module? It's a useful adapter for third-party loggers. But if you aren't already using NLog, log4Net, or System.Diagnostics.Trace, consider just calling Application Insights TrackTrace() directly.

Install logging on your app

Install your chosen logging framework in your project, which should result in an entry in app.config or web.config.

    <configuration>
      <system.diagnostics>
    <trace autoflush="true" indentsize="0">
      <listeners>
        <add name="myAppInsightsListener" type="Microsoft.ApplicationInsights.TraceListener.ApplicationInsightsTraceListener, Microsoft.ApplicationInsights.TraceListener" />
      </listeners>
    </trace>
  </system.diagnostics>
   </configuration>

Configure Application Insights to collect logs

Add Application Insights to your project if you haven't done that yet. You'll see an option to include the log collector.

Or right-click your project in Solution Explorer to Configure Application Insights. Select the Configure trace collection option.

Note

No Application Insights menu or log collector option? Try Troubleshooting.

Manual installation

Use this method if your project type isn't supported by the Application Insights installer (for example a Windows desktop project).

  1. If you plan to use log4Net or NLog, install it in your project.

  2. In Solution Explorer, right-click your project, and select Manage NuGet Packages.

  3. Search for "Application Insights."

  4. Select one of the following packages:

The NuGet package installs the necessary assemblies and modifies web.config or app.config if that's applicable.

ILogger

For examples of using the Application Insights ILogger implementation with console applications and ASP.NET Core, see ApplicationInsightsLoggerProvider for .NET Core ILogger logs.

Insert diagnostic log calls

If you use System.Diagnostics.Trace, a typical call would be:

System.Diagnostics.Trace.TraceWarning("Slow response - database01");

If you prefer log4net or NLog, use:

logger.Warn("Slow response - database01");

Use EventSource events

You can configure System.Diagnostics.Tracing.EventSource events to be sent to Application Insights as traces. First, install the Microsoft.ApplicationInsights.EventSourceListener NuGet package. Then edit the TelemetryModules section of the ApplicationInsights.config file.

    <Add Type="Microsoft.ApplicationInsights.EventSourceListener.EventSourceTelemetryModule, Microsoft.ApplicationInsights.EventSourceListener">
      <Sources>
        <Add Name="MyCompany" Level="Verbose" />
      </Sources>
    </Add>

For each source, you can set the following parameters:

  • Name specifies the name of the EventSource to collect.
  • Level specifies the logging level to collect: Critical, Error, Informational, LogAlways, Verbose, or Warning.
  • Keywords (optional) specify the integer value of keyword combinations to use.

Use DiagnosticSource events

You can configure System.Diagnostics.DiagnosticSource events to be sent to Application Insights as traces. First, install the Microsoft.ApplicationInsights.DiagnosticSourceListener NuGet package. Then edit the "TelemetryModules" section of the ApplicationInsights.config file.

    <Add Type="Microsoft.ApplicationInsights.DiagnosticSourceListener.DiagnosticSourceTelemetryModule, Microsoft.ApplicationInsights.DiagnosticSourceListener">
      <Sources>
        <Add Name="MyDiagnosticSourceName" />
      </Sources>
    </Add>

For each DiagnosticSource you want to trace, add an entry with the Name attribute set to the name of your DiagnosticSource.

Use ETW events

You can configure Event Tracing for Windows (ETW) events to be sent to Application Insights as traces. First, install the Microsoft.ApplicationInsights.EtwCollector NuGet package. Then edit the "TelemetryModules" section of the ApplicationInsights.config file.

Note

ETW events can only be collected if the process that hosts the SDK runs under an identity that's a member of Performance Log Users or Administrators.

    <Add Type="Microsoft.ApplicationInsights.EtwCollector.EtwCollectorTelemetryModule, Microsoft.ApplicationInsights.EtwCollector">
      <Sources>
        <Add ProviderName="MyCompanyEventSourceName" Level="Verbose" />
      </Sources>
    </Add>

For each source, you can set the following parameters:

  • ProviderName is the name of the ETW provider to collect.
  • ProviderGuid specifies the GUID of the ETW provider to collect. It can be used instead of ProviderName.
  • Level sets the logging level to collect. It can be Critical, Error, Informational, LogAlways, Verbose, or Warning.
  • Keywords (optional) set the integer value of keyword combinations to use.

Use the Trace API directly

You can call the Application Insights trace API directly. The logging adapters use this API.

For example:

var telemetry = new Microsoft.ApplicationInsights.TelemetryClient();
telemetry.TrackTrace("Slow response - database01");

An advantage of TrackTrace is that you can put relatively long data in the message. For example, you can encode POST data there.

You can also add a severity level to your message. And, like other telemetry, you can add property values to help filter or search for different sets of traces. For example:

var telemetry = new Microsoft.ApplicationInsights.TelemetryClient();
telemetry.TrackTrace("Slow database response",
               SeverityLevel.Warning,
               new Dictionary<string,string> { {"database", db.ID} });

This would enable you to easily filter out in Search all the messages of a particular severity level that relate to a particular database.

Explore your logs

Run your app in debug mode or deploy it live.

In your app's overview pane in the Application Insights portal, select Search.

You can, for example:

  • Filter on log traces or on items with specific properties.
  • Inspect a specific item in detail.
  • Find other system log data that relates to the same user request (has the same OperationId).
  • Save the configuration of a page as a favorite.

Note

If your application sends a lot of data and you're using the Application Insights SDK for ASP.NET version 2.0.0-beta3 or later, the adaptive sampling feature may operate and send only a portion of your telemetry. Learn more about sampling.

Troubleshooting

How do I do this for Java?

Use the Java log adapters.

There's no Application Insights option on the project context menu

  • Make sure that Developer Analytics Tools is installed on the development machine. At Visual Studio Tools > Extensions and Updates, look for Developer Analytics Tools. If it isn't on the Installed tab, open the Online tab and install it.
  • This might be a project type that Devloper Analytics Tools doesn't support. Use manual installation.

There's no log adapter option in the configuration tool

  • Install the logging framework first.
  • If you're using System.Diagnostics.Trace, make sure that you have it configured in web.config.
  • Make sure that you have the latest version of Application Insights. In Visual Studio, go to Tools > Extensions and Updates, and open the Updates tab. If Developer Analytics Tools is there, select it to update it.

I get the "Instrumentation key cannot be empty" error message

You probably installed the logging adapter Nuget package without installing Application Insights. In Solution Explorer, right-click ApplicationInsights.config, and select Update Application Insights. You'll be prompted to sign in to Azure and create an Application Insights resource or reuse an existing one. That should fix the problem.

It can take a while for all the events and requests to get through the pipeline.

How much data is retained?

Several factors affect the amount of data that's retained. For more information, see the limits section of the customer event metrics page.

I don't see some log entries that I expected

If your application sends voluminous amounts of data and you're using the Application Insights SDK for ASP.NET version 2.0.0-beta3 or later, the adaptive sampling feature may operate and send only a portion of your telemetry. Learn more about sampling.

Next steps