Log4net. config configuration enables summary of common methods

  • 2021-11-14 05:25:07
  • OfStack

In the above, a simple log4 configuration was built, and the configuration of log4net was enabled in practice. Here is a summary.

Mode 1:

Programmatically configured at run time, the code is as follows:


class Program
  {
    private readonly static ILog log = InitILog();
    //private readonly static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    public static void Main(string[] args)
    {
      var gp=log4net.LogManager.GetRepository().Configured;
      log.Debug(" Test ");
      Console.ReadKey();
    }

    public static ILog InitILog() 
    {
      var file = AppDomain.CurrentDomain.BaseDirectory + @"\Config\log4net.config";
      FileInfo info = new FileInfo(file);
      XmlConfigurator.Configure(info);
      return LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    }
  }

In the method InitLog, the configuration of log4net is started by obtaining the path of the configuration file (configuration file path = application basic directory + assembly file name + extension), reading the contents of the configuration file using FileInfo and Configure.

Mode 2:

assembly-level configuration properties, see code:


[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config/log4net.config", Watch =true)]namespace SpringNetIOC
{
  class Program
  {
    //private readonly static ILog log = InitILog();
    private readonly static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    public static void Main(string[] args)
    {
      var gp=log4net.LogManager.GetRepository().Configured;
      log.Debug(" Test ");
      Console.ReadKey();
    }

  }
}

[assembly: log4net. Config. XmlConfigurator (ConfigFile = "Config/log4net. config", Watch = true)] can also be added to the Properties/AssemblyInfo. cs== file to let the program find the log4net. config file.

It is necessary to explain 3 configurable attributes of XmlConfigurator under 1.

a. ConfigFile configuration file name and path, including extension, file relative to the root directory of the program. Note that this property cannot be used in conjunction with the ConfigFileExtension property.

b. ConfigFileExtension; The suffix name of the configuration file, which defaults to 'config', is not common to the ConfigFile property.

With regard to ConfigFileExtension attributes, I hereby record 1, and then review it after having a more in-depth understanding.

c. Watch (bool attribute), if true, the log4net framework monitors files while running. If the configuration file is modified, reload the configuration file.

Mode 3:

app. appSettings configuration in config. Look at the code:


<configuration>
  <appSettings>
    <add key="log4net.Config" value="Config/log4net.config"/>
    <add key="log4net.Config.Watch" value="True"/>
  </appSettings>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

key is lognet. Config overrides the value specified by ConfigFile in the XmlConfigurator configuration, and key is log4net. Config. Watch overrides the value specified by Watch in the assembly XmlConfigurator configuration.


Related articles: