It is not clear if the approach to add line
services.AddApplicationInsightsTelemetry("ikeyhere");
is mutually exclusive to the code section mentioned earlier
services.AddLogging(loggingBuilder =>
{
// Optional: Apply filters to configure LogLevel Trace or above is sent to ApplicationInsights for all
// categories.
loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
loggingBuilder.AddApplicationInsights("--YourAIKeyHere--");
});
Could you please clarify?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@gopala000 Thank you for the question! We are investigating this and will get back to you shortly.
@gopala000
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 is recommended for customized configuration.
Please comment and let us know if you have further questions.
Thank you for the quick response. I'd need to set Component (application's name) and trace level set per environment. I see trace level can be read from appsettings.config based on this article. It will be helpful to know what config variables are available to customize.
In our system, there are windows services as well. Can AddApplicationInsightsTelemetry() used there too or is there a similar nuget library available for use in windows services?
For Windows Services, please use Nuget package Microsoft.ApplicationInsights
A code sample is here
I will need some more clarity on your earlier question.
When you say Application's name do you mean appName property? appName cannot be customized from the SDK.
services.AddApplicationInsightsTelemetry("ikeyhere") and loggingBuilder.AddApplicationInsights("--YourAIKeyHere--");
I guess this was the original questions - these are two separate extension methods serving different purposes. 1st one is used to add ApplicationInsights monitoring (i.e Requests, Dependencies, counters etc), and the 2nd one is to add ApplicationInsights logging provider.
@KalyanChanumolu-MSFT , I need a way to filter the log by application name to investigate production issues. If appName cannot be customized, any property that I can use as filter will be helpful. Even better will be a sample appSettings.config with various customizable properties .
@gopala000 Sorry for the delay in getting back to you
You will have to use a custom dimension to acheive the above
Please use a Telemetry initializer to assign the properties you wish to populate per environment/application.
Sample code below
``` C#
public class MyCustomTelemetryPropertyInitializer : ITelemetryInitializer
{
IHttpContextAccessor httpContextAccessor;
public MyCustomTelemetryPropertyInitializer(IHttpContextAccessor httpContextAccessor)
{
this.httpContextAccessor = httpContextAccessor;
}
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.GlobalProperties.Add("MyApplicationName", "ApplicationInsightsTester");
}
}
in Startup.cs,
```C#
services.AddSingleton<ITelemetryInitializer, MyCustomTelemetryPropertyInitializer>();
services.AddApplicationInsightsTelemetry();
You will then be able to query in Log Analytics using the below query
requests
| take 100
| where customDimensions["MyApplicationName"] == "ApplicationInsightsTester"
Please try and let us know
@gopala000 We will now proceed to close this thread as resolved, but if there are any further questions regarding the documentation, please tag me in your reply and we will be happy to continue the conversation.
So what was the verdict? @KalyanChanumolu-MSFT is saying they're mutually exclusive, but @cijothomas is saying they enable two different things (one for monitoring, the other for logging)
Not sure if someone actively monitors closed issues. Please open a new issue under the respective docs if you need clarification.
This is answer to the original question:
https://github.com/MicrosoftDocs/azure-docs/issues/27395#issuecomment-473929451
Most helpful comment
@gopala000
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 is recommended for customized configuration.
Please comment and let us know if you have further questions.