.NET Core 3.0 辅助角色 - 无法使日志记录与应用程序见解或事件日志一起使用



我需要一个.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(无论我做什么事件日志都没有来自我的工作人员的任何记录。我确实在应用程序设置中设置了日志级别:

{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

2(Application Insights只有在.AddApplicationInsightsTelemetryWorkerService()被注释掉时才获取记录并且检测密钥被硬编码在logging.AddApplicationInsights("8d3bc77d-1cc3-4c4a-83e4-6d8aaa87f8f7")中。应该怎么做才能从应用程序设置中获取密钥?

3(为什么这么麻烦

更新

这是完整的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

ApplicationInsights添加到日志记录部分:

{
"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"
}
}

同样的事情-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
}
TIL有一个类似"Application Insights Telemetry WorkerService"的东西:-(

我用AddApplicationInsightsTelemetryWorkerService让它工作。但仅当您不调用logging.ClearProviders();或在ConfigureServices之前调用ConfigureLogging时,否则添加的日志记录提供程序将被清除。

这项工作:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
})
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddApplicationInsightsTelemetryWorkerService();
});

使用此配置:

"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
},
"ApplicationInsights": {
"InstrumentationKey": "xxx"
}
}

正如你在输出中看到的,AI键被正确拾取:

Application Insights Telemetry: {
"name": "Microsoft.ApplicationInsights.Dev.3b40adb096064da0816e7b8579aa443c.Message",
"time": "2019-11-13T07:52:11.0027057Z",
"iKey": "xxx",
"tags": {
"ai.application.ver": "1.0.0.0",
"ai.cloud.roleInstance": "xxx",
"ai.internal.sdkVersion": "il:2.11.0-21511",
"ai.internal.nodeName": "xxx"
},
"data": {
"baseType": "MessageData",
"baseData": {
"ver": 2,
"message": "Application started. Press Ctrl+C to shut down.",
"severityLevel": "Information",
"properties": {
"{OriginalFormat}": "Application started. Press Ctrl+C to shut down.",
"CategoryName": "Microsoft.Hosting.Lifetime",
"DeveloperMode": "true"
}
}
}
}

AddApplicationInsightsTelemetryWorkerService((是在WorkerService中启用应用程序洞察的推荐方法。它可以从appsettings.json获取ikey。这在内部设置日志记录提供程序(applicationinsightsloggingprovider(。

ApplicationInsights日志记录提供程序默认情况下仅捕获级别为"警告"或更高级别的日志(https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-日志(。所以这是我的第一个怀疑。你能记录一些警告或错误的东西,看看是否被捕获。或者将应用程序洞察捕获的默认级别更改为捕获信息或跟踪级别。(https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger#control-日志级别(

相关内容

  • 没有找到相关文章

最新更新