0

我在我的 .net core 3.1 应用程序中使用了 steeltoe 2.4.3。最近我将 steeltoe 包更新到 v3.0.1,看起来日志不是以 serilog 格式出现的。我无法弄清楚出了什么问题。我的 appsettings 中确实有 serilog 配置,如下所示。

  {
    "management": {
    "endpoints": {
      "path": "/myexample/cloudfoundryapplication",
      "cloudfoundry": {
        "validateCertificates": false
      },
      "actuator": {
        "exposure": {
          "include": [ "*" ],
          "exclude": [ "env", "refresh" ]
        }
      }
    }
  },

  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Information",
        "Steeltoe": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
        }
      },
      {
        "Name": "Trace",
        "Args": {
          "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  },
  "AllowedHosts": "*"
}

Program.cs 如下所示

    public static class Program
    {

        public static void Main(string[] args)
        {
            Activity.DefaultIdFormat = ActivityIdFormat.W3C;
            CreateHostBuilder(args).Build().Run();
        }
       
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .AddCloudFoundryConfiguration()
                .ConfigureLogging((builderContext, loggingBuilder) =>
                {
                    // Add Serilog Dynamic Logger 
                    loggingBuilder.AddDynamicSerilog();
                })
                .AddCloudFoundryActuators()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

我的 .csproj 如下所示。

<PropertyGroup>
   <TargetFramework>netcoreapp3.1</TargetFramework>
   <GenerateDocumentationFile>true</GenerateDocumentationFile>
 </PropertyGroup>

 <ItemGroup>
   <None Update="wwwroot\**\*;*.yml">
     <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
   </None>
 </ItemGroup>

 <ItemGroup>
   <PackageReference Include="Serilog.Sinks.Trace" Version="2.1.0" />
   <PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
   <PackageReference Include="Steeltoe.Extensions.Logging.DynamicSerilogCore" Version="3.0.1" />
   <PackageReference Include="Steeltoe.Management.CloudFoundryCore" Version="3.0.1" />
   <PackageReference Include="Steeltoe.Extensions.Configuration.CloudFoundryCore" Version="3.0.1" />
 </ItemGroup>

</Project>
4

1 回答 1

1

AddDynamicSerilog不(当前)添加控制台接收器,请尝试将您的代码更新为如下所示:

AddDynamicSerilog(new LoggerConfiguration().WriteTo.Console())

如果未提供配置,此 PR将导致自动添加控制台接收器,并且可能会在 Steeltoe 3.0.2 中发布(尚无 ETA)

于 2020-11-06T20:52:36.380 回答