如何为.NET Core 3.0 Worker Service设置事件日志 [英] How to setup event log for .NET Core 3.0 Worker Service
问题描述
我正在使用带有.NET Core 3.0预览版的新Worker Service应用程序模板,并尝试使用AddEventLog
方法添加事件日志记录.但是,我无法通过Windows中的事件查看器"看到任何日志.
I'm working with the new Worker Service app template with .NET Core 3.0 Preview and am trying to add event logging using the AddEventLog
method. However, I cannot see any of my logs via the Event Viewer in Windows.
我有一个非常简单的Worker应用程序设置,并已配置了Program.cs
文件中的日志记录,如下所示:
I have a very simple Worker app setup and have configured logging in the Program.cs
file as follows:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureLogging((context, logging) =>
{
logging.AddEventLog(new EventLogSettings()
{
SourceName = "MyTestSource",
LogName = "MyTestLog"
});
})
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
});
然后我在Worker.cs
文件中有一些日志记录语句,如下所示:
I then have some logging statements in the Worker.cs
file as follows:
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
public override async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Worker started at: {DateTime.Now}");
await base.StartAsync(cancellationToken);
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Worker stopped at: {DateTime.Now}");
await base.StopAsync(cancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation( $"Worker running at: {DateTime.Now}");
await Task.Delay(1000, stoppingToken);
}
}
要设置事件日志,我从提升的Powershell提示符中运行了以下命令:
To setup the event logs, I ran the following from an elevated Powershell prompt:
New-EventLog -LogName MyTestLog -Source MyTestSource
如果打开事件查看器,则可以在应用程序和服务日志"下面看到"MyTestLog".
If I open the Event Viewer I can see "MyTestLog" listed below "Applications and Services Logs".
然后,要将我的Worker设置为Windows服务,我从提升权限的命令提示符下运行了以下命令:
Then, to set up my Worker as a Windows service, I ran the following commands from an elevated command prompt:
dotnet publish -o publish
(发布项目并输出到发布目录)
dotnet publish -o publish
(Publishes project and outputs to publish directory)
sc create MyTestService binPath=<path to exe in publish directory>
服务已成功创建,我可以在服务"查看器应用程序中看到它.从那里,我手动启动该服务,然后在事件查看器"中返回并没有显示日志.
The service is created successfully, and I can see it in the Services viewer application. From there, I manually start the service and then check back in the Event Viewer and no logs are displayed.
我期望会有一些日志.但是,"MyTestLog"部分在事件查看器中保持空白.
I was expecting there to be some logs. However, the "MyTestLog" section remains empty in the Event Viewer.
推荐答案
So I was able to find the issue reported here.
从本质上讲,它可以归结为最新的.NET Core 3.0预览版中的一项更改,在该更改中,默认情况下现在按警告级别过滤事件日志.因此,您看不到任何信息日志.
Essentially it boils down to a change in the latest .NET Core 3.0 Preview where Event Logs are filtered at the warning level by default now. Thus, you cannot see any information logs.
解决方法是简单地将appsettings.json
文件编辑为如下所示:
The fix was to simply edit the appsettings.json
file to look like the following:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"EventLog": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}
这将覆盖默认设置,并允许再次查看信息日志.
This overrides the default set and allows information logs to be viewed again.
这篇关于如何为.NET Core 3.0 Worker Service设置事件日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!