辅助服务中的事件登录未发布到 Azure Application Insight
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 ,使用 Nuget 安装 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
事件。但是我在 trace
中找不到任何我期望使用 _logger
发送的信息。我检查了输出 window,也没有发现针对 _logger.LogInformation
.
发送了任何遥测数据
我在这里做错了什么以及如何让 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"
}
}
}
}
我正在构建一个 Worker Service 作为 Windows Service。我使用 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<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
事件。但是我在 trace
中找不到任何我期望使用 _logger
发送的信息。我检查了输出 window,也没有发现针对 _logger.LogInformation
.
我在这里做错了什么以及如何让 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"
}
}
}
}