4

In the ConversionPattern in a log4net config, is there a way to specify the thread number, even if it has a name?

e.g. something like the following: [7] ThreadFoo

musaul
  • 2,341
  • 19
  • 26

2 Answers2

3

It doesn't look like it's supported.

Based on the the PatternLayout Class documentation listing all the output options here the %thread variable seems to wrap the behaviour you want to change.

You could possibly consider using Process ID instead? Depending on what your end objective is.

Have a look at this SO Answer, it would look like:

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

and config usage

<layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%property{pid}" />
</layout>
Community
  • 1
  • 1
Nick Josevski
  • 4,156
  • 3
  • 43
  • 63
  • I'm assuming you would need to use a `ThreadContext` and not `GlobalContext` because he's wanting thread level attributes. – Cole W Mar 05 '12 at 12:45
  • I actually just need the thread ID to to be always printed, even if the thread in question has a name. This is because I'm dealing with a class that creates a set of threads, and gives them names. When you have multiple instances of that class, you end up having multiple threads with the same name in the log. I think I'll go with a custom/extended PatternLayout class. Thanks! – musaul Mar 05 '12 at 14:30
0

you could add the thread id to the thread name:

Thread myThreadObj = new Thread(...);
myThreadObj.Name = "The thread name " + myThreadObj.ManagedThreadId;
Alex
  • 583
  • 5
  • 18