Thanks very much! That’s very helpful. It’s more or less what I was going to
do, except starting from the Ext.Trace TraceLogManager instead. I wasn’t sure
which methods were truly necessary, so you just saved me a ton of time! It’s
working beautifully. I do wish they hadn’t made LogManager sealed—if they’d
made WrapperCreationHandler virtual that would make this much more extensible.
Anyway, thank you very much your help!
Julie
P.S. In case anyone stumbles upon this trying to do the same thing I’m doing, I
should point out how to use the loggingEvent.Properties. In your ILog
implementation, your methods would look something like this:
override public void Debug(object message, Exception exception)
{
if (IsDebugEnabled) {
LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType,
Logger.Repository, Logger.Name, Level.Debug, message, exception);
loggingEvent.Properties["ID"] = “foo”;
Logger.Log(loggingEvent);
}
}
Then in log4net.config, modify the PatternLayout conversion pattern to include
your new property with %P{property_name} or %property{property_name}, e.g.
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] [%property{ID}] %-5level %logger -
%message%newline" />
</layout>
From: Rob Prouse [mailto:[email protected]]
Sent: Thursday, August 19, 2010 2:42 PM
To: 'Log4NET User'
Subject: RE: Using my own implementation of ILog
If that is the case, then I would suggest the following.
1. Download the log4net source as a guide,
2. Derive an ILoggerWrapper class from LogImpl class that adds your
properties
3. Copy the code for LogManager into your own LogManager class.
Unfortunately the wrapper map is private, so you can’t extend L
4. You should only need to change the WrapperCreationHandler method to
return your class derived from LogImpl.
5. Change your log creation in your code to use your new LogManager.
Should be about an hours work. Hope this helps,
Rob
From: Sheffield, Julie [mailto:[email protected]]
Sent: Thursday, August 19, 2010 2:26 PM
To: Log4NET User
Subject: RE: Using my own implementation of ILog
The data changes per request. I’d rather not have the caller be responsible
for adding this info to the logs—we need to make sure it’s always there and I
thought adding it to the log method was the best way to do that.
I could always write a wrapper around the log and pass properties to the
underlying implementation, but that doesn’t feel as clean to me. It would mean
calling logManager.getLogger on every log request, and even though I know it’s
a singleton, that still feels like too much overhead. Especially if the log
level is turned down.
Since it uses a nice factory pattern, I had hoped that log4net might use
dependency injection for its concrete class. But I can’t find it documented
anywhere, so I guess not. Oh, well.
Thanks for your help with it!
Julie
From: Rob Prouse [mailto:[email protected]]
Sent: Thursday, August 19, 2010 12:56 PM
To: 'Log4NET User'
Subject: RE: Using my own implementation of ILog
Extension methods can’t override existing methods, so you would need to
create/use new methods on ILog that chain to the underlying methods. For
example DebugWithProperties(…) -> Debug()
Do these properties change much? Is there a reason you can’t set them on a
global or thread context before the log calls and have them logged out that way?
Rob
From: Sheffield, Julie [mailto:[email protected]]
Sent: Thursday, August 19, 2010 12:47 PM
To: Log4NET User
Subject: RE: Using my own implementation of ILog
I have some custom properties I want to stick in LoggingEvent.Properties. Can
I do that with extension methods?
From: Rob Prouse [mailto:[email protected]]
Sent: Thursday, August 19, 2010 12:38 PM
To: 'Log4NET User'
Subject: RE: Using my own implementation of ILog
Before you go to too much work, what do you hope to achieve with your custom
ILog? Can you achieve what you want another way like writing or extending an
appender, or possibly with extension methods?
From: Sheffield, Julie [mailto:[email protected]]
Sent: Thursday, August 19, 2010 12:20 PM
To: [email protected]
Subject: Using my own implementation of ILog
I see examples for extending the ILog interface that involve a custom version
of the LogManager. However, is it possible simply to provide my own
implementation of the standard ILog interface and configure the LogManager to
return it? If so, how? I can certainly follow the examples and write my own
LogManager, but I’d hate to do so unnecessarily.
Thanks very much!
Julie