Add AppInsights in .NET
Describes how to implement AppInsights in the .NET backend for Optimizely Configured Commerce.
Note
Optimizely Configured Commerce does not support AppInsights out-of-the-box. This requires project customization.
You must have version 5.2.2312 or later.
Prerequisites
- Optimizely Configured Commerce
- .NET framework
- AppInsights keys
Note
Optimizely does not provide the AppInsights keys. You or your partner must provide them.
Implementing Application Insights
There are two options for setting your connection string.
Adding an assembly attribute sets the same connection string across all environments your code is deployed.
[assembly: EnableApplicationInsights("application_insights_connection_string")]
For more customization, you can define a method that is called on application start and set the connection string there. This lets the connection string vary between production and sandbox.
using Insite.Common.Providers;
using Insite.Core;
// this attribute is required, it tells dotnet what method to call at startup.
[assembly: System.Web.PreApplicationStartMethod(typeof(Extensions.WebStart), nameof(Extensions.WebStart.Initialize))]
namespace Extensions;
public static class WebStart
{
// this method can contain any logic to determine the connection string, however it happens before the application is fully ready. IOC is not available.
public static void Initialize()
{
if (AppSettingProvider.Current["Environment"] == "Production")
{
ApplicationInsights.ConnectionString = "application_insights_connection_string"
}
}
}
Customizing Application Insights
Your Extensions.dll
can define any number of ITelemetryInitializer
classes. You can use them to customize data such as RoleName
and RoleInstance
.
public class CustomTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.Cloud.RoleName = "CustomRole";
telemetry.Context.Cloud.RoleInstance = "CustomInstance";
}
}
See Enable a framework extension for Application Insights JavaScript SDK. For Spire, partners can implement as much of this as needed.
Ensure to match environment keys with C# code. This can be swapped with the JS build constant IS_PRODUCTION
to change keys. See Microsoft's applicationinsights-react-js repository for information.
Updated 4 days ago