Serilog.Settings.Configuration 在作为包引用时不起作用

Serilog.Settings.Configuration is not working when it is referenced as package

我在尝试使用 appsettings.json.

配置 Serilog 时遇到了非常奇怪的行为

仅当我使用“添加项目引用”将 Serilog.Settings.Configuration 添加到我的项目时才有效。 当 Serilog.Settings.Configuration (3.2.0) 作为包安装时,我得到异常:

System.InvalidOperationException: 'Cannot create instance of type 'Serilog.Formatting.ITextFormatter' because it is either abstract or an interface.'

这是我用来解析配置的代码示例:

    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
        .Build();

    var logger = new LoggerConfiguration()
        .ReadFrom.Configuration(configuration)
        .CreateLogger();

这是我正在使用的配置:

  "Serilog": {
    "MinimumLevel": {
      "Override": {
        "Microsoft": "Debug",
        "System": "Debug"
      }
    },
    "Enrich": [ "FromLogContext" ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": {
            "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
            "template": "[{@t:yyyy-MM-dd HH:mm:ss.fff} {@l:u3}] {#if CorrelationId is not null}{CorrelationId} | {#end}{@m:lj}\n{@x}"
          }
        }
      }
    ]
  }

这是一个不工作的项目文件的例子(Serilog.Settings.Configuration 是 PackageReference):

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <UserSecretsId>0718b2b6-70cb-4a9b-9c43-30625d246f61</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <AssemblyName>SomeName</AssemblyName>
    <RootNamespace>SomeNamespace</RootNamespace>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
    <PackageReference Include="Serilog.Expressions" Version="3.2.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" />
  </ItemGroup>
    
</Project>

这是工作项目文件的示例(Serilog.Settings.Configuration 是 ProjectReference):

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <UserSecretsId>0718b2b6-70cb-4a9b-9c43-30625d246f61</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <AssemblyName>SomeName</AssemblyName>
    <RootNamespace>SomeNamespace</RootNamespace>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
    <PackageReference Include="Serilog.Expressions" Version="3.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\..\serilog-settings-configuration-dev\src\Serilog.Settings.Configuration\Serilog.Settings.Configuration.csproj" />
  </ItemGroup>

</Project>

我错过了什么? 看起来 Serilog.Settings.Configuration 在被引用为包时被忽略了,对吧? 我该如何解决?

更新 2021-10-11v3.3.0 has been released and includes the feature you're trying to use (Support of constructor parameters).


您尝试使用的功能(Support of constructor parameters) has not yet been released and as such is not present in the Serilog.Settings.Configuration 3.2.0 package which is why you get that error... This feature is planned to be released with 3.3.0 日期待定。

您的备选方案是:

1.) 使用预发布包(例如3.3.0-dev-00291

<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0-dev-00291" />

2.) 从源代码编译它,正如您在问题中所展示的那样

3.) 等待 3.3.0 出来