0

Hi guys firstly excuse me for my bad english, ill try my very best to be as accurate as possible.

I am using log4net 3.5 in .NET Framework 4.5.

The Problem I am actually facing is a very common. log4net is logging everything twice. It isn't configured with a xml file, i am using a static Class to do this.

/// <summary>
/// Diese Klasse konfiguriert die Logs bei ausführungszeit
/// </summary>
public static class LoggerSetup
{
    static LoggerSetup()
    {
        _fileDirectory = @"Log\";
        _archiveInterval = 7;
        Setup();
    }

    /// <summary>
    /// Der Ort wo das Logfile gespeichert wird
    /// </summary>
    public static string FileDirectory
    {
        get
        {
            return _fileDirectory;
        }
        set
        {
            _fileDirectory = value;
            Setup();

        }
    }
    private static string _fileDirectory;

    /// <summary>
    /// Wieviele Tage die Logfiles im Speicher bleiben
    /// </summary>
    public static int ArchiveInterval
    {
        get
        {
            return _archiveInterval;
        }
        set
        {
            if (value <= 0)
                value = 1;
            _archiveInterval = value;
            Setup();
        }
    }
    private static int _archiveInterval;



    private static void Setup()
    {
        // Here the hierarchy object is taken
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

        // Pattern Layout defined
        PatternLayout patternLayout = new PatternLayout();
        patternLayout.ConversionPattern = "%date [%-5level] %message%newline";
        patternLayout.ActivateOptions();

        // configurating the RollingFileAppender object
        RollingFileAppender roller = new RollingFileAppender();
        roller.AppendToFile = true;
        roller.File = FileDirectory + "Log.log";
        roller.StaticLogFileName = true;
        roller.PreserveLogFileNameExtension = true;
        roller.LockingModel = new FileAppender.MinimalLock();
        roller.Layout = patternLayout;
        roller.MaxSizeRollBackups = ArchiveInterval;
        roller.RollingStyle = RollingFileAppender.RollingMode.Date;
        roller.DatePattern = "_yyyy-MM-dd";    
        roller.ActivateOptions();

        // Here it is added only once to the Root
        hierarchy.Root.AddAppender(roller);

        hierarchy.Root.Level = Level.Info;
        hierarchy.Configured = true;
    }
}

I have already been looking up some other pages, such as

In those Questions i have found, the thread there was alaways double adding an appender, mostly they have been added to a root and to a logger object.

As you can see in the code above I am doing it only once... Any Ideas? the Setup() method is called everytime a Property is updated, from nowhere else.

I am thankful to any answer i get.

Community
  • 1
  • 1
LuckyLikey
  • 3,504
  • 1
  • 31
  • 54

1 Answers1

0

I actually solved it myself... the problem was that there's alaways been a new instance of Rolling file appender added to the root everytime Setup() has been executed..

I solved it that way:

    private static RollingFileAppender _roller;
    private static PatternLayout _patternLayout;

    static LoggerSetup()
    {
        // root config
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
        hierarchy.Root.Level = Level.Info;
        hierarchy.Configured = true;

        // appender vorbereiten
        _roller = new RollingFileAppender();
        _patternLayout = new PatternLayout();
        _roller.Layout = _patternLayout;


        hierarchy.Root.AddAppender(_roller);

        _fileDirectory = @"Log\";
        _archiveInterval = 7;
        Setup();
    }


    public static void Setup()
    {
        _roller.AppendToFile = true;
        _roller.File = FileDirectory + "Log.log";
        _roller.StaticLogFileName = true;
        _roller.PreserveLogFileNameExtension = true;
        _roller.LockingModel = new FileAppender.MinimalLock();
        _roller.MaxSizeRollBackups = ArchiveInterval;
        _roller.RollingStyle = RollingFileAppender.RollingMode.Date;
        _roller.DatePattern = "_yyyy-MM-dd";

        _patternLayout.ConversionPattern = "%date [%-5level] %message%newline";
        _patternLayout.ActivateOptions();

        _roller.ActivateOptions();

    }

Setup() can now be executed as many times as you want, it won't double the appender anymore.

LuckyLikey
  • 3,504
  • 1
  • 31
  • 54