logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nicko Cadell" <ni...@neoworks.com>
Subject RE: How to enforce certain things in log messages while still giving the flexibility to add more things
Date Wed, 24 Aug 2005 11:58:31 GMT
You can create your own layout that outputs the fields you always want
to see in the log. For example:

using System.IO;
using log4net.Core;
using log4net.DateFormatter;
using log4net.Layout;

public class FixedLayout : LayoutSkeleton
{
  private PatternLayout m_nestedLayout;
  private Iso8601DateFormatter m_dateFormatter;

  public override void ActivateOptions()
  {
    m_dateFormatter = new Iso8601DateFormatter();
  }

  public override void Format(TextWriter writer, LoggingEvent
loggingEvent)
  {
    m_dateFormatter.FormatDate(loggingEvent.TimeStamp, writer);
    writer.Write(' ');
    writer.Write(loggingEvent.Domain);
    writer.Write(' ');
    writer.Write(loggingEvent.LocationInformation.ClassName);
    writer.Write(' ');
    writer.Write(loggingEvent.LocationInformation.MethodName);
    writer.Write(' ');
    writer.Write(loggingEvent.LocationInformation.LineNumber);
    writer.Write(' ');
    writer.Write(loggingEvent.ThreadName);
    writer.Write(' ');
    writer.Write(loggingEvent.RenderedMessage);
    writer.Write(' ');

    if (m_nestedLayout != null)
    {
      writer.Write(' ');
      m_nestedLayout.Format(writer, loggingEvent);
    }

    writer.WriteLine();
  }

  public PatternLayout PatternLayout 
  {
    get { return m_nestedLayout; }
    set { m_nestedLayout = value; }
  }
}

Lets say you build this class in to an assembly called
MyLayoutAssembly.dll.
You will also note that the above code supports a nested PatternLayout
in the PatternLayout property. This is used to log additional patterns.

An example configuration is:

<appender name="ConsoleAppenderWithFixedLayout"
type="log4net.Appender.ConsoleAppender">

  <layout type="FixedLayout, MyLayoutAssembly">
    <patternLayout value="[%-5level %logger]" />
  </layout>

</appender>

You may need to adjust the layout's type attribute to the location of
your custom layout.

Also I should point out that it would still be possible for someone
editing the logging config file to remove your custom layout and replace
it with another one, or even remove the whole appender.

Cheers,
Nicko

> -----Original Message-----
> From: Shireesh Thanneru [mailto:thanneru@yahoo.com] 
> Sent: 24 August 2005 05:49
> To: Log4NET User
> Subject: How to enforce certain things in log messages while 
> still giving the flexibility to add more things
> 
> I have kind of an unusual requirement here...let me explain 
> it. While we want the extreme flexibility that Log4Net 
> provides, we also want to make sure certain things always get 
> logged with every log message. Basically, with each log 
> message, we want to make sure certain things always get 
> logged, for example: Date, Time, App Name, Class Name, Method 
> Name, Line Number and ThreadId along with the actual log message. 
> 
> So, while we want to use the Pattern Layout that Log4Net 
> provides which can be used to log the above information (and 
> lot of other additional info), any one can change config file 
> and remove these elements from it resulting in a log file 
> that won't contain these parameters making the support 
> people's job a nightmare. So, we are kind of looking for a 
> middle of the road solution, where certain things always get 
> logged (mentioned above) and developers or end users can 
> enable more things in the PatternLayout if they want more 
> things in the log file but they shouldn't be able to remove 
> any of the enforced parameters.
> 
> Thanks in advance.
> 
> Regards,
> 
> Shireesh Thanneru
> 
> 

Mime
View raw message