You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by wijitha <wi...@gmail.com> on 2008/12/05 16:46:38 UTC

How to use separate config file for log4net

Hi all

I want to use log4net with my project.
here is the simple example i tried.

App.config :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
       <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    
  </configSections>

  <log4net>
     A1 is set to be a ConsoleAppender 
    <appender name="A1" type="log4net.Appender.ConsoleAppender">

       A1 uses PatternLayout 
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-4timestamp [%thread] %-5level %logger
%ndc - %message%newline" />
      </layout>
    </appender>

     Set root logger level to DEBUG and its only appender to A1 
    <root>
      <level value="DEBUG" />
      <appender-ref ref="A1" />
    </root>
  </log4net>
  </configuration>

here is the code:

        private static readonly ILog log =
LogManager.GetLogger(typeof(Program));
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();
            log.Info("Entering application.");           
            log.Debug("Exiting application.");
            Console.Write("This Log4Net example");
        }

This thing is working well.
And what i want to do is put the log4net configuration part in a separate
file call Log4Net.config

if any one know how to do this please reply me. It is better if u can
separately give me the two configuration files. (App.config and
Log4Net.config)

thanks a lot 
wijitha

-- 
View this message in context: http://www.nabble.com/How-to-use-separate-config-file-for-log4net-tp20856712p20856712.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


Re: How to use separate config file for log4net

Posted by Michael Schall <mi...@gmail.com>.
You don't need anything in your app.config.  Just use
the log4net.Config.XmlConfigurator.ConfigureAndWatch method instead as pass
it a file with your log4net definitions.
Log4Net.config:
<log4net>
    ...
    <root>
        ...
    </root>
</log4net>

This will also allow you to change logging settings while the program is
running.

Mike

On Fri, Dec 5, 2008 at 9:46 AM, wijitha <wi...@gmail.com> wrote:

>
> Hi all
>
> I want to use log4net with my project.
> here is the simple example i tried.
>
> App.config :
>
> <?xml version="1.0" encoding="utf-8" ?>
> <configuration>
>    <configSections>
>       <section name="log4net"
> type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
>
>  </configSections>
>
>  <log4net>
>     A1 is set to be a ConsoleAppender
>    <appender name="A1" type="log4net.Appender.ConsoleAppender">
>
>       A1 uses PatternLayout
>      <layout type="log4net.Layout.PatternLayout">
>        <conversionPattern value="%-4timestamp [%thread] %-5level %logger
> %ndc - %message%newline" />
>      </layout>
>    </appender>
>
>     Set root logger level to DEBUG and its only appender to A1
>    <root>
>      <level value="DEBUG" />
>      <appender-ref ref="A1" />
>    </root>
>  </log4net>
>  </configuration>
>
> here is the code:
>
>        private static readonly ILog log =
> LogManager.GetLogger(typeof(Program));
>        static void Main(string[] args)
>        {
>            XmlConfigurator.Configure();
>            log.Info("Entering application.");
>            log.Debug("Exiting application.");
>            Console.Write("This Log4Net example");
>        }
>
> This thing is working well.
> And what i want to do is put the log4net configuration part in a separate
> file call Log4Net.config
>
> if any one know how to do this please reply me. It is better if u can
> separately give me the two configuration files. (App.config and
> Log4Net.config)
>
> thanks a lot
> wijitha
>
> --
> View this message in context:
> http://www.nabble.com/How-to-use-separate-config-file-for-log4net-tp20856712p20856712.html
> Sent from the Log4net - Users mailing list archive at Nabble.com.
>
>

RE: re: How to use separate config file for log4net

Posted by John Sinnott <js...@sftcon.com>.
At the program or service entry point.

Without a hard path, you will want it in the bin/debug and bin/release
folder for your dev environment.  
There is the chicken and egg issue of providing a path for it in the config
file - do you want to record config problems, etc.?

-----Original Message-----
From: wijitha [mailto:wijithapaw@gmail.com] 
Sent: Saturday, December 06, 2008 7:29 AM
To: log4net-user@logging.apache.org
Subject: Re: re: How to use separate config file for log4net


Hi
First, thanks for replying me.

now that is working.
but now i wanna know, Should this piece of code be written  in constructor
of each class i write.

      FileInfo log4NetConfigFile = new FileInfo("log4net.config");

  if (log4NetConfigFile == null || !log4NetConfigFile.Exists) throw new
Exception("unable to find log file");
           
log4net.Config.XmlConfigurator.ConfigureAndWatch(log4NetConfigFile);

thank you
wijitha



jsinnott@sftcon.com wrote:
> 
> 
> 
>  // initialize the log4net config file 
> 
>  // the system expects to find it in the same folder as the exe file = 
> /bin/debug 
> 
>  FileInfo  log4NetConfigFile = new  FileInfo ( "MyCustomConfigFile.config"

> );
> 
> if (log4NetConfigFile == null || ! log4NetConfigFile.Exists) throw new  
> Exception ( "unable to find log4net log file" );
> 
>  XmlConfigurator .Configure(log4NetConfigFile);
> 
>   
> 
> ----------------------------------------
> 
> From: "wijitha" <wi...@gmail.com>
> Sent: Friday, December 05, 2008 10:14 AM
> To: log4net-user@logging.apache.org
> Subject: How to use separate config file for log4net 
> 
> Hi all
> 
> I want to use log4net with my project.
> here is the simple example i tried.
> 
> App.config :
> 
> type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
> 
> A1 is set to be a ConsoleAppender 
> 
> A1 uses PatternLayout 
> 
> Set root logger level to DEBUG and its only appender to A1 
> 
> here is the code:
> 
> private static readonly ILog log =
> LogManager.GetLogger(typeof(Program));
> static void Main(string[] args)
> {
> XmlConfigurator.Configure();
> log.Info("Entering application."); 
> log.Debug("Exiting application.");
> Console.Write("This Log4Net example");
> }
> 
> This thing is working well.
> And what i want to do is put the log4net configuration part in a separate
> file call Log4Net.config
> 
> if any one know how to do this please reply me. It is better if u can
> separately give me the two configuration files. (App.config and
> Log4Net.config)
> 
> thanks a lot 
> wijitha
> 
> -- 
> View this message in context: 
>
http://www.nabble.com/How-to-use-separate-config-file-for-log4net-tp20856712
> p20856712.html
> Sent from the Log4net - Users mailing list archive at Nabble.com.
> 
>  
> 
> 

-- 
View this message in context:
http://www.nabble.com/How-to-use-separate-config-file-for-log4net-tp20856712
p20870124.html
Sent from the Log4net - Users mailing list archive at Nabble.com.




Re: How to use separate config file for log4net

Posted by Michael Schall <mi...@gmail.com>.
No, it is only required once for the process.

Sent from my iPod

On Dec 6, 2008, at 7:28 AM, wijitha <wi...@gmail.com> wrote:

>
> Hi
> First, thanks for replying me.
>
> now that is working.
> but now i wanna know, Should this piece of code be written  in  
> constructor
> of each class i write.
>
>      FileInfo log4NetConfigFile = new FileInfo("log4net.config");
>
>  if (log4NetConfigFile == null || !log4NetConfigFile.Exists) throw new
> Exception("unable to find log file");
>
> log4net.Config.XmlConfigurator.ConfigureAndWatch(log4NetConfigFile);
>
> thank you
> wijitha
>
>
>
> jsinnott@sftcon.com wrote:
>>
>>
>>
>> // initialize the log4net config file
>>
>> // the system expects to find it in the same folder as the exe file =
>> /bin/debug
>>
>> FileInfo  log4NetConfigFile = new  FileInfo  
>> ( "MyCustomConfigFile.config"
>> );
>>
>> if (log4NetConfigFile == null || ! log4NetConfigFile.Exists) throw  
>> new
>> Exception ( "unable to find log4net log file" );
>>
>> XmlConfigurator .Configure(log4NetConfigFile);
>>
>>
>>
>> ----------------------------------------
>>
>> From: "wijitha" <wi...@gmail.com>
>> Sent: Friday, December 05, 2008 10:14 AM
>> To: log4net-user@logging.apache.org
>> Subject: How to use separate config file for log4net
>>
>> Hi all
>>
>> I want to use log4net with my project.
>> here is the simple example i tried.
>>
>> App.config :
>>
>> type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
>>
>> A1 is set to be a ConsoleAppender
>>
>> A1 uses PatternLayout
>>
>> Set root logger level to DEBUG and its only appender to A1
>>
>> here is the code:
>>
>> private static readonly ILog log =
>> LogManager.GetLogger(typeof(Program));
>> static void Main(string[] args)
>> {
>> XmlConfigurator.Configure();
>> log.Info("Entering application.");
>> log.Debug("Exiting application.");
>> Console.Write("This Log4Net example");
>> }
>>
>> This thing is working well.
>> And what i want to do is put the log4net configuration part in a  
>> separate
>> file call Log4Net.config
>>
>> if any one know how to do this please reply me. It is better if u can
>> separately give me the two configuration files. (App.config and
>> Log4Net.config)
>>
>> thanks a lot
>> wijitha
>>
>> -- 
>> View this message in context:
>> http://www.nabble.com/How-to-use-separate-config-file-for-log4net-tp20856712
>> p20856712.html
>> Sent from the Log4net - Users mailing list archive at Nabble.com.
>>
>>
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/How-to-use-separate-config-file-for-log4net-tp20856712p20870124.html
> Sent from the Log4net - Users mailing list archive at Nabble.com.
>

Re: re: How to use separate config file for log4net

Posted by wijitha <wi...@gmail.com>.
Hi
First, thanks for replying me.

now that is working.
but now i wanna know, Should this piece of code be written  in constructor
of each class i write.

      FileInfo log4NetConfigFile = new FileInfo("log4net.config");

  if (log4NetConfigFile == null || !log4NetConfigFile.Exists) throw new
Exception("unable to find log file");
           
log4net.Config.XmlConfigurator.ConfigureAndWatch(log4NetConfigFile);

thank you
wijitha



jsinnott@sftcon.com wrote:
> 
> 
> 
>  // initialize the log4net config file 
> 
>  // the system expects to find it in the same folder as the exe file = 
> /bin/debug 
> 
>  FileInfo  log4NetConfigFile = new  FileInfo ( "MyCustomConfigFile.config" 
> );
> 
> if (log4NetConfigFile == null || ! log4NetConfigFile.Exists) throw new  
> Exception ( "unable to find log4net log file" );
> 
>  XmlConfigurator .Configure(log4NetConfigFile);
> 
>   
> 
> ----------------------------------------
> 
> From: "wijitha" <wi...@gmail.com>
> Sent: Friday, December 05, 2008 10:14 AM
> To: log4net-user@logging.apache.org
> Subject: How to use separate config file for log4net 
> 
> Hi all
> 
> I want to use log4net with my project.
> here is the simple example i tried.
> 
> App.config :
> 
> type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
> 
> A1 is set to be a ConsoleAppender 
> 
> A1 uses PatternLayout 
> 
> Set root logger level to DEBUG and its only appender to A1 
> 
> here is the code:
> 
> private static readonly ILog log =
> LogManager.GetLogger(typeof(Program));
> static void Main(string[] args)
> {
> XmlConfigurator.Configure();
> log.Info("Entering application."); 
> log.Debug("Exiting application.");
> Console.Write("This Log4Net example");
> }
> 
> This thing is working well.
> And what i want to do is put the log4net configuration part in a separate
> file call Log4Net.config
> 
> if any one know how to do this please reply me. It is better if u can
> separately give me the two configuration files. (App.config and
> Log4Net.config)
> 
> thanks a lot 
> wijitha
> 
> -- 
> View this message in context: 
> http://www.nabble.com/How-to-use-separate-config-file-for-log4net-tp20856712
> p20856712.html
> Sent from the Log4net - Users mailing list archive at Nabble.com.
> 
>  
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-use-separate-config-file-for-log4net-tp20856712p20870124.html
Sent from the Log4net - Users mailing list archive at Nabble.com.