tags:

views:

34

answers:

2

hey all

I'm using log4net for logging and my conversion pattern includes threadId in my output, but its only highest 2 digits and i am asked to convert it to hexadecimal but so far i found code C# to convert decimal numbers to hex but how can i convert my threadID to hex and make it appear in hex format. I'm very new to this not even sure where to look, anybody help please?

here is the code in the xml file

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

and [%thread] gives me a number like 10 or 7 or 8, but i need it to be hex format maybe like 0x887df9 so what should i do???

A: 
threadId.ToString("X");
Jacob Seleznev
i need the change in the xml file, because that is where my configuration is.
Precious
+1  A: 

You could write a converter like this:

public sealed class HexPatternConverter : PatternLayoutConverter
{       
    override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        long id;
        if (long.TryParse(loggingEvent.ThreadName, out id))
        {
            writer.Write(id.ToString("X"));
        }
        else
        {
            writer.Write(loggingEvent.ThreadName);
        }
    }
}

then you configure the layout like this:

<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="[%hex_thread] %message%newline" />
  <converter>
    <name value="hex_thread" />
    <type value="YourNamespace.HexPatternConverter" />
  </converter>
</layout>

Obviously you can use this converter in your pattern as you see fit and you will also need to tweak the converter so that it prints the hex value exactly as you want it.

Stefan Egli
i did like this and this is my output[X] inside method D[X] inside method D[X] inside method D where "inside method D" is my message, but the thread ID became only [X]. what am i doing wrong?
Precious
did you see my edit? I accidentally just wrote "X" instead of converting the long to hex format
Stefan Egli
Yes Thanks Alot... it works perfectly.. many thnx
Precious