Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent log4net configuration

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?

like image 310
Bealer Avatar asked Jan 06 '10 13:01

Bealer


2 Answers

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;
    }
like image 171
Bealer Avatar answered Nov 08 '22 08:11

Bealer


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,

like image 1
kgiannakakis Avatar answered Nov 08 '22 09:11

kgiannakakis