发布应用程序时,Serilog 不会生成日志到控制台或文件 (.net 6)

标签 serilog

我在 .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/

相关文章:

trace - 无法使用 SerilogTraceListener 将 Trace.WriteLine 重定向到 Serilog

c# - 如何在深度调用中使用 serilog 跟踪请求

c# - 如何将 Autofac 与 Serilog 包装类一起使用

c# - Serilog:防止创建空日志文件

c# - Serilog的ReadFrom.KeyValuePairs方法如何使用?

asp.net-core - Serilog 电子邮件接收器启用 SSL 错误检查

logging - 当源上下文不是特定值时,最好的丰富方式

c# - 程序类中的 Serilog

c# - serilog通过.net核心中的记录器引用为对象建立索引