我创建了一个 C# .net5.0 控制台应用程序,并且在测试期间 Serilog 一直在正常工作,登录到控制台和文件(相同的文件夹;path="log.txt")。但是,当我在我们服务器上的应用程序上运行时,控制台和文件日志接收器都不起作用!我现在假设问题不在于接收器本身,而是 Serilog 没有实际工作。
我试过启用自我日志:
Serilog.Debugging.SelfLog.Enable(msg =>
Console.WriteLine(msg)
);
但即使在我的开发环境的调试器中运行,Console.WriteLine(msg)
也不会调用该行!
我appsettings.json
的如下:
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information",
"System": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "log.txt",
"rollingInterval": "Infinite",
"outputTemplate": "{Timestamp:HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"shared": false
}
}
],
"Enrich": [ "FromLogContext" ]
},
"Database": {
"Server": "(local)",
"Database": "ActivbaseLive"
},
"Email": {
"SmtpHost": "localhost",
"SmtpPort": 25,
"SmtpSecurityOption": "None",
"SmtpUsername": null,
"SmtpPassword": null,
"SmtpSender": "\"Activbase Learning Platform\" <noreply@activbase.net>"
}
}
我尝试过绝对路径(在 中使用双反斜杠appsettings.json
)。我已经尝试预先创建日志文件(例如log.txt
和log200428.txt
)并将权限设置为“所有人完全控制”,但这些更改都没有解决问题,并且他们没有解释为什么控制台接收器也不写入。
以下是在启动期间如何配置 Serilog,这是我怀疑问题所在(即使它在开发环境中工作):
return Host.CreateDefaultBuilder()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
})
.UseSerilog((hostContext, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom.Configuration(hostContext.Configuration);
})
.ConfigureAppConfiguration((hostContext, builder) =>
{
builder.AddEnvironmentVariables();
})
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
...
});
}
任何想法为什么 Serilog 不在生产中工作?