tags:

views:

50

answers:

1

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

+1  A: 

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

Stefan Egli
Thanks Stefan. Sorry I asked wrongly, I don't want to have unique threadID per every line. Right now all I want to do is to make my threadID unique which is different then the rest of the message in that line. For example, if threadID is "A" (using hex) then when I search this, it will also include any occurrence of the letter "A" in my message. This is what I want to prevent. How do I assign unique ID myself?
Precious
when you search for [A] then you will only find any reference to the thread id... btw. the [] in your pattern string are not required for the "hex_thread" property to work you could also remove them or replace them with something else
Stefan Egli
It was requested by my boss to add [] to the threadID, that's why I enclosed it in []. I have figured a way to do it.I posted my own fix appended to my question.. after giving it a long thought i came up with very simple solution. Thanks for your time Stefan.
Precious