【问题标题】:How to get the ThreadId in a custom target without using layout?如何在不使用布局的情况下获取自定义目标中的 ThreadId?
【发布时间】:2018-04-09 19:58:16
【问题描述】:

我确实创建了一个 custom-db-target 来登录到我的数据库。这是一个非常简单的目标,它只覆盖Write 方法来在我的数据库中创建一个日志条目。

这很好用,所有信息都由LogEventInfo 提供。

但现在我想记录 threadId。据我所知,NLog 有布局参数${threadid} 这样做。但我不想使用布局渲染器写入我的数据库。

我的问题

是否可以向LogEventInfo 询问threadId?如果是:如何?如果不是:NLog 从哪里得到这个 id?

【问题讨论】:

    标签: c# nlog


    【解决方案1】:

    您可以使用 threadId 布局创建属性(首选)或变量并呈现该布局。

    例如

    Layout threadIdLayout = "${threadid}"; //for performance, as static property on the custom class is recommend
    
    var threadId = threadIdLayout.Render(logEvent);
    

    【讨论】:

    • 完全正确。我想我想复杂并试图避免渲染太多:D
    猜你喜欢
    • 1970-01-01
    • 2020-04-18
    • 2012-02-13
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多