2

We use log4net for our current application logging but I don't like the way our logging framework is built around it currently. For example, it currently has a single point of failure, if for whatever reason it can't connect to the logging server then you get no logging. I'd like to build it as a flexible but simple framework that would allow me to specify one or more 'backup' appenders to use in the event that the primary one fails (for example, if logging server is down, write to a file instead).

Have any of you stumbled across anything like this that you liked and found well written? I don't mind rolling my own, but I'm a big fan of trying not to reinvent the wheel

thanks

snappymcsnap
  • 2,050
  • 2
  • 29
  • 53
  • NLog allows multiple log targets to be specified. Doesn't log4net allow that too? – Eric J. Apr 05 '12 at 19:22
  • I think what the OP wants is have multiple log targets but only using the alternative log target when the primary one is not available. – aKzenT Apr 05 '12 at 19:35
  • Possible duplicate: http://stackoverflow.com/questions/3244623/how-to-configure-log4net-for-fallback – aKzenT Apr 05 '12 at 19:36

3 Answers3

5

The combination of Seq and Serilog supports this: the logger buffers all events to a set of rolling files, then asychronously reads those files and sends them to the server using HTTP/S.

Config looks like:

var log = new LoggerConfiguration()
    .WriteTo.Seq("http://my-seq-server", bufferBaseFilename: "C:\\Logs\\myapp")
    .CreateLogger();

I work on both projects- keen to get some feedback on how this approach fits the bill (if you're lucky enough to be able to switch to Serilog ;))

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
2

There isn't anything already built that I have come across. I would suggest you make your own appender that uses the existing appenders but adds in the property for what to do when failure happens. You can use the AppenderSkeleton as the base class for your new appender and then from there call the append functions of the appenders you want to use.

So when log4 uses your logger it will be something like this (NOTE: This is not complete, but should give you a starting point):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net.Appender;
using log4net.Core;

namespace logger {
    class FailoverAppender : AppenderSkeleton {

        protected override void Append(LoggingEvent loggingEvent) {
            // Send the log message to the web service.
            try {
                FirstAppender.Append(loggingEvent);
            }
            catch (Exception e) {
                try{
                    SecondAppender.Append(loggingEvent);
                }
                catch (Exception e2){
                    ErrorHandler.Error("An error occurred while connecting to the logging service.", e);
                }
            }
        }
    }
}

I hope this helps you get started, maybe if you get it complete you can post the solution back here as well.

Kevin Green
  • 1,137
  • 11
  • 21
  • thanks Kevin, that's a nice approach...I actually wrote something semi-similar to that against the log4j package but that was probably 5-6 years ago and I can't find the damn code unfortunately. I'll definitely post back here if I find something or just wind up writing it from scratch, cheers – snappymcsnap Apr 05 '12 at 21:15
0

I actually decided to go with NLog instead because log4net is such a pain to setup. NLog is easier to use, easier to setup and the codebase is kept up to date (log4net is all but abandoned)

snappymcsnap
  • 2,050
  • 2
  • 29
  • 53