You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Nicko Cadell <ni...@neoworks.com> on 2004/07/30 20:01:47 UTC

RE: how to include % conversion pattern tokens

Dan,

The only way to change the patterns recognised by the PatternLayout is
to actually change the PatternLayout and recompile the log4net assembly.
You can write your own layout to do whatever you require and use that
instead of the PatternLayout but that means that you may need to
duplicate some of its functionality to produce the rest of the message.

The above is correct for the latest downloadable release, however it
should be a lot easier to do what you want with the current CVS version
of log4net. If you want to proceed I recommend that you checkout the
latest version from CVS and build a new version of the log4net assembly.

The CVS version supports adding custom patterns to a PatternLayout in
the config file, e.g.:

<layout type="log4net.Layout.PatternLayout">

  <converter>
    <name value="wibble" />
    <type value="TestConsoleApp.WibblePatternConverter, TestConsoleApp"
/>
  </converter>

  <conversionPattern value="%wibble %date [%thread] %-5level %logger
[%global{MachineName}] [%ndc] &lt;%mdc{auth}&gt; - %message%newline" />

</layout>

The new PatternLayout also supports multi-char pattern names.
The code for the WibblePatternConverter is:

public class WibblePatternConverter :
log4net.Layout.Pattern.PatternLayoutConverter 
{
  override protected void Convert(System.IO.TextWriter writer,
log4net.Core.LoggingEvent loggingEvent)
  {
    writer.Write("WIBBLE");
  }
}

While this isn't a generally useful pattern converter it does
demonstrate the minimum required to make it work ;)

The CVS version also supports global properties that can be included
using the %global pattern, e.g. %global{hostIP}. This is useful for
static information that you want to include in all log messages. You
would set the 'hostIP' global property as follows:

String hostIP =
Dns.Resolve(Dns.GetHostName()).AddressList[0].ToString();
log4net.GlobalContext.Properties["hostIP"] = hostIP;

Hope this is useful info,

Nicko



> -----Original Message-----
> From: Dan Vanderboom [mailto:dvanderboom@panatrack.com] 
> Sent: 30 June 2004 16:41
> To: log4net-user@logging.apache.org
> Subject: how to include % conversion pattern tokens
> 
> What is the simplest way to add %tokens to the conversion 
> patterns?  For example, I want to include the stack trace of 
> the exception object and the IP or MAC address of the 
> originating device with each log.Error() call.  Do I have to 
> jump through a lot of hoops, or is there an easy way?
> 
>