1

I configure the output format in appsettings.json, but it does not work:

{
  "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)
);

The output is:

[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... 

But, if I configure the format in code, it will work:

var builder = Host.CreateDefaultBuilder(args);

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

The output is:

{"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"}}

I use .NET 7, installed packages:

<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" />

How do I fix this issue, Thanks!

WENJUN CHI
  • 507
  • 3
  • 11
  • If you look at the serilog examples (many around) you will notice there is an extension that reads the data from the appsettings and you need to call this in your code. I believe it is called something like ReadFromConig(). – AliK Apr 21 '23 at 03:39
  • Sorry, I forget to paste the read configure file code, actually, I read the configure file. – WENJUN CHI Apr 21 '23 at 05:27

3 Answers3

1

program.cs

builder.Services.AddSwaggerGen();

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

var app = builder.Build();

Action

        [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>

output
enter image description here

Qiang Fu
  • 1,401
  • 1
  • 2
  • 8
  • I use the JsonFormatter not CompactJsonFormatter, so installing Serilog.Formatting.Compact is useless. – WENJUN CHI Apr 21 '23 at 08:19
  • 1
    @WENJUNCHI Yes,you are right. It is not necessary. I ocassionally have the same issue which solved by install that. I update my answer as a working sample you can reference. – Qiang Fu Apr 21 '23 at 09:14
  • 1
    I see, formatter cannot use with outputTemplate, I remove the outputTemplate from configure file, it works! – WENJUN CHI Apr 21 '23 at 09:22
1

The formatter cannot use with outputTemplate, remove the outputTemplate from configuration, and it works!

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
WENJUN CHI
  • 507
  • 3
  • 11
0

my definition: i think you forget to initialize 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 is static:

    Log.Information("i am logging some info");
Frenchy
  • 16,386
  • 3
  • 16
  • 39