Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Add AppInsights in .NET

Describes 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.

Implementing Application Insights

5.2.2411+

The Application Insights connection string can be added to the System Setting at System - Developer - Application Insights Connection String

After adding or changing the connection string, the site must be restarted.

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"
        }
    }
} 

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.