How to disable creation of empty log file on app start?

I actually found a way to do this in this thread:

http://www.l4ndash.com/Log4NetMailArchive/tabid/70/forumid/1/postid/18271/view/topic/Default.aspx

I’ve tested the first method and it works. Just in case that link is not longer good I’ll reproduce the code here. Basically the author states that there are two ways of doing this.

First way:

Create a new locking model that only acquires a lock (and creates the file) if the appropriate threshold for that logger works.

public class MyLock : log4net.Appender.FileAppender.MinimalLock
{
      public override Stream AcquireLock()
      {
            if (CurrentAppender.Threshold == log4net.Core.Level.Off)
                  return null;

            return base.AcquireLock();
      }
}

Now in the config file, set the threshold to start out as:

<threshold value="OFF" />

and make sure you set this new LockingModel as you model:

<lockingModel type="Namespace.MyLock" />

I’m using this with a rolling file appender.

The second method is listed at the link. I haven’t tried this technique but it seems to be technically sound.

Leave a Comment