AddApplicationInsightsTelemetry()
and UseApplicationInsights()
both are ways to add instrumentation capabilities to your application. These are "mutually exclusive" and you should use only one of them.
UseApplicationInsights()
will automatically read and initialize configuration variables from appsettings.json
and is the easiest way to get started with default settings.
AddApplicationInsightsTelemetry()
is recommended for customized configuration.
Telemetry Initializers set context properties that are sent along with every item of telemetry.
You can write your own initializers to set context properties.
The standard initializers are all set either by the Web or WindowsServer NuGet packages and can be found at ApplicationInsights.config reference - Azure - Azure Monitor | Microsoft Docs.
Examples of standard initializers are:
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"