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 methodC – 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

Answers


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"...


Need Your Help

Any reason not to always log stack traces?

java logging stack-trace

Encountered a frustrating problem in our application today which came down to an ArrayIndexOutOfBounds exception being thrown. The exception's type was just about all that was logged which is fairly

Update textarea with jquery

jquery

Trying to get current data from textarea with jquery and replace it with what server returns. so far q value is picked up right, instead of updating it with server returned content, it just replace...