logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: Correct point to add a custom property?
Date Sat, 05 Jan 2008 06:30:41 GMT
Properties are the right place for additional items. Here's an example that wraps ILog and
add the overloads you need:

public class LogItemLog : LogImpl
{
 private readonly static Type declaringType = typeof(LogItemLog);

 public LogItemLog(ILogger logger) : base(logger)
 {
  // empty
 }

 public void Warn(Guid guid, string message)
 { 
  LoggingEvent loggingEvent = new LoggingEvent(
   declaringType, 
   Logger.Repository, 
   Logger.Name, 
   Level.Warn, 
   message);

  loggingEvent.Properties["ID"] = guid;

  Logger.Log(loggingEvent);
 }

 public void Warn(LogItem logItem)
 { 
  // ...
 }
}

Then make a thin wrapper around LogManager to return your custom object:

public static class LogItemLogManager
{
    public static LogItemLog GetLogger(Type type)
    {
        return new LogItemLog(LogManager.GetLogger(type).Logger);
    }
}

----- Original Message ----
From: Dave Brotherstone <davebrotherstone@gmail.com>
To: log4net-user@logging.apache.org
Sent: Thursday, January 3, 2008 7:10:40 AM
Subject: Correct point to add a custom property?

Hi,
   We're trying to integrate log4net into our current application
framework.  We need to log an ID property against each log entry,
retrieved from the object we're trying to log.  This ID will
eventually go in a separate column in the database we're logging to.
To keep things simple, I'm currently using the FileAppender to test
things.

Effectively we want to do :-

_logger.Warn(new LogItem(someGuid, someMessage));

And have a pattern layout (or database appender) to do something like
 :-

<layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger
[[%property{ID}]] - %message%newline" />
</layout>


At the moment, I have this working by creating a new FileAppender,
inherting from FileAppender, and overriding the Append method with

protected override void Append(log4net.Core.LoggingEvent loggingEvent)
 {
            loggingEvent.Properties["ID"] =
((LogItem)loggingEvent.MessageObject).ID;
            base.Append(loggingEvent);
 }


Are properties the right thing to use for this, or is there another
way? And if they are, is this the correct place to set them?
In reality, the LogItem object already exists when we want to log it,
so I'd rather not have to set the property before the _logger.Warn
line.

I have searched through the archives, but haven't been able to find
anything doing the same thing, perhaps I'm searching for the wrong
thing though!

Many, many thanks,

Dave.




Mime
View raw message