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 in the App.config to:
...
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
...
<log4net configSource="Log.config" />
...
And in the Log.Config file (same directory as the App.config) we have:
<log4net>
<appender name="General" type="log4net.Appender.FileAppender">
<file value="myapp.log" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<appender-ref ref="General" />
</root>
</log4net>
However, when we run the app, no log file is created (and no logging done). There are no error messages output to the console.
If we move the contents of the Log.config file back into the App.config (replacing the first code line above), it works as expected. Any idea why it is not working in an external file?
@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.
The step that has been missed is
this will cause the configSource to be used. Make sure you call it once before calling GetLogger();
Do you have the following attribute in your
AssemblyInfo.cs
file:and code like this at the start of each class that requires logging functionality:
I have a blog post containing this and other info here.
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:
Step 2: Use the magic log4net.Config key in appSettings.
Step 3: Contribute a patch to fix the handling of configSource.
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. :)
In my case I got this error message:
And
log4net.config
the file was in Visual Studio 2017 project, but when I checked the file inbin\Debug
folder I could not find it.My original assembly setup was like this:
After some research time, I change it to the following and it works: