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

前端 未结 7 805
生来不讨喜
生来不讨喜 2020-11-30 07:04

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 o

7条回答
  •  渐次进展
    2020-11-30 07:37

    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:

    
    

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

    
    

    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.

提交回复
热议问题