简体   繁体   中英

Formatting Serilog logs

How do I remove unnecessary information in Serilog logs? The docs seem to have nothing about this.

我想去掉红色部分

I want to remove the red part in the image above

This is my config in appsettings.json

  "Serilog": {
"Using": [ "Serilog.Settings.Configuration" ],
"Filter": [
  {
    "Name": "ByIncludingOnly",
    "Args": {
      "expression": "@Level in ['Debug']"
    }
  }
],
"MinimumLevel": {
  "Default": "Debug",
  "Override": {
    "System": "Debug",
    "Microsoft": "Debug"
  }
},
"WriteTo": [
  {
    "Name": "Seq",
    "Args": {
      "serverUrl": "http://localhost:5341/",
      "compact": true
    }
  },
  {
    "Name": "Console"
  },
  {
    "Name": "File",
    "Args": {
      "path": "D:\\dev.SmartCity.Peafowls.Logs\\PeafowlsLog_.txt",
      "rollingInterval": "Day",
      "rollOnFileSizeLimit": true,
      "fileSizeLimitBytes": 4194304
    }
  }
] }

Its not completly clear what you want. I can only assume, that you want to display only the Log-Message to the console.

To achieve that, you can add the following outputTemplate to your block:

{
    "Name": "Console",
    "Args": {
      "outputTemplate":  "{Message}{NewLine}"
    }
}

The problem was that i had this line in the startup class when initializing the logger. .WriteTo.Providers(providers) This is my configuration now:

   private void ConfigureLogging(ServiceProvider services, LoggerProviderCollection providers)
    {
        var configuration = services.GetRequiredService<IConfiguration>();

        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .Destructure.ByTransforming<ExpandoObject>(e => new Dictionary<string, object>(e))
            .Enrich.WithProperty("Source", "Peafowls")
             #if DEBUG || STAGE
            .Enrich.WithProperty("Environment", _env)
             #endif
            .CreateLogger();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM