我当前的工作项目要求所有配置文件都采用 JSON 格式,包括我的 config.log 文件,这意味着我不允许将自动生成的 .xml 与 nlog.config nuget 包一起使用。我查看了 github 上的 nlog 文档,但它们对我没有帮助,因为我对 nlog 或一般的日志记录还很陌生。
最佳答案
这是 NLog.Extensions.Logging >= 1.5.0 引入的新功能
下面的示例展示了如何从 appsettings.json 加载 NLog 配置。
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));
有关更多详细信息,请查看 https://github.com/NLog/NLog.Extensions.Logging/wiki/Json-NLog-Config 中的文档
关于c# - 如何设置 JSON 文件来配置 nlogging,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47122753/