Question

Anyone know if there's a fluent way of configuring log4net (appenders and all the properties for appenders etc...).

The xml is driving me crazy.

Or if not, does anyone know of a decent .Net logging framework that can easily be fluently configured and offer similar features to log4net?

Was it helpful?

Solution

Cool cheers I'll take a look. Found what I was looking for in log4net too.

Annoyingly we've started using Castle Logging Facility which only seems to take a string to an xml file! So may have to consider doing it all via DSL and generating our xml configs as a pre build step.

    private static void ConfigureLog()
    {
        var root = ((Hierarchy)LogManager.GetRepository()).Root;
        root.AddAppender(GetConsoleAppender());
        root.AddAppender(GetFileAppender(@"d:\dev\huddle\log\Huddle.Sync", "standard.log", Level.Debug));
        root.AddAppender(GetFileAppender(@"d:\dev\huddle\log\Huddle.Sync", "error.log", Level.Warn));
        root.Repository.Configured = true;
    }

    private static FileAppender GetFileAppender(string directory, string fileName, Level threshold)
    {
        var appender = new FileAppender
        {
            Name = "File", 
            AppendToFile = true,
            File = directory + "\\" + fileName, 
            Layout = new PatternLayout(_pattern), 
            Threshold = threshold
        };

        appender.ActivateOptions();
        return appender;
    }

    private static ConsoleAppender GetConsoleAppender()
    {
        var appender = new ConsoleAppender
        {
            Name = "Console", 
            Layout = new PatternLayout(_pattern), 
            Threshold = Level.Debug
        };

        appender.ActivateOptions();
        return appender;
    }

OTHER TIPS

If you don't mind the dependency on Microsoft's Enterprise library, you could use the Logging Application Block. Configuration is still in XML, but you can edit through a graphical interface directly in Visual Studio,

I feel your pain with the XML-based log4net configuration. I got frustrated enough with debugging it a while ago that I started putting together my own fluent API for log4net. Check out fluent-log4net over on github. Not everything is 100% supported yet, but it's getting there.

It does make testing a viewing the configuration at a glance a lot easier, I will say.

I have since moved to NLog for this very reason

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