有什么方法可以从正确构建的 LoggerConfiguration 实例中获取有效的 Serilog json 配置(字符串或文件)?
Is there any way to get a valid Serilog json configuration (string or file) from a properly built LoggerConfiguration instance?
上下文,以及我到目前为止所做的尝试...
作为一项政策,我们的应用程序从 appsetting.json 配置日志记录。
我正在使用以下代码:
// configuration variable is properly built with ConfigurationBuilder
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
当我尝试向配置添加新内容(丰富、解构、过滤等)时,总是很难做到,因为大多数示例、入门和文档都使用流畅的 API LoggerConfiguration
class.
我可以轻松复制这些示例 + 使用 IDE 智能感知来实现我的目标,但不幸的是我必须编写一个有效的 json 配置文件,这并不总是那么简单。
我的想法是,在 POC 项目中创建配置运行时,然后以某种方式序列化构建的 LoggerConfiguration
实例,这样我就会有一个示例 serilog json 配置文件。
不幸的是,我找不到 ReadFrom.Configuration(...)
操作的逆运算。
我还尝试使用以下代码 (using System.Text.Json
):
简单地序列化构建的 LoggerConfiguration
var loggerConfigurarion = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.WithThreadName()
.Enrich.WithProperty(ThreadNameEnricher.ThreadNamePropertyName, "Unknown");
var myHope = JsonSerializer.Serialize(loggerConfigurarion, new JsonSerializerOptions {WriteIndented = true});
但这导致一个主要是空的 json(这也是一个谜题,怎么样)
{
"WriteTo": {},
"AuditTo": {},
"MinimumLevel": {},
"Enrich": {},
"Filter": {},
"Destructure": {},
"ReadFrom": {}
}
问题
所以问题仍然存在:有没有办法从 LoggerConfiguration 实例中获取有效的 Serilog json 配置(字符串或文件)?
Update:有一个名为 SerilogAnalyzer 的社区项目可以从 C# Serilog 管道生成 appSettings.json
和 app.config
的内容。太酷了!
Serilog 中没有内置任何东西可以让你这样做,而且你不能简单地序列化 LogConfiguration
以按照你的想法发送文本,因为大多数配置不存储在简单变量中,而是存储在创建管道期间创建的 lambda 函数中。
无论哪种方式,您都必须求助于使用反射来检查任何已配置的接收器及其属性。你可以在这个答案上看到一个相关的例子:
Unit test Serilog configuration
也就是说,我个人认为使用流利的 API 是可行的方法,而不是使用 JSON 配置(或 XML app.config),因为迟早你会想要做一些接收器 asynchronous, and/or use a predicate or a function to configure certain aspects of the pipeline (for example, to configure a sub-logger),并且无论如何都很难(在某些情况下不可能)在 JSON/XML 配置中进行配置。
您可以使用 options pattern to configure the small parts that you think you might want to change via configuration (such as URLs, logging levels, etc), and the rest of it, use the fluent API combined with .
上下文,以及我到目前为止所做的尝试...
作为一项政策,我们的应用程序从 appsetting.json 配置日志记录。 我正在使用以下代码:
// configuration variable is properly built with ConfigurationBuilder
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
当我尝试向配置添加新内容(丰富、解构、过滤等)时,总是很难做到,因为大多数示例、入门和文档都使用流畅的 API LoggerConfiguration
class.
我可以轻松复制这些示例 + 使用 IDE 智能感知来实现我的目标,但不幸的是我必须编写一个有效的 json 配置文件,这并不总是那么简单。
我的想法是,在 POC 项目中创建配置运行时,然后以某种方式序列化构建的 LoggerConfiguration
实例,这样我就会有一个示例 serilog json 配置文件。
不幸的是,我找不到 ReadFrom.Configuration(...)
操作的逆运算。
我还尝试使用以下代码 (using System.Text.Json
):
LoggerConfiguration
var loggerConfigurarion = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.WithThreadName()
.Enrich.WithProperty(ThreadNameEnricher.ThreadNamePropertyName, "Unknown");
var myHope = JsonSerializer.Serialize(loggerConfigurarion, new JsonSerializerOptions {WriteIndented = true});
但这导致一个主要是空的 json(这也是一个谜题,怎么样)
{
"WriteTo": {},
"AuditTo": {},
"MinimumLevel": {},
"Enrich": {},
"Filter": {},
"Destructure": {},
"ReadFrom": {}
}
问题
所以问题仍然存在:有没有办法从 LoggerConfiguration 实例中获取有效的 Serilog json 配置(字符串或文件)?
Update:有一个名为 SerilogAnalyzer 的社区项目可以从 C# Serilog 管道生成 appSettings.json
和 app.config
的内容。太酷了!
Serilog 中没有内置任何东西可以让你这样做,而且你不能简单地序列化 LogConfiguration
以按照你的想法发送文本,因为大多数配置不存储在简单变量中,而是存储在创建管道期间创建的 lambda 函数中。
无论哪种方式,您都必须求助于使用反射来检查任何已配置的接收器及其属性。你可以在这个答案上看到一个相关的例子:
Unit test Serilog configuration
也就是说,我个人认为使用流利的 API 是可行的方法,而不是使用 JSON 配置(或 XML app.config),因为迟早你会想要做一些接收器 asynchronous, and/or use a predicate or a function to configure certain aspects of the pipeline (for example, to configure a sub-logger),并且无论如何都很难(在某些情况下不可能)在 JSON/XML 配置中进行配置。
您可以使用 options pattern to configure the small parts that you think you might want to change via configuration (such as URLs, logging levels, etc), and the rest of it, use the fluent API combined with