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.

GONeale
  • 26,302
  • 21
  • 106
  • 149
  • Here you can find a way to create a named Logger instance [tip](http://kenegozi.com/blog/2009/12/11/windsorrsquos-logging-facility-getting-a-named-instance) – Johnny_D Apr 17 '12 at 11:27

1 Answers1

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • 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. – GONeale Nov 05 '09 at 02:23
  • Did you read the links in my answer? There's a lot you can filter on besides logger name. – Michael Petrotta Nov 05 '09 at 02:27
  • 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 around appenders was working earlier, should I be trying that to wrap this filtered appender in, instead of an actual named logger? – GONeale Nov 05 '09 at 02:30
  • Sorry, not 'wrapped' around that. with the 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.. – GONeale Nov 05 '09 at 02:32
  • Nope, you don't need access to the logger by name, unless you're using a filter that requires it. Many don't. – Michael Petrotta Nov 05 '09 at 02:34
  • Ok, well, I will take a look at filters. If I can get them to work through 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! – GONeale Nov 05 '09 at 02:39
  • @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? – Michael Petrotta Nov 05 '09 at 02:41
  • 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. – GONeale Nov 05 '09 at 07:37
  • 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. – GONeale Nov 05 '09 at 07:38
  • Still question is open, access logger by name, but not resolving situation by filters. – Johnny_D Apr 17 '12 at 11:09
  • log4net SDK link is bad – Stealth Rabbi Apr 29 '16 at 15:59