Application Insights lets you troubleshoot exceptions that happen in the Web Apps and performance issues. See [Creating an Optimizely Cloud Account](🔗) to request access to Application Insights for your environment. To learn how to use this tool, see Microsoft's [Application Insights for ASP.NET Core applications](🔗).
Application Insights is configured automatically for all sites in the DXP from the _EPiServer.CloudPlatform.Cms_ package.
## Application Insight API key
Customers and partners can request an Application Insights API key for envionments from [[email protected]](🔗).
The API Key has list and read-only permissions.
You get only one key per requested environment; that is, if you request keys for Integration, Preproduction, and Production environments, you will get three keys.
If you need a new key, the previous key is overwritten.
For information about read-only API access key, see [Using the API](🔗).
## Runtime and buildtime instrumentation
By default, sites in DXP use _Runtime instrumentation_, which does not require any changes in the web application, but does not store log messages from the Optimizely platform in Application Insights; you can find the logs in the [DXP Dashboard](🔗).
To use all the Application Insights features, you need to use both runtime and buildtime instrumentation. The buildtime instrumentation is enabled when installing Application Insights SDK. This consists of two NuGet packages:
_Microsoft.ApplicationInsights.Web_ to collect telemetry.
_Microsoft.ApplicationInsights.TraceListener_ to get the logs (see also [Custom logging](🔗) below).
See [Instrument Web Apps at runtime with Application Insights Codeless Attach (Microsoft documentation)](🔗) for details about the difference between runtime and buildtime instrumentation.
## Single-page application configuration
Set up usage tracking parameters to ensure that the website traffic volume monitoring is correct. For websites using the Single Page Application (SPA) concept, you need to specifically configure the page view tracking, because pages on these websites will not reload during usage. See [Consumption metrics](🔗) for how to configure page view tracking for SPAs.
## Custom logging
To direct Optimizely logs to Application Insights as traces for you to explore and search, configure diagnostics tracing with System.Diagnostics.Trace.
Install the NuGet package _Microsoft.ApplicationInsights.TraceListener_, which modifies your _web.config_ to add trace listeners. When installed, a configuration entry similar to the following code is added to _web.config_.
See [Application Insights logging adapters](🔗) and [Explore .NET/.NET Core and Python trace logs in Application Insights (Microsoft Documentation)](🔗) for information and examples.
Note
After you have installed the _Microsoft.ApplicationInsights.TraceListener_ package, you also need to install _Microsoft.ApplicationInsights.Web_ package, to collect site telemetry.
When configured, your logs are merged with the other telemetry coming from your application, so that you can identify the traces associated with servicing each user request, and correlate them with other events and exception reports.
If you already have your own Application Insights settings via _ApplicationInsights.config_ file when you deploy your site to DXP, the Applications Insights resource provisioned by DXP overwrites your **Instrumentation Key**, which means that any custom logs or telemetry information that you are adding is saved to an Application Insights resource provisioned by DXP.
## Content security policy
Default instrumentation in the environment does not support content security policy (CSP). To configure instrumentation with CSP enabled, [customer support](🔗) can disable the default instrumentation and then you can inject the script using a nonce token. Set the nonce token in the CSP header or a meta-tag; it must be unique for each page load. The following example shows how this can look using the meta-tag:
## Troubleshoot Application Insights
### When logs and metrics are not collected automatically, it could be the following.
Application Insights SDK is being installed but not yet configured.
_System.Diagnostics.DiagnosticSource.dll_ exists in the solution, which conflicts with the automatic instrumentation.
_System.Diagnostics.DiagnosticSource.dll_ is also bundled with some versions of _Microsoft.CodeDom.Providers.DotNetCompilerPlatform_, in which case you can resolve the issue by upgrading it or changing DotNetCompilerPlatform\-version.
### Verify that the following dll files are not in the code package for automatic instrumentation to work.
_Microsoft.ApplicationInsights.dll_
_Microsoft.AspNet.TelemetryCorrelation.dll_
_System.Diagnostics.DiagnosticSource.dll_
### Application Insights codeless attach not working.
If any of the following entries exist, it causes the codeless monitoring to stop working or “back off.” You should remove the following packages from your application to give way for codeless monitoring:
Microsoft.ApplicationInsights
System.Diagnostics.DiagnosticSource
Microsoft.AspNet.TelemetryCorrelation
Note
If you can not remove the following packages due to code dependencies, use the following recommendation.
**Recommendation**
The Microsoft article [Add Application Insights Automatically](🔗) suggests that a change in Visual Studio should be a very simple step, and that doing it once should resolve all future conflict issues you may face. To automatically add Application Insights to a template-based ASP.NET web app, perform the following steps from within your ASP.NET web app project in Visual Studio. The DXP solution uses an app setting for **InstrumentationKey**, so you do not need to add it to the _ApplicationsInsights.config_ file.
Select **Add Application Insights Telemetry** \> **Application Insights Sdk (local)** > **Next** > **Finish** > **Close**.

Select **Project**\> **Manage NuGet Packages** > **Updates**. Then update each ApplicationInsights NuGet package to the latest stable release.
Run your application by selecting **IIS Express**. A basic ASP.NET app opens. As you browse through the pages on the site, telemetry is sent to Application Insights.
If you do not want to use Visual Studio, you can manually install it also by following the instructions in the Microsoft article: [Add Application Insights Manually](🔗)