Log4 net.config Summary of common methods for enabling configuration

Time:2021-2-26

In the above, a simple log4 configuration is built, and the log4net configuration is enabled in the actual operation. Here is a summary.

Method 1:

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 (the "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 initlog method, by obtaining the path of the configuration file (configuration file path = application basic directory + assembly file name + extension), use FileInfo and configure to read the content of the configuration file and start log4net configuration.

Method 2:

For the assembly level configuration property, see the 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 (the "test");
      Console.ReadKey();
    }

  }
}

[assembly: log4 net.Config.XmlConfigurator (ConfigFile = “Config/log4 net.config “, watch = true)] can also be used in properties/ AssemblyInfo.cs== File, let the program find log4 net.config Documents.

You need to explain the three configurable properties of xmlconfigurator.

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

  b。 Configfileextension; the suffix name of the configuration file. By default, it is’ config ‘. This property cannot be shared with configFile property.

About the configfileextension property, I’d like to make a record here. Let’s wait for a deeper understanding later, and then review it.

c。 Watch (bool attribute). If it is true, the log4net framework monitors the file when it is running. If the configuration file is modified, reload the configuration file.

Method 3:

   app.config The Appsettings configuration in. 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 It will override the value specified in the configFile in the assembly xmlconfigurator configuration, with the key of log4 net.Config.Watch The specified value of watch in the assembly xmlconfigurator configuration is overridden.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.