Sign up ×

For various reasons I'd like to output the processId in hexadecimal rather than in the default decimal format when outputing logs from NLog.

I'm using ${processid} in the layout, which outputs it in decimal. I know one can format dates using the format attribute, like ${shortdate:format=YYYY-mm-dd}. Is there anything similar for ints?

share|improve this question

1 Answer 1

I'm not aware of anything that will do that out of the box with NLog, but you could pretty easily write a LayoutRendererWrapper to do this. In your NLog.config file, you would wrap the field you want convert, something like this:


Here is a blog posting describing the feature:

You can find the source for all of the delivered LayoutRendererWrappers here:

The wrapper might look something like this (uncompiled and untested):

public sealed class HexLayoutRendererWrapper : WrapperLayoutRendererBase
  public HexLayoutRendererWrapper()
    this.Culture = CultureInfo.InvariantCulture;
  public CultureInfo Culture { get; set; }

  protected override string Transform(string text)        
    int x;
    if (Int32.TryParse(text, out x))
      string hex = string.Format("{0:X}", x);
      return hex;
      return text;        

You could even generalize and make a "formatting" wrapper that has a Format property that is then applied to the value before writing.

share|improve this answer

Your Answer


