我需要一个.NET3.0工作服务来登录到AzureApplicationInsights和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"
});
});
}
(一)
不管我做什么
{
"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")
. 该怎么做才能从应用程序设置中获取密钥?
为什么这么麻烦?
更新
这是全部
app.development.settings
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
},
"ApplicationInsights": {
"InstrumentationKey": "8d3bc77d-1cc5-what-ever-0000000000"
}
}
更新2
应用程序权限
登录中
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
},
"ApplicationInsights": {
"InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
}
}
更新3
属性名称已更改
Logging:ApplicationInsights:LogLevel
:
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ApplicationInsights": {
"LogLevel": {
"PushNotificationsWatchdog.Worker": "Information"
}
}
},
"ApplicationInsights": {
"InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
}
}
分辨率
多亏了彼得·邦斯!
ConfigureServices()
和
ConfigureLogging()
使用
appsettings
从更新2开始,现在开始工作了!所以我们开始了:
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
}