Assign custom property in Application Insights

Use a custom Telemetry initializer to assign additional context properties you wish to populate.

Sample code below

public class MyCustomTelemetryPropertyInitializer : ITelemetryInitializer
{
    IHttpContextAccessor httpContextAccessor;

    public MyCustomTelemetryPropertyInitializer(IHttpContextAccessor httpContextAccessor)
    {
        this.httpContextAccessor = httpContextAccessor;
    }

    public void Initialize(ITelemetry telemetry)
    {
        telemetry.Context.GlobalProperties.Add("MyApplicationName", "ApplicationInsightsTester");
    }
}

You will then be able to query in Log Analytics using the below query

requests
| take 100 
| where customDimensions["MyApplicationName"] == "ApplicationInsightsTester"

Comments

Leave a Comment

All fields are required. Your email address will not be published.