Loading...

log4net-user@logging.apache.org

[Prev] Thread [Next]  |  [Prev] Date [Next]

RE: Disable/enable appender for all loggers Radovan Raszka Wed Aug 08 06:06:26 2007

Great!
I tested and used first solution, because it seems better for me (file
path + name is independent on the program code).
Thanks for help
Radovan
________________________________

From: Vanderkolk, John [mailto:[EMAIL PROTECTED] 
Sent: Monday, August 06, 2007 10:16 PM
To: Log4NET User
Subject: RE: Disable/enable appender for all loggers



        That's because of how log4net configures itself. When the
repository finds a reference to a file appender in a logger that it is
creating, it will automatically ask its LockingModel to create that
file.  Even if your program has no intent on writing to that file
log4net will still create it as soon as it knows that you might write to
it. I managed to find two ways to accomplish what you want.

        

        1.

        

        Create a new locking model and use that as your locking model:

        

        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" />. Now when your
locking model is called to create the file, if the threshold is set to
OFF, it will refuse. This will cause an internal exception (no way
around it) but I see no reason to believe this is an invalid method.

        

        2.

        

        If in the config file you specify an empty string as the file
name (<file value=""/>), when log4net configures itself with that
appender it will throw an exception to itself and give up on creating
any file for that appender, but importantly, it WILL create the appender
anyway.  So if you do that then the first time that you want to log to
it you can specify a file like this:

        

        public void directAppenderToFile(string appender, string path)

        {

              log4net.Appender.IAppender[] appenders =
log4net.LogManager.GetRepository().GetAppenders();

        

              for (int i = 0; i < appenders.Length; ++i)

              {

                    log4net.Appender.FileAppender curAppender =
appenders[i] as log4net.Appender.FileAppender;

        

                    if (curAppender != null && curAppender.Name ==
appender)

                    {

                          curAppender.File = path;

                          curAppender.ActivateOptions();

                    }

              }

        }

        

        I personally prefer the first method, but it's up to you to
decide which one works best for you.

        

        Happy Logging,

        John VanderKolk 


        The contents of this e-mail are intended for the named addressee
only. It contains information that may be confidential. Unless you are
the named addressee or an authorized designee, you may not copy or use
it, or disclose it to anyone else. If you received it in error please
notify us immediately and then destroy it.
        

        From: Radovan Raszka [mailto:[EMAIL PROTECTED] 
        Sent: Friday, August 03, 2007 5:27 PM
        To: Log4NET User
        Subject: RE: Disable/enable appender for all loggers

        

        Thanks for proposal to John and Ron, it works well.

        But when I tried the reverse strategy (set Threshold to OFF in
config file and change to DEBUG when needed), I noticed that empty log
file (Service.log in my configuration) is created (file size = 0 bytes).

        Can this feature be disabled?