开发者

How to associate unique threadID per execution with log4net output?

I'm using log4net, and when i log messages my threadID will be duplicated inside my file.. i want it to be a uniqueID whereby it never exists twice in the file so that it will make it easy for searching. I hope this is coming out right. basically my conversion pattern includes these items

  <conversionPattern value="%date [%hex_thread] %method %-5level %logger – %message%newline"/>

And this is my output:

2010-07-08 11:12:32,426 [B] methodC INFO methodC – inside method C

2010-07-08 11:12:58,316 [B] methodC INFO met开发者_C百科hodC – inside method C

2010-07-08 11:13:09,284 [A] methodC INFO methodC – inside method C

2010-07-08 11:15:04,214 [B] methodC INFO methodC – inside method C

where hex_thread is my threadID in hexadecimal. You can see that [B] occurred several times and it shouldn't since the execution time is different. So, I want to make it also unique. so that its something that doesn't occur any where in that line. like maybe "0b87007" something like this which if I search it will be the only occurrence in that whole line. How can I achieve this?

Edited To add My Fix

My threadId is printed in hex format so i just appending several characters to the output like this....

long id = System.Threading.Thread.CurrentThread.GetHashCode(); String str = id.ToString("X"); writer.Write("0x0000"+str);

Then the output would look like this

2010-07-09 10:07:37,917 [0x00004] methodC INFO methodC – message

2010-07-09 10:07:37,917 [0x0000B] methodC INFO methodC – message


As long as you are using the same thread you will have the same thread id. You would need to create a new thread for every operation and assign it a unique id yourself.

From your question it almost seems that you want to have a unique id for every line in your logfile. Not sure why you want to do that but if that is the goal you should not use the thread id but rather some "global counter"...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜