WebApi 单文件发布Serilog 失效

提问

WebApi 单文件发布Serilog 失效怎么解决

回答

配置文件Appsetting.json增加Using块
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],

示例

{
  "Serilog": {
    "Using":  [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "File", "Args": { "path": "Logs/log.txt" } }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "Destructure": [
      { "Name": "With", "Args": { "policy": "Sample.CustomPolicy, Sample" } },
      { "Name": "ToMaximumDepth", "Args": { "maximumDestructuringDepth": 4 } },
      { "Name": "ToMaximumStringLength", "Args": { "maximumStringLength": 100 } },
      { "Name": "ToMaximumCollectionCount", "Args": { "maximumCollectionCount": 10 } }
    ],
    "Properties": {
        "Application": "Sample"
    }
  }
}

原因

API 不兼容
某些 API 与单文件部署不兼容。 如果应用程序使用这些 API,可能需要进行修改。 如果使用第三方框架或包,则它们可能使用了这样的 API 并需要修改。 出现问题的最常见原因是依赖于应用程序附带的文件或 DLL 的文件路径。

下表提供了用于单文件的相关运行时库 API 详细信息。

API 注意
Assembly.CodeBase 引发 PlatformNotSupportedException。
Assembly.EscapedCodeBase 引发 PlatformNotSupportedException。
Assembly.GetFile 引发 IOException。
Assembly.GetFiles 引发 IOException。
Assembly.Location 返回空字符串。
AssemblyName.CodeBase 返回 null。
AssemblyName.EscapedCodeBase 返回 null。
Module.FullyQualifiedName 返回值为 的字符串,或引发异常。
Marshal.GetHINSTANCE 返回 -1。
Module.Name 返回值为 的字符串。

参考

https://github.com/serilog/serilog-settings-configuration#net-50-single-file-applications

https://learn.microsoft.com/zh-cn/dotnet/core/deploying/single-file/overview?tabs=cli

posted @ 2023-03-17 10:45  喜爱糖葫芦  阅读(54)  评论(0编辑  收藏  举报