Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have configured log4net in my app successfully but one thing is a little bit annoying for me.

The log file is created (empty) after my app start even if no error occurs. I would like to log file be created only after some error.

like image 479
binball Avatar asked Mar 28 '10 15:03

binball


1 Answers

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.

like image 90
Loathian Avatar answered Oct 04 '22 04:10

Loathian