Question

I have an application with lots of logging of all levels. I wish to add some custom behavior to the Log.Fatal calls. Is that possible to do without building a custom Appender?

I tried this:

    log4net.Util.LogLog.LogReceived += LogLog_LogReceived;        

    private void LogLog_LogReceived(object source, LogReceivedEventArgs e)
    {

    }

But LogReceived doesn't seem to be fired. It's perhaps not intended to be used this way. Not sure.

Does anyone have an idea?

Was it helpful?

Solution

I realized my question is probably doable (some way), but not at all wise. Creating a custom appender is so easy that it would make anything related to my question silly to demand from the logging framework. Sorry that I asked explicitly how to do that WITHOUT appender. It's stupid.

I ended up adding this to my assembly:

namespace MyAssembly
{
    public class MyAppenderClass: AppenderSkeleton
    {
        protected override void Append(LoggingEvent loggingEvent)
        {
            // something, something, dark side.
        }
    }  
}

and adding this piece of config:

<root>
  <!-- Set the log level for the entire application here -->
  <level value="DEBUG" />
  <appender-ref ref="MyAppender" />
</root>    

<appender name="MyAppender" type="MyAssembly.MyAppenderClass">
  <!-- the threshold is to make sure it starts logging FATAL and up (so only FATAL really) -->
  <threshold value="FATAL" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="[%date - %message%newline%exception" />
  </layout>
</appender>

And that's it. Lovely log4net.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top