SeriLog 不输出非字符串数据

SeriLog not outputing non-string data

我刚刚开始在 Core 2 网络应用程序中使用 SeriLog,并将示例代码粘贴到我的控制器中:

    _logger.LogInformation("Before");

    using (_logger.BeginScope("Some name"))
    using (_logger.BeginScope(42))
    using (_logger.BeginScope("Formatted {WithValue}", 12345))
    using (_logger.BeginScope(new Dictionary<string, object> { ["ViaDictionary"] = 100 }))
    {
        _logger.LogInformation("Hello from the Index!");
        _logger.LogDebug("Hello is done");
    }

    _logger.LogInformation("After");

但输出排除了非平面文本的行:

2017-11-01 14:53:19.587 -05:00 [Information] Before
2017-11-01 14:53:19.588 -05:00 [Information] Hello from the Index!
2017-11-01 14:53:19.588 -05:00 [Debug] Hello is done
2017-11-01 14:53:19.588 -05:00 [Information] After

一方面这有点道理,但另一方面 - 为什么将它包含在回购示例代码中?

BeginScope 将元数据添加到与包含的消息关联的范围对象。

您必须根据 discussion in SeriLog's issues list:

修改 outputTemplate 以查看传递的值
.WriteTo.LiterateConsole(outputTemplate:
    "{Timestamp:o} [{Level:u3}] {Scope} {Message}{NewLine}{Exception}")

此外,请注意 how Scope is affected by the values passed to BeginScope

上的备注

Just because you can pass practically anything to BeginScope() doesn’t mean that you necessarily should. It’s important for a provider to capture the most useful information it can, hence this article, but as a diagnostic aid I’m strongly biased towards the judicious use of Dictionary scope values, rather than hierarchical scope names.

Structured key/value properties are self-documenting and simpler to query. A log filter like OrderId = 54 is easy to formulate. The Scope array doesn’t attach any meaning to each level in the hierarchy: Scope[?] = 'Downloading messages' might retrieve a meaningful set of events, but Scope[0] = 42 is just nonsense.

Of course, the flexibility is there, so you should use the API in the manner that makes the most sense to you :-).