如何在不使用布局的情况下获取自定义目标中的 ThreadId?
How to get the ThreadId in a custom target without using layout?
我确实创建了一个自定义数据库目标来登录到我的数据库。这是一个非常简单的目标,它只覆盖 Write
方法以在我的数据库中创建一个日志条目。
这很好用,所有信息都由 LogEventInfo
提供。
但现在我想记录 threadId。据我所知,NLog 有布局参数 ${threadid}
可以这样做。但我不想使用布局渲染器写入我的数据库。
我的问题
是否可以向 LogEventInfo
询问 threadId
?如果是:如何?如果不是:NLog 从哪里得到这个 id?
您可以使用 threadId 布局创建 属性(首选)或变量并渲染那个。
例如
Layout threadIdLayout = "${threadid}"; //for performance, as static property on the custom class is recommend
var threadId = threadIdLayout.Render(logEvent);
我确实创建了一个自定义数据库目标来登录到我的数据库。这是一个非常简单的目标,它只覆盖 Write
方法以在我的数据库中创建一个日志条目。
这很好用,所有信息都由 LogEventInfo
提供。
但现在我想记录 threadId。据我所知,NLog 有布局参数 ${threadid}
可以这样做。但我不想使用布局渲染器写入我的数据库。
我的问题
是否可以向 LogEventInfo
询问 threadId
?如果是:如何?如果不是:NLog 从哪里得到这个 id?
您可以使用 threadId 布局创建 属性(首选)或变量并渲染那个。
例如
Layout threadIdLayout = "${threadid}"; //for performance, as static property on the custom class is recommend
var threadId = threadIdLayout.Render(logEvent);