How can I get Log4net to crash silently so that I can implement FailoverAppender?

Log4j has an application called FailoverAppender. This appender allows you to select the primary appender and as many secondary applications as possible. If the primary appender is not registered, the secondary login and login. I noticed that Log4Net doesn't have this type of application. I know Log4Net can log in in parallel, but I would prefer the secondary appender to be activated and logged only if the primary appender fails. What prevents me from doing this is that Log4Net is a crash logging system. Is there a way to force an exception to be thrown from the application on registration failure so that I can implement FailoverAppender?... I think I need to override some public ErrorHandler that implements IErrorHandler. I tried to configure the ForwardingAppender to do what I want, but since every appender connected fails, I cannot get it to work. Any help would be greatly appreciated. If there are examples, please point me to them.

+3


source to share


3 answers


So, after reading stuartd's answer, I did a bit more research on implementing IErrorHandler. During this research, I came across FallbackAppender . Now I feel a bit silly that I found this. I just thought I'd put this answer so others can find FallbackAppender much faster. Thanks peer and stuartd. I appreciate the answers.



+1


source


With SkeletonAppender, you can write your own transitional attachment. If logging fails, remove the current appender and add a fallback appender, for example:



class FailoverAppender : AppenderSkeleton {

    protected override void Append(LoggingEvent loggingEvent) {
        try {
            //Add the appender implementation
        }
        catch (Exception e) {
            try{
                //Remove the current appender and add an other, then append the message to the new appender
                var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
                var attachable = root as IAppenderAttachable;
                attachable.RemoveAppender(this);

                AppenderOnError appender = new AppenderOnError(); //Your backup appender
                attachable.AddAppender(appender);
                appender.Append(loggingEvent);

            }
            catch (Exception e2){
                ErrorHandler.Error("An error occurred while connecting to the logging service.", e);
            }
        }
    }
}

      

+2


source


Here's an example from the log4net mailing list on how to implement an IErrorHandler:

log4net uses a framework of error handling plugins, each with an ErrorHandler property that contains an IErrorHandler object . This is used to handle errors reported by Appender. The error is the default OnlyOnceErrorHandler used , and this writes the first (per application only) to the console.

If you want to handle errors in your own way, all you have to do is create your own implementation of the IErrorHandler interface, and then set the ErrorHandler property in appenders to use this new type, for example:

<appender ...>
     ...
     <errorHandler type="MyErrorHandler, MyAssembly"/>
</appender>

      

Error handlers can only be installed in applications, and there is no way to globally override the default error handler for all applications.

Then your error handler can do a failure registration.

0


source







All Articles