如何禁用空日志文件对应用程序的创建开始? [英] How to disable creation of empty log file on app start?

查看:109
本文介绍了如何禁用空日志文件对应用程序的创建开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在成功的应用程序配置log4net的,但有一件事是我一点点讨厌。

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.

推荐答案

其实,我找到了一种方法,在这个线程要做到这一点:

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

<一个href=\"http://www.l4ndash.com/Log4NetMailArchive/tabid/70/forumid/1/postid/18271/view/topic/Default.aspx\">http://www.l4ndash.com/Log4NetMailArchive/tabid/70/forumid/1/postid/18271/view/topic/Default.aspx

我测试过的第一个方法和它的作品。万一该链接是不再好,我会在这里重现code。基本上,笔者指出,这样做有两种方式。

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.

第一种方式:

创建一个新的锁定模式,只获得一个锁(并创建该文件),如果该记录器的合适的阈值的工作原理。

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" />

和确保你设置这个新LockingModel为你的模型:

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.

这篇关于如何禁用空日志文件对应用程序的创建开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆