【发布时间】:2023-02-02 17:46:29
【问题描述】:
我们想要一个 ILogger 实例,以便它可以传递给其他库。 我们在下面进行了尝试,但 ILogger 实例未登录到 Application Insights。它成功登录到事件查看器。
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder => builder
.AddFilter("Default", LogLevel.Information)
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddEventLog(config => { config.LogName = "Pages"; config.SourceName = "Pages"; })
.AddApplicationInsights(telemetry =>
telemetry.ConnectionString = "my-key",
options => options = new ApplicationInsightsLoggerOptions()));
var serviceProvider = serviceCollection.BuildServiceProvider();
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger("my-logger");
logger.LogInformation("Hi");
我们添加了必要的包,即 Microsoft.Extensions.Logging 和 Microsoft.Extensions.Logging.ApplicationInsights
有没有办法,我们可以从 AppInsights 的 ServiceCollection 中获取一个 ILogger 实例?
【问题讨论】:
标签: asp.net webforms azure-application-insights event-viewer ilogger