使用appsettings.json配置Serilog RollingFile [英] Configuring Serilog RollingFile with appsettings.json

查看:640
本文介绍了使用appsettings.json配置Serilog RollingFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为.NET Core项目配置Serilog。这是我的 appsettings.json 中的内容:

I'm trying to configure Serilog for a .NET Core project. Here's what I have in my appsettings.json:

 "Serilog": 
{
    "MinimumLevel": "Verbose",
    "Enrich": ["FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId"],
    "WriteTo": [
      { "Name": "RollingFile",
        "Args": {
          "pathFormat": "C:/Logfiles/testapp/log-{Date}.json",
          "textFormatter": "JsonFormatter",
          "fileSizeLimitBytes": 2147483648,
          "retainedFileCountLimit": 5
        }
      }
    ]
  }

我看到的问题是 JsonFormatter 不会被拾取,而是使用默认的文本格式器获取条目。我尝试使用 formatter: JsonFormatter ,但是得到了相同的结果。

The problem I see is that JsonFormatter is not picked up, and instead I get entries using the default text formatter. I tried using "formatter": "JsonFormatter", but got the same result.

如果我在代码中配置Serilog,则一切正常:

It all works fine if I configure Serilog in code:

var jsonSink = new RollingFileSink(config["Logger:FilePath"], new JsonFormatter(), 2147483648, 5);

var logger = new Serilog.LoggerConfiguration().WriteTo.Sink(jsonSink);

这是我的 project.json

"Serilog": "2.2.1",
"Serilog.Extensions.Logging": "1.1.0",
"Serilog.Sinks.Literate": "2.0.0",
"Serilog.Sinks.Seq": "2.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Serilog.Enrichers.Thread": "2.0.0",
"Serilog.Enrichers.Process": "2.0.0",
"Serilog.Sinks.ColoredConsole": "2.0.0",
"Serilog.Settings.Configuration": "2.2.0"

任何有关如何使其工作的指针吗?

Any pointers on how to get this working?

谢谢!

推荐答案

格式化程序参数必须是完全限定的类型名称。尝试:

The formatter argument needs to be a fully-qualified type name. Try:

"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"

这篇关于使用appsettings.json配置Serilog RollingFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆