SoFunction
Updated on 2025-03-07

Summary of common methods for configuring enablement

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

Method 1:

Program the configuration at runtime, the code is as follows:

class Program
  {
    private readonly static ILog log = InitILog();
    //private readonly static ILog log = (().DeclaringType);
    public static void Main(string[] args)
    {
      var gp=().Configured;
      ("test");
      ();
    }

    public static ILog InitILog() 
    {
      var file =  + @"\Config\";
      FileInfo info = new FileInfo(file);
      (info);
      return (().DeclaringType);
    }
  }

In method InitLog, use FileInfo and Configure to read the configuration file content and start log4net configuration by getting the path of the configuration file (configuration file path = application base directory + assembly file name + extension).

Method 2:

Assembly-level configuration properties, see the code:

[assembly: (ConfigFile = "Config/", Watch =true)]namespace SpringNetIOC
{
  class Program
  {
    //private readonly static ILog log = InitILog();
    private readonly static ILog log = (().DeclaringType);
    public static void Main(string[] args)
    {
      var gp=().Configured;
      ("test");
      ();
    }

  }
}

[assembly: (ConfigFile = "Config/", Watch =true)] You can also add it in the Properties/== file to let the program find the file.

We need to explain the three configurable properties of XmlConfigurator.

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 simultaneously with the ConfigFileExtension property.

b. ConfigFileExtension; The suffix name of the configuration file, 'config' by default, this property cannot be shared with the ConfigFile property.

Regarding the ConfigFileExtension property, I will record it here to leave it to a deeper understanding later and then review it.

c. Watch(bool property), if true, the log4net framework monitors files when running. If the configuration file is modified, the configuration file is reloaded.

Method 3:

appSettings configuration in. Look at the code:

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

The key will overwrite the value specified by ConfigFile in the assembly XmlConfigurator configuration, and the key will overwrite the value specified by the Watch in the assembly XmlConfigurator configuration.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.