如何在log4net appender中包含线程号,即使线程有名称



在log4net配置中的ConversionPattern中,有没有一种方法可以指定线程号,即使它有名称?

例如以下内容:[7] ThreadFoo

它看起来不受支持。

根据PatternLayoutClass文档列出的所有输出选项,%thread变量似乎包装了您想要更改的行为。

您是否可以考虑使用进程ID?取决于你的最终目标。

看看这个SO答案,它看起来像:

log4net.GlobalContext.Properties["pid"] = Process.GetCurrentProcess().Id;

和配置使用

<layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%property{pid}" />
</layout>

您可以将线程id添加到线程名称中:

Thread myThreadObj = new Thread(...);
myThreadObj.Name = "The thread name " + myThreadObj.ManagedThreadId;

最新更新