How to receive events if log4net stops logging

Log4net is too good at not throwing errors. I am trying to create some kind of handler that fires if log4net cannot start or dies and can no longer register.

I know the application settings key to enable log4net internal debugging (log4net.Internal.Debug). I don't need all the debug info all the time, though, if there is a problem with log4net.

Does anyone have a way by which they programmatically capture and handle errors in log4net?

+3
source share
4 answers

Well, log4net will be very hard for you to do this, as it (by design) suppresses the exceptions that are thrown during registration operations. This is because the production system should not fail due to an error while formatting the log message.

It is worth a try with very simple template layouts, because sometimes it is the use of% P {XYZ} elements in template layouts that cause problems if the corresponding properties are not set properly. If everything works as expected with a simple template template, you can add what you need one at a time and see if you can pinpoint the problem this way.

0
source

-, , , IErrorHandler appender, appender . , log4net.Internal.Debug.

, ( , Logger log4net, - , SMTP- ):

using System;

using log4net.Core;

namespace Test
{
    public class SmtpErrorHandler : IErrorHandler
    {
        public void Error(string message)
        {
            Logger.Log.Error(message);
        }

        public void Error(string message, Exception ex)
        {
            Logger.Log.Error(message, ex);
        }

        public void Error(string message, Exception ex, ErrorCode errorCode)
        {
            Logger.Log.Error(String.Format("{0}{1}Error code: {2}", message, Environment.NewLine, errorCode), ex);
        }
    }
}

appender (, ):

emailAppender.ErrorHandler = new SmtpErrorHandler();
+4

, Logger.Log.Error... - , , Windows.

When analyzing errors, the user must send us not only the log files, but also the event log. We created a tool that automatically exports event logs and compresses them together with all available Log4net log files into a zip file.

0
source

If you are using a monitoring tool such as SCOM (System Center Operations Manager), you can configure it to monitor log files and notify that no changes have been made for more than n hours / days.

0
source

Source: https://habr.com/ru/post/1714516/


All Articles