Question/Suggestion for ${threadname}

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Question/Suggestion for ${threadname}

BillyM
I've been using log4net, but NLog seems MUCH easier to use and offers all of the functionality I need.  I'm impressed!

It took me days to really dial in log4net but only a few hours to get NLog setup and running the way I wanted.  Great examples of how to use NLog!


I do have one question regarding outputting the Thread Name in the log output that I couldn't find in the documentation:

In log4net, I can specify to have the Thread Name in the log output using a config like this:
<conversionPattern value="%date [%-8thread]  %message%newline" />

and I get a sample outlut like this: (simple file logging)

{ The Thread Names are in the [] }

2006-09-03 14:25:12,022 [Main    ]  Starting the Server
2006-09-03 14:25:12,366 [TCP#2   ]  Starting the TCP Remote Channels (3)
2006-09-03 14:25:12,366 [8040    ]  TCP Remote Channel-1 (Running)  IpEndPoint: 192.168.0.1:9000
2006-09-03 14:25:12,394 [1354    ]  TCP Remote Channel-2 (Running)  IpEndPoint: 192.168.0.2:9000
2006-09-03 14:25:12,462 [8040    ]  TCP Remote Channel-3 (Running)  IpEndPoint: 192.168.0.3:9000

Using NLog and an XML config target like this:
  <target name="file" xsi:type="File"
           layout="${longdate} [${threadname:padding=8:fixedLength=True}] ${message}"
           fileName="${basedir}/Logs/NLog.txt" />

I get:

2006-09-03 14:25:12.5022 [    Main]  Starting the Server
2006-09-03 14:25:12.5366 [   TCP#2]  Starting the TCP Remote Channels (3)
2006-09-03 14:25:12.5466 []  TCP Remote Channel-1 (Running)  IpEndPoint: 192.168.0.1:9000
2006-09-03 14:25:12.5466 []  TCP Remote Channel-2 (Running)  IpEndPoint: 192.168.0.2:9000
2006-09-03 14:25:12.5672 []  TCP Remote Channel-3 (Running)  IpEndPoint: 192.168.0.3:9000

The first two threads are Threads with Names assigned in code but the last three are Threads spawned from the .Net Thread Pool and do not seem to have any Names.

Is it possible to have NLog display the threadid instead if the thread name is null (or empty string)?  With log4net it's easy to see the threads that are not named (it shows some thread id of some sort instead) and keep track of which threads are sending the log message, but with NLog, the threads without names don't display anything (totally expected :-) and it also don't seem to honor the padding configuration for the ${threadname}.

(I'm not sure where log4net is getting the thread identifiers it uses for threads without names as it seems to be a much different number than NLog shows with 'threadid'.)


On a different note...  Thank you for providing programatic ways to use NLog!!  It's been driving me crazy trying to use log4net programatically as there are zero examples that I could find.  XML config files are great, but there are still many times when it's better or required to config the logging in code.

Very nice work on NLog....

BillyM
Reply | Threaded
Open this post in threaded view
|

Re: Question/Suggestion for ${threadname}

Jarek Kowalski
Administrator
The problem with null ${threadname} not being aligned properly has been fixed in SVN and you can download the latest snapshots which have this issue fixed.

Note that NLog.dll assembly version number has been changed in this release as part of the new versioning policy. The number now includes SVN revision so the DLL is 1.0.0.498 instead of 1.0.0.0 (You can override this in the NLog.version file if you want).
NLog Blog
Reply | Threaded
Open this post in threaded view
|

Re: Question/Suggestion for ${threadname}

BillyM
Jaroslaw,

Thanks for the quick reply.  :-)  I'll DL the newest build and give it try.