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.
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 calling Application Insights TrackTrace() directly.
Note
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.
<configuration>
<system.diagnostics>
<trace>
<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 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.
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, if it's a Windows desktop project.
If you plan to use log4net or NLog, install it in your project.
In Solution Explorer, right-click your project, and select Manage NuGet Packages.
Search for Application Insights.
Select one of the following packages:
- ILogger: Microsoft.Extensions.Logging.ApplicationInsights
- NLog: Microsoft.ApplicationInsights.NLogTarget
- log4net: Microsoft.ApplicationInsights.Log4NetAppender
- System.Diagnostics: Microsoft.ApplicationInsights.TraceListener
- Microsoft.ApplicationInsights.DiagnosticSourceListener
- Microsoft.ApplicationInsights.EtwCollector
- Microsoft.ApplicationInsights.EventSourceListener
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 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.
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:
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<string, string> { { "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=<your-instrumentation_key-here>'))
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:
- 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 operation ID).
- 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 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?
- Make sure that Developer Analytics Tools is installed on the development machine. In Visual Studio, go to Tools > Extensions and Updates, and look for Developer Analytics Tools. If it isn't on the Installed tab, open the Online tab and install it.
- This project type might be one that Developer Analytics Tools doesn't support. Use manual installation.
Why is there no log adapter option in the configuration tool?
- Install the logging framework first.
- If you're using System.Diagnostics.Trace, make sure that you've configured it 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.
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.