使用beginScope作为JSOM的nlog格式

发布于 2025-02-03 02:01:23 字数 2093 浏览 3 评论 0 原文

使用 ilogger.beginscope ,如何使NLOG将NLOG格式化为JSON?看起来@符号被忽略,或者只是没有编码以使用开始范围? ref 文档

我已经尝试了所有我可以尝试的组合想一想,它们都导致对象为 toString 'd,而不是转换为JSON。最终结果是数据无法作为“结构化”数据搜索,因为它只是一个字符串。

第一次尝试:

using (_logger.BeginScope(new Dictionary<string, object>
{
    ["CacheValue"] = value
}))
{
    _logger.LogInfo("howdy");
}

seq中的结果:

howdy
CacheValue MyApp.ViewModels.EntityIssueQueueGetModel

第二尝试:

using (_logger.BeginScope(new Dictionary<string, object>
{
    ["@CacheValue"] = value
}))
{
    _logger.LogInfo("howdy");
}

seq中的结果:

howdy
@CacheValue MyApp.ViewModels.EntityIssueQueueGetModel

第三尝试:

using (_logger.BeginScope(new Dictionary<string, object>
{
    ["{@CacheValue}"] = value
}))
{
    _logger.LogInfo("howdy");
}

在SEQ中结果:

howdy
{@CacheValue} MyApp.ViewModels.EntityIssueQueueGetModel

第四尝试:

using (_logger.BeginScope("{@CacheValue}", value))
{
    _logger.LogInfo("howdy");
}

seq中的结果:

howdy
@CacheValue MyApp.ViewModels.EntityIssueQueueGetModel

下一个位,但不是我所需要的。这与友好的信息一起打印出JSON。我只希望JSON数据与日志关联以进行查询目的;未作为消息的一部分显示。

工作示例:

_logger.LogInfo("howdy. {@CacheValue}", value);

seq中的结果:

howdy. {"Issues":[],"TotalRecords":0,"Success":true,"TraceId":"00-3b8ef0c2d84714e0c81a07cbb5d50444-8269922e21923478-00","Errors":[]}
CacheValue {
  "Issues": [],
  "TotalRecords": 0,
  "Success": true,
  "TraceId": "00-3b8ef0c2d84714e0c81a07cbb5d50444-8269922e21923478-00",
  "Errors": []
}

How do you get NLog to format data as JSON when using ILogger.BeginScope? It appears the the @ symbol is ignored, or just not coded to work with begin scope? ref documentation

I've tried every combination I can think of and they all result in the object being ToString'd rather than converted to JSON. The end result is that the data is not searchable as "structured" data because it's just a string.

1st Attempt:

using (_logger.BeginScope(new Dictionary<string, object>
{
    ["CacheValue"] = value
}))
{
    _logger.LogInfo("howdy");
}

Result in Seq:

howdy
CacheValue MyApp.ViewModels.EntityIssueQueueGetModel

2nd Attempt:

using (_logger.BeginScope(new Dictionary<string, object>
{
    ["@CacheValue"] = value
}))
{
    _logger.LogInfo("howdy");
}

Result in Seq:

howdy
@CacheValue MyApp.ViewModels.EntityIssueQueueGetModel

3rd Attempt:

using (_logger.BeginScope(new Dictionary<string, object>
{
    ["{@CacheValue}"] = value
}))
{
    _logger.LogInfo("howdy");
}

Result in Seq:

howdy
{@CacheValue} MyApp.ViewModels.EntityIssueQueueGetModel

4th Attempt:

using (_logger.BeginScope("{@CacheValue}", value))
{
    _logger.LogInfo("howdy");
}

Result in Seq:

howdy
@CacheValue MyApp.ViewModels.EntityIssueQueueGetModel

This next bit works, but is not what I need. This prints the JSON out along with the friendly message. I only want the JSON data to be associated with the log for querying purposes; not shown as part of the message.

Working example:

_logger.LogInfo("howdy. {@CacheValue}", value);

Result in Seq:

howdy. {"Issues":[],"TotalRecords":0,"Success":true,"TraceId":"00-3b8ef0c2d84714e0c81a07cbb5d50444-8269922e21923478-00","Errors":[]}
CacheValue {
  "Issues": [],
  "TotalRecords": 0,
  "Success": true,
  "TraceId": "00-3b8ef0c2d84714e0c81a07cbb5d50444-8269922e21923478-00",
  "Errors": []
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2025-02-10 02:01:23

nlog无法识别范围内下文范围内的消息 - 网板词法。

nlog jsonlayout可以格式化范围 - 上下文属性类似:

  <layout xsi:type="JsonLayout" includeScopeProperties="true" maxRecursionLimit="1">
  </layout>

另请参阅: https://github.com/ nlog/nlog/wiki/jsonlayout

也可以将单个示波器 - 属性属性格式化为JSON,

  layout="${scopeproperty:CacheValue:format=@}"

也可以: https://github.com/nlog/nlog/nlog/nlog/wiki/scopeproperty-layout-renderer

也有可能将整个Scope-nested-stack as Gremat AS格式化为JSON类似:

  layout="${scopenested:format=@}"

另请参阅: https://github.com/nlog/nlog/nlog/nlog/nlog/nlog /wiki/scopenested-layout-Renderer

也许可以考虑在

NLog doesn't recognize message-template-syntax for Scope-Context-Properties.

NLog JsonLayout can format Scope-context Properties like this:

  <layout xsi:type="JsonLayout" includeScopeProperties="true" maxRecursionLimit="1">
  </layout>

See also: https://github.com/NLog/NLog/wiki/JsonLayout

It is also possible to format a single Scope-context Property as Json like this:

  layout="${scopeproperty:CacheValue:format=@}"

See also: https://github.com/NLog/NLog/wiki/ScopeProperty-Layout-Renderer

It is also possible to format the entire Scope-Nested-Stack as Json like this:

  layout="${scopenested:format=@}"

See also: https://github.com/NLog/NLog/wiki/ScopeNested-Layout-Renderer

Maybe consider posting your question at https://github.com/datalust/nlog-targets-seq

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文