作为 Windows 服务运行时,.Net Core 6 Worker Service 不写入日志
我使用 C# / .Net Core 6 (Visual Studio 2022) 创建了一个辅助服务。
如果通过 Visual Studio 运行或直接从 Windows 资源管理器/PowerShell 启动,它会按预期写入日志文件。但是,当作为 Windows 服务安装时,它不会创建或写入日志文件。
这是我的program.cs:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.File("./logs/log-.txt", rollingInterval:RollingInterval.Day)
.CreateBootstrapLogger();
try
{
Log.Information("Starting the Service");
IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureAppConfiguration((hostContext, configBuilder) =>
{
configBuilder
//.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.MachineName}.json", true, true)
.Build();
})
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext())
.ConfigureServices((hostContext, services) =>
{
...
})
.Build();
await host.RunAsync();
}
catch (Exception ex)
{
Log.Fatal(ex, "There was a problem starting the service");
}
finally
{
Log.Information("Service successfully stopped");
Log.CloseAndFlush();
}
我在appsettings.json中有这个:
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Debug" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path": ".\\logs\\log-.txt",
"rollingInterval": "Day"
}
}
],
"Enrich": [ "FromLogContext" ]
}
如果我包含“SetBasePath”部分,服务将无法启动(尽管它仍然通过Visual Studio运行)。
我从 Visual Studio 发布服务,目标框架 = net6.0,部署模式 = 框架相关,目标运行时 = win-x64。
注意:我在 Program.cs 中创建了一个 Logger,因为我想记录服务的启动和停止/崩溃。
I have created a Worker Service using C# / .Net Core 6 (Visual Studio 2022).
It writes to a log file as expected if run via Visual Studio or started directly from Windows Explorer / PowerShell. However, when installed as a Windows Service, it does not create or write to a log file.
This is my program.cs:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.File("./logs/log-.txt", rollingInterval:RollingInterval.Day)
.CreateBootstrapLogger();
try
{
Log.Information("Starting the Service");
IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureAppConfiguration((hostContext, configBuilder) =>
{
configBuilder
//.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile(quot;appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddJsonFile(quot;appsettings.{Environment.MachineName}.json", true, true)
.Build();
})
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext())
.ConfigureServices((hostContext, services) =>
{
...
})
.Build();
await host.RunAsync();
}
catch (Exception ex)
{
Log.Fatal(ex, "There was a problem starting the service");
}
finally
{
Log.Information("Service successfully stopped");
Log.CloseAndFlush();
}
I have this in appsettings.json:
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Debug" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path": ".\\logs\\log-.txt",
"rollingInterval": "Day"
}
}
],
"Enrich": [ "FromLogContext" ]
}
If I include the "SetBasePath" part, the service fails to start (although it still runs via Visual Studio).
I am publishing the service from Visual Studio with Target Framework = net6.0, Deployment Mode = Framework Dependent, Target runtime = win-x64.
Note: I have created a Logger in Program.cs because I wanted to log the start and stop / crash of the service.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如其他人所指出的,服务运行的工作目录与进程位置不同。 我喜欢使用的一个老技巧是设置首先将工作目录复制到进程的二进制目录:
As others have noted, services run with a working directory different than the process location. One old trick I like to use is to set the working directory to the process' binary directory first thing:
就我而言,我只是将
部署模式
更改为自包含,并选择生成单个文件
。虽然它会创建一个大文件,但现在编译器将所有依赖库嵌入到一个文件中。
In my case, I just changed
Deployment mode
to Self-Contained and choose theProduce single file
.Although it will create a large file but now compiler embed all dependent libraries into a single file.