2

Log4net fails silently by design. Good. I don't want it taking down my app.

Is there a way to set up a notification when Log4net crashes and stops logging?

Is there some kind of event handler that I can hook into which will tell me that Log4net has gone silent?

I want to know when this happens so I can recycle the app pool as soon as possible.

Thanks!

onefootswill
  • 3,707
  • 6
  • 47
  • 101
  • 1
    No, you would have to implement this yourself with some kind of heartbeat logging. Personally I have never seen log4net crash in many years of using it - I've seen it fail to start and I've seen it drop a database connection (which is why there's [ReconnectOnError](https://logging.apache.org/log4net/release/sdk/log4net.Appender.AdoNetAppender.ReconnectOnError.html)), but never fail as such. – stuartd Jun 16 '15 at 09:00
  • @stuartd Thanks for the info. I was not even aware of ReconnectOnError. – onefootswill Jun 17 '15 at 00:30

1 Answers1

2

If I'm understanding you correctly, you would like log4net to signal you when an appender fails. If we look at the logging code for the Logger implementation we see that the only point that takes into account the appenders failing is the internal logging mechanism:

// log4net.Repository.Hierarchy.Logger
public virtual void Log(Type callerStackBoundaryDeclaringType, Level level, object message, Exception exception)
{
    try
    {
        if (this.IsEnabledFor(level))
        {
            this.ForcedLog((callerStackBoundaryDeclaringType != null) ? callerStackBoundaryDeclaringType : Logger.ThisDeclaringType, level, message, exception);
        }
    }
    catch (Exception exception2)
    {
        LogLog.Error("Log: Exception while logging", exception2);
    }
    catch
    {
        LogLog.Error("Log: Exception while logging");
    }
}

Of course this occurs only if the appender throws an exception. The LogLog component then forwards the message to the Console and Trace components:

// log4net.Util.LogLog
private static void EmitErrorLine(string message)
{
    try
    {
        Console.Error.WriteLine(message);
        Trace.WriteLine(message);
    }
    catch
    {
    }
}

So, by listening messages coming from the trace you can get an idea of what happens in your appenders. To turn this into an event you can add a trace listener that triggers in some specific cases: you can take a look at what is in this answer regarding custom trace listeners in order to trigger an event from one.

Community
  • 1
  • 1
samy
  • 14,832
  • 2
  • 54
  • 82
  • Thanks for that. We see Log4net cease logging with reasonable regularity. We haven't yet identified whether it is confined to the ADO appender. That should be step1, as we have recently got a file appender going too. Your trace suggestion looks like a good idea, which we will look into. Thanks. – onefootswill Jun 17 '15 at 00:29