Hi @Pamodi Rathnayake , Welcome to Microsoft Q&A,
It is recommended to configure Application Insights in appsettings.json
instead of using ConfigurationManager.AppSettings
:
{
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=你的InstrumentationKey"
}
}
Do not create TelemetryConfiguration manually, use services.AddApplicationInsightsTelemetry() instead. Use dependency injection (DI) to manage ILoggerFactory, do not create ApplicationInsightsLoggerProvider manually.
var services = new ServiceCollection();
var appInsightsConnectionString = ConfigurationManager.AppSettings["ApplicationInsights:ConnectionString"];
if (!string.IsNullOrEmpty(appInsightsConnectionString))
{
services.AddApplicationInsightsTelemetry(options =>
{
options.ConnectionString = appInsightsConnectionString;
});
services.AddLogging(logging =>
{
logging.AddApplicationInsights();
});
var serviceProvider = services.BuildServiceProvider();
_loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
_logger = _loggerFactory.CreateLogger(_categoryName);
}
If an error occurs, please let me know the specific error message and I will follow up.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.