代码之家  ›  专栏  ›  技术社区  ›  justSteve

SeriLog未输出非字符串数据

  •  2
  • justSteve  · 技术社区  · 7 年前

    我刚刚开始在Core 2 web应用程序中使用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
    

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

    1 回复  |  直到 7 年前
        1
  •  2
  •   orhtej2    7 年前

    BeginScope 将元数据添加到与包含的消息相关联的作用域对象。

    您必须修改 outputTemplate 查看传递的值,根据 discussion in SeriLog's issues list :

    .WriteTo.LiterateConsole(outputTemplate:
        "{Timestamp:o} [{Level:u3}] {Scope} {Message}{NewLine}{Exception}")
    

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

    仅仅因为你实际上可以将任何东西传递给BeginScope(),并不意味着你一定要这样做。提供者获取最有用的信息是很重要的,因此本文提出,但作为一种诊断帮助,我强烈倾向于明智地使用字典范围值,而不是分层范围名称。

    结构化键/值属性是自文档化的,更易于查询。OrderId=54这样的日志过滤器很容易公式化。Scope数组没有给层次结构中的每个级别附加任何意义:Scope[?]='下载消息可能会检索到一组有意义的事件,但范围[0]=42只是胡说八道。

    当然,灵活性是存在的,因此您应该以对您最有意义的方式使用API:-)。