gpt4 book ai didi

.net - 什么是 Serilog 解构?

转载 作者:行者123 更新时间:2023-12-01 09:18:48 36 4
gpt4 key购买 nike

Serilog的@的目的是什么?句法?

如果我运行以下命令:

var dummy = new { Foo = "Bar", Date = DateTime.Now };

Log.Information("Dummy object: {Dummy}", dummy);

然后我得到一个输出到控制台,如下所示:
Time: 16:20 [Level: Information] (ManagedThreadID: 8) Message: Dummy object: "Foo = Bar, Date = 25/06/2016 16:20:30 }"

如果我更改 {Dummy}{@Dummy}然后我得到相同的输出
Time: 16:22 [Level: Information] (ManagedThreadID: 8) Message: Dummy object:  Foo: "Bar", Date: 06/25/2016 16:22:28 }

那么,什么是 @应该做的?

最佳答案

仔细观察,你会发现它不是同一个输出。
@ Dummy 前面的操作符告诉 Serilog 序列化传入的对象,而不是使用 ToString() 转换它,这就是您的第一个示例中不使用 @ 时发生的情况运算符(operator)。

第一个示例中的日志事件将以如下属性结束(此处为 JSON):

{
"Dummy": "{ Foo = Bar, Date = 25/06/2016 16:20:30 }"
}

使用 {@Dummy} 将导致参数被序列化为结构化数据:
{
"Dummy":
{
"Foo": "Bar",
"Date": "25/06/2016 16:20:30"
}
}

Comment from Nicholas Blumhardt (Serilog 的创建者):

Where appropriate, using the @ operator is much more useful for manipulation/analysis.

The reason for this "opt-in" requirement is that most types in .NET programs convert nicely into strings, but aren't cleanly/meaningfully serializable. By opting in to serialization with @ you're saying: "I know what I'm doing, serialize this object!" :)

关于.net - 什么是 Serilog 解构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38029980/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com