我正在构建一个Worker服务作为Windows服务..。我使用EventLog用于日志记录。我还想补充一点
ApplicationInsights来记录事件。我关注这篇文章https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service,使用Nuget安装SDK,并按要求设置所有内容。这是我的program.cs配置。
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService();
services.AddApplicationInsightsTelemetryWorkerService();
services.CustomeDependencyInjection();
})
.UseWindowsService()
.ConfigureLogging(logging =>
{
logging.AddEventLog(eventLogSetting =>
{
eventLogSetting.LogName = "MyTestEventLog";
eventLogSetting.SourceName = "MyTestEventApp";
});
});下面是appsetting.json文件
{
"ApplicationInsights": {
"InstrumentationKey": "bd******-****-****-****-***********b"
},
Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
},
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}下面是一个日志记录示例,位于worker.cs文件
public Worker(ILogger logger, TelemetryClient tc)
{
_logger = logger;
this.tc = tc;
}
public Task StartAsync(CancellationToken cancellationToken)
{
this._logger.LogInformation("In Start");
using (tc.StartOperation("Operation"))
{
/**
my service starting code
**/
this._logger.LogInformation("Service is being started");
tc.TrackEvent("Worker service starting operation completed.");
}
this._logger.LogInformation( "Service Started");
return Task.CompletedTask;
}当我运行应用程序时,我可以看到一个customEvent..。我也能看到request事件。但是我在里面找不到任何东西trace我期望使用以下命令发送信息的位置_logger..。我检查了输出窗口,也没有发现针对_logger.LogInformation..。
我在这里做错了什么?如何使记录器信息可供ApplicationInsights什么?还是我找对地方了?
发布于 2021-01-05 03:59:27
Application Insight默认为Warning作为日志级别。由于您使用level进行跟踪Information您需要使用appsettings.json文件配置应用程序洞察:
{
"ApplicationInsights": {
"InstrumentationKey": "bd******-****-****-****-***********b"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}https://stackoverflow.com/questions/65565879
复制相似问题