我在 .net 6 上有一个工作应用程序。使用 dotnet run
运行应用程序时,Serilog 工作正常(控制台和文件日志记录)。但是,当从可执行应用程序运行时,它不会记录任何内容(甚至不创建日志文件)
我正在编译应用程序:
dotnet publish -r linux-x64 -c Release -o app/publish --self-contained true -p:PublishSingleFile=true -p:PublishReadyToRun=false -p:PublishTrimmed=false
serilog.json 文件是:
{
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information",
"System": "Information",
"MyApp.LoadData": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:o}][{Level:u4}][{ThreadId}][{SourceContext}] {Message}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"buffered": true,
"flushToDiskInterval": 15,
"outputTemplate": "[{Timestamp:o}][{Level:u4}][{ThreadId}][{SourceContext}] {Message}{NewLine}{Exception}",
"path": "customerscredit//logs//LoadCredit.log",
"retainedFileCountLimit": 31,
"rollingInverval": "Day",
"textFormatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId", "WithExceptionDetails"] ,
"Properties": {
"ApplicationName": "MyApp.LoadData",
"Product": "MyApp",
"Env": ""
}
}
}
我在配置中遗漏了什么吗?是否有编译应用程序以使 Serilog 工作的特定方法?
感谢您的帮助。
最佳答案
发现问题了。来自 Serilog.Settings.Configuration 文档:
NET 5.0 Single File Applications Currently, auto-discovery of configuration assemblies is not supported in bundled mode. DO use Using section for workaround.
因此,我也需要将“Using”设置如下:
"Using": ["Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.Thread"]
关于发布应用程序时,Serilog 不会生成日志到控制台或文件 (.net 6),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70127316/