Add AppInsights in .NET
How to implement AppInsights in the .NET backend for Optimizely Configured Commerce.
Prerequisites
- Optimizely Configured Commerce 5.2.2312 or later.
- Application Insights Connection String
Note
Optimizely does not provide the Application Insights Connection String You or your partner must provide them.
Implement Application Insights
5.2.2411+
You can add the Application Insights connection string at System > Developer > Application Insights Connection String.
You must restart the site when adding or changing the connection string.
5.2.2312 - 5.2.2410
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"
}
}
}
Customize Application Insights
Below are some ways to customize Application Insights. See Microsoft's documentation for what else you can do.
Use Extensions to customize data
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.
Configure availability
You can configure availabilty in Application Insights. See Micrsoft's documentation on availability tests.
Updated 3 days ago