Explore .NET/.NET Core and Python 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. For Python applications, send diagnostic tracing logs by using AzureLogHandler in OpenCensus Python for Azure Monitor. You can then explore and search for them. Those logs are merged with the other log files from your application. You can use them to identify traces that are associated with each user request and correlate them with other events and exception reports.

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 calling Application Insights TrackTrace() directly.

On March 31, 2025, support for instrumentation key ingestion will end. Instrumentation key ingestion will continue to work, but we'll no longer provide updates or support for the feature. Transition to connection strings to take advantage of new capabilities.

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.

Configure Application Insights to collect logs

Add Application Insights to your project if you haven't done that yet and there is 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.

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, if it's 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.

For each source, you can set the following parameters:

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.

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

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.

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.

For each source, you can set the following parameters:

Use the Trace API directly

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

TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault(); var telemetryClient = new TelemetryClient(configuration); telemetryClient.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:

TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault(); var telemetryClient = new TelemetryClient(configuration); telemetryClient.TrackTrace("Slow database response", SeverityLevel.Warning, new Dictionary  < < "database", "db.ID" >>); 

Now you can easily filter out in Transaction Search all the messages of a particular severity level that relate to a particular database.

AzureLogHandler for OpenCensus Python

The Azure Monitor Log Handler allows you to export Python logs to Azure Monitor.

Instrument your application with the OpenCensus Python SDK for Azure Monitor.

This example shows how to send a warning level log to Azure Monitor.

import logging from opencensus.ext.azure.log_exporter import AzureLogHandler logger = logging.getLogger(__name__) logger.addHandler(AzureLogHandler(connection_string='InstrumentationKey=')) logger.warning('Hello, World!') 

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 Transaction Search.

You can, for example:

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 might operate and send only a portion of your telemetry. Learn more about sampling.

Troubleshooting

Find answers to common questions.

What causes delayed telemetry, an overloaded network, and inefficient transmission?

System.Diagnostics.Tracing has an Autoflush feature. This feature causes SDK to flush with every telemetry item, which is undesirable, and can cause logging adapter issues like delayed telemetry, an overloaded network, and inefficient transmission.

How do I do this for Java?

In Java codeless instrumentation, which is recommended, the logs are collected out of the box. Use Java 3.0 agent.

The Application Insights Java agent collects logs from Log4j, Logback, and java.util.logging out of the box.

Why is there no Application Insights option on the project context menu?

Why is there no log adapter option in the configuration tool?

Why do 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 are prompted to sign in to Azure and create an Application Insights resource or reuse an existing one. It should fix the problem.

Why can I see traces but not other events in diagnostic search?

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.

Why don't I see some log entries that I expected?

Perhaps 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. In this case, the adaptive sampling feature might operate and send only a portion of your telemetry. Learn more about sampling.

Next steps