logging in log4net to different appenders based on circumstances
Asked Answered
U

1

9

I am using log4net and in one class require logging to a RollingFile appender, but then in another class, I wish to log to the event log + rolling file + console appender.

What is the best practice? and could I see some sample code?

By the way to make things more difficult, I am using Castle Windsor Logging Facility with Log4net to resolve my Logger instance.

If it helps, I was thinking this below, but have no idea if this is best practice, or how to activate a particular logger based on 'name' still utilising my current logger instance from windsor:

log4net.config:

...
    <logger name="EventLogOnly">
      <level value="ALL" />
      <appender-ref ref="EventLogAppender" />
    </logger>
    <logger name="ConsoleEventLog">
      <level value="ALL" />
      <appender-ref ref="ColoredConsoleAppender" />
      <appender-ref ref="EventLogAppender" />
    </logger>
...

castle windsor container builder class:

container.AddFacility("logging.facility", 
   new LoggingFacility(LoggerImplementation.Log4net, "log4net.config"));

class in which to log:

private ILogger Logger;
public Test(ILogger logger) {
  Logger.Info("Can I log under event log only?");
  Logger.Info("Now can I log under both?");
}

Thanks guys.

Uncial answered 5/11, 2009 at 1:59 Comment(1)
Here you can find a way to create a named Logger instance tipHallowed
R
13

You can do this by applying a filter to an appender. Only if the log event passes the filter does the event get logged by that appender.

This filter configuration will log only those events coming from the logger named "MyLogger":

<appender name="EventLogAppender" ...
    <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="MyLogger" />
    </filter>       
    <filter type="log4net.Filter.DenyAllFilter" />
</appender>

...and this one will match log messages with certain contained text:

<filter type="log4net.Filter.StringMatchFilter">
    <stringToMatch value="database" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

There's a good bit of configuration possible with filters. See the log4net SDK, or the Filters section of the manual, for more details.

Reece answered 5/11, 2009 at 2:18 Comment(11)
Thanks Michael, but I am still unsure of how to access my Logger by name. Using the logging facility in Castle Windsor, I don't instantiate a new instance by using LogManager.GetLogger("loggerName"), an instance is passed through for me.Uncial
Did you read the links in my answer? There's a lot you can filter on besides logger name.Reece
Sorry, yes I understand you are talking about filters, but to get started for this appender to work at all, I assumed I still needed to access the logger by name. How would I get the appender to be picked up? Using <root> around appenders was working earlier, should I be trying that to wrap this filtered appender in, instead of an actual named logger?Uncial
Sorry, not 'wrapped' around that. <root> with the <appender-ref> pointing to that EventLogAppender. However my problem will actually still exist, as I can't seem to name a logger "MyLogger". the log4net SDK won't help me, as this is a criteria I might need to specify through the logging facility..Uncial
Nope, you don't need access to the logger by name, unless you're using a filter that requires it. Many don't.Reece
Ok, well, I will take a look at filters. If I can get them to work through <root> without a specific logger name I might be ok. Thanks for your help. If you hear of how to access a logger instance by name with CW logging facility, let me know as I then could do it quite simply. Thnx Michael!Uncial
@GONeale: Again, you don't need the logger name to use log4net filters. I promise. Is there some other reason you need the logger name?Reece
Well, I didn't want to rely on including a special string in my log message to then filter upon to determine which appender it should write to. Seamless ability to send to different appenders through a Logger.Info() or whatever would have been nicer. That's all.Uncial
I guess if you could show me an example using the filter with my constructor featured in my test code above, it would help resolve my doubt to use a filter.Uncial
Still question is open, access logger by name, but not resolving situation by filters.Hallowed
log4net SDK link is badGrandniece

© 2022 - 2024 — McMap. All rights reserved.