我需要.NET 3.0辅助服务才能登录到Azure Application Insights和EventLog。这些都不起作用(几乎)!
这是我的CreateHostBuilder
:
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext,services) =>
{
IConfiguration configuration = hostContext.Configuration;
WatchdogOptions options = configuration.GetSection("WorkerOptions").Get<WatchdogOptions>();
if (options == null) throw new WatchdogException("WorkerOptions settings are not set");
services.AddSingleton(options);
services.AddHostedService<Worker>()
// .AddApplicationInsightsTelemetryWorkerService();
;
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
//logging.AddConsole();
logging.AddApplicationInsights("<instr-key>");
logging.AddEventLog(new EventLogSettings
{
SourceName = "PNWatchdog",LogName = "Watchdog"
});
});
}
1)无论我做什么,EventLog都没有来自我的工作人员的任何记录。我确实在应用程序设置中设置了日志记录级别:
{
"Logging": {
"LogLevel": {
"Default": "Debug","System": "Information","microsoft": "Information"
}
},"EventLog": {
"LogLevel": {
"Default": "Information","microsoft.Hosting.Lifetime": "Information"
}
}
}
2)应用洞察力仅在注释.AddApplicationInsightsTelemetryWorkerService()
并且将检测键硬编码在logging.AddApplicationInsights("8d3bc77d-1cc3-4c4a-83e4-6d8aaa87f8f7")
中时获得记录。应该怎么做才能从应用程序设置中获取密钥?
3)为什么这么麻烦?
更新
完整的app.development.settings
:
{
"Logging": {
"LogLevel": {
"Default": "Debug","microsoft": "Information"
},"EventLog": {
"LogLevel": {
"Default": "Information","microsoft.Hosting.Lifetime": "Information"
}
}
},"ApplicationInsights": {
"InstrumentationKey": "8d3bc77d-1cc5-what-ever-0000000000"
}
}
更新2
ApplicationInsights 已添加到记录部分:
{
"Logging": {
"LogLevel": {
"Default": "Debug","microsoft.Hosting.Lifetime": "Information"
}
},"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
},"ApplicationInsights": {
"InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
}
}
更新3
Logging:ApplicationInsights:LogLevel
的属性名称已更改:
{
"Logging": {
"LogLevel": {
"Default": "Debug","ApplicationInsights": {
"LogLevel": {
"PushNotificationsWatchdog.Worker": "Information"
}
}
},"ApplicationInsights": {
"InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
}
}
同一件事-App Insights中没有记录。仪表键正确。
解决方案
感谢@ peter-bons!
因此,我更改了ConfigureServices()
和ConfigureLogging()
的顺序,并使用了UPDATE 2中的appsettings
,它现在可以工作了!这样我们就可以了:
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddEventLog(new EventLogSettings
{
SourceName = "PNWatchdog",LogName = "Watchdog"
});
})
.ConfigureServices((hostContext,services) =>
{
services.AddHostedService<Worker>()
.AddApplicationInsightsTelemetryWorkerService();
})
.UseWindowsService(); // windows only feature
}