Log4Net config in external file does not work

前端 未结 13 2017
清歌不尽
清歌不尽 2020-11-30 20:27

We are using log4net and want to specify it\'s configuration in an external config file (as we have done with other sections). To do this we have changed the log4net section

相关标签:
13条回答
  • 2020-11-30 21:08

    The step that has been missed is

    log4net.Config.XmlConfigurator.Configure(); 
    

    this will cause the configSource to be used. Make sure you call it once before calling GetLogger();

    0 讨论(0)
  • 2020-11-30 21:08

    Watch out for this issue...

    In my case everything was configured correctly. The problem is that I used Web Deploy inside Visual Studio 2013 to upload the site to WinHost.com and it reset the ACLs on the server. This in turn revoked all permission on folders and files - log4net could not write the file.

    You can read more about it here:

    How to stop Web Deploy/MSBuild from messing up server permissions

    I asked the support team there to reset the ACLs and then log4net started spitting logs. :)

    0 讨论(0)
  • 2020-11-30 21:08

    It's also worth pointing out that in a web app it looks in the root directory, not the bin folder, so make sure that's where you put the config file.

    0 讨论(0)
  • 2020-11-30 21:10

    @Mitch, It turns out there is a file with the [assembly:...] declaration, but it did not have the ConfigFile property.

    Once I added it and pointed it to Log.config, it started working. I would have thought that it would work like all the other config sections (ie AppSettings) and accept external config files with no modification.

    We don't have the second statement you mentioned, as we wrap it in a global static log provider.

    0 讨论(0)
  • 2020-11-30 21:12

    Either use,

    <configuration>
    <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    <appSettings>
    <add key="log4net.Config" value="Log4Net.config" />
    </appSettings>
    

    or just,

    log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("Log4Net.config"));
    

    In both cases the file,Log4Net.config, should be there in output directory. You can do this by setting Log4Net.config file properties in solution explorer. Build Action - Content and Copy to Output Directory - Copy always

    0 讨论(0)
  • 2020-11-30 21:18

    There is an open defect on this issue. Log4Net does not support the configSource attribute of configuration elements. To use a purely configuration file solution you use the log4net.Config key in appSettings.

    Step 1: Include the normal configuration section definition:

    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    

    Step 2: Use the magic log4net.Config key in appSettings.

    <appSettings>
          <add key="log4net.Config" value="log4net.simple.config" />
    </appSettings>
    

    Step 3: Contribute a patch to fix the handling of configSource.

    0 讨论(0)
提交回复
热议问题