Serilog.Sinks.Console 格式化程序配置不起作用

问题描述 投票:0回答:3

我在appsettings.json里配置了输出格式,但是不行:

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
          "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] [{SourceContext:l}] {Message:lj} {NewLine}{Exception}"
        }
      }
    ]
  }
}
var builder = Host.CreateDefaultBuilder(args);

builder.ConfigureLogging(logBuilder =>
{
    logBuilder.ClearProviders();
});
builder.UseSerilog((hostingContext, loggerConfiguration) =>
    loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
);

输出为:

[2023-04-21 10:50:33 INF] [Microsoft.Hosting.Lifetime] Application started. Press Ctrl+C to shut down. 
[2023-04-21 10:50:33 INF] [Microsoft.Hosting.Lifetime] Hosting environment: Local 
[2023-04-21 10:50:33 INF] [Microsoft.Hosting.Lifetime] Content root path: C:\Users\chiwenjun\Documents\Sources\emoney_com\mqproxy\src\MQProxy.Host 
[2023-04-21 10:50:34 INF] [Microsoft.Hosting.Lifetime] Application is shutting down... 

但是,如果我在代码中配置格式,它将起作用:

var builder = Host.CreateDefaultBuilder(args);

builder.ConfigureLogging(logBuilder =>
{
    logBuilder.ClearProviders();
});
builder.UseSerilog((hostingContext, loggerConfiguration) =>
    {
        loggerConfiguration.WriteTo.Console(new JsonFormatter());
    }
);

输出为:

{"Timestamp":"2023-04-21T10:52:13.9108077+08:00","Level":"Information","MessageTemplate":"Application started. Press Ctrl+C to shut down.","Properties":{"SourceContext":"Microsoft.Hosting.Lifetime"}}
{"Timestamp":"2023-04-21T10:52:13.9110602+08:00","Level":"Information","MessageTemplate":"Hosting environment: {EnvName}","Properties":{"EnvName":"Local","SourceContext":"Microsoft.Hosting.Lifetime"}}
{"Timestamp":"2023-04-21T10:52:13.9111968+08:00","Level":"Information","MessageTemplate":"Content root path: {ContentRoot}","Properties":{"ContentRoot":"C:\\Users\\chiwenjun\\Documents\\Sources\\emoney_com\\mqproxy\\src\\MQProxy
.Host","SourceContext":"Microsoft.Hosting.Lifetime"}}
{"Timestamp":"2023-04-21T10:52:15.9303377+08:00","Level":"Information","MessageTemplate":"Application is shutting down...","Properties":{"SourceContext":"Microsoft.Hosting.Lifetime"}}

我使用.NET 7,安装包:

<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Formatting.Elasticsearch" Version="9.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Http" Version="8.0.0" />

我该如何解决这个问题,谢谢!

c# asp.net-core .net-core serilog serilog-sinks-console
3个回答
0
投票

我的定义:我想你忘了初始化

Log.Logger

    var builder = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                                            .AddJsonFile("appsettings.json")
                                            .AddJsonFile(jsonfile);
    
    
    var config = builder.Build();

    Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(config)
                                          .CreateLogger();

日志是静态的:

    Log.Information("i am logging some info");

0
投票

程序.cs

builder.Services.AddSwaggerGen();

var configuration = new ConfigurationBuilder()
      .AddJsonFile("appsettings.json")
      .Build();
Log.Logger = new LoggerConfiguration()
        .ReadFrom.Configuration(configuration)
        .CreateLogger();

var app = builder.Build();

动作

        [HttpGet("test")]
        public void test()
        {
            Log.Information("hello");
            Log.Information("bye");
        }

appsettings.json

  "Serilog": {
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
        }
      }
    ]
  }

csproj

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Serilog" Version="2.12.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

</Project>

输出


0
投票

formatter不能与outputTemplate一起使用,从配置中删除outputTemplate,它可以工作!

© www.soinside.com 2019 - 2024. All rights reserved.