[英]Event log in in worker service not published to Azure Application Insight
我正在构建一个Worker Service作为Windows Service 。 我使用EventLog
进行日志记录。 我还想添加ApplicationInsights
来记录事件。 我按照这篇文章https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service ,安装 SDK 并按照要求安装所有内容。 这是我的program.cs
配置。
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
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<Worker> logger, TelemetryClient tc)
{
_logger = logger;
this.tc = tc;
}
public Task StartAsync(CancellationToken cancellationToken)
{
this._logger.LogInformation("In Start");
using (tc.StartOperation<RequestTelemetry>("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
事件。 但是我在我期望使用_logger
发送的信息的trace
中找不到任何内容。 我检查了 output window 也没有发现针对_logger.LogInformation
发送任何遥测。
我在这里做错了什么,如何使 Logger Info 可用于ApplicationInsights
? 还是我没有找对地方?
Application Insight 默认将Warning
作为日志级别。 由于您使用级别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"
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.