Blanche
Engineer
Engineer
  • UID619
  • Fans3
  • Follows2
  • Posts59
Reads:2207Replies:0

[Share]log4net.Config configuration

Created#
More Posted time:Nov 4, 2016 16:31 PM
1. Programming configuration at runtime
var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
    XmlConfigurator.ConfigureAndWatch(logCfg);


2. Configure assembly-level attributes
Add the following code in ==Properties/AssemblyInfo.cs== file, so that the program will find the Log4net.config configuration file at startup.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

You can also add it in the program’s entry file Program.cs.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

XmlConfigurator can configure three attributes:
1. ConfigFile: The path and file name of the configuration file, including the extension, and the root directory of the file corresponding to the program. Note: This attribute cannot be used with the ConfigFileExtension attribute at the same time.
2. ConfigFileExtension: The extension of the configuration file. The default file extension is “config”. This attribute cannot be used with the ConfigFile attribute at the same time.
3. Watch (Boolean attribute): If it is set to true, Log4Net framework will monitor the file during runtime. If the configuration file is modified, it will be read again.
XmlConfigurator configuration examples: suppose the program name is TestApp
// Configure Log4Net to use files with the .config extension
// Log4Net will search for the configuration file TestApp.exe.config in the program’s root directory
// The file TestApp.exe.config is monitored by Log4Net for any modifications
// If TestApp.exe.config file is modified, Log4Net will automatically load it again
[assembly: log4net.Config.XmlConfigurator(Watch=true)]


// Configure Log4Net to use files with the .log4net extension
// Log4Net will search for the configuration file TestApp.exe.log4net in the program’s root directory
// The file TestApp.exe.log4net is monitored by Log4Net for any modifications
// If TestApp.exe.log4net file is modified, Log4Net will automatically load it again
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]


3. The appSettings configurations in app.config
If the setting key is log4net.Config, the setting will override the specified values by ConfigFile in assembly XmlConfigurator configuration.
If the setting key is log4net.Config.Watch, the setting will override the specified values by Watch in assembly XmlConfigurator configuration.
// With assembly configuration
[assembly: log4net.Config.XmlConfigurator(Watch=false)]
//The assemble configuration above uses the configuration file “TestApp.exe.config” which is not monitored.

//The configuration below overrides the assemble configuration, use the configuration file log4net.config which is monitored.
// The appSettings configuration
<appSettings>
    <add key="log4net.Config" value="log4net.config" />
    <add key="log4net.Config.Watch" value="True" />
    <!--
    <add key="log4net.Internal.Debug" value="true"/>
    -->
</appSettings>
Guest