Given that you have
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
in your AssemblyInfo.cs file, when is Log4Net configured? Is it at the application start or when you use a logger for the first time?
Typically the log4net configuration is specified using a file. This file can be read in one of two ways: The System.Configuration API is only available if the configuration data is in the application's config file; the file named MyApp.exe.config or Web.config.
These configurations are designed to work with the log4net.Config.DOMConfigurator and the log4net.Repository.Hierarchy.Hierarchy . These examples are by no means exhaustive configurations for the appenders.
Using rolling files, you tell log4net to split up your files in chunks. Rolling logging can be configured by size, dates, and more. You often see people configuring a log file per day. You can log to elmah.io through log4net too. To do so, install the elmah.io.log4net NuGet package: Configure the elmah.io log4net appender in your config file:
Add log4net.config file. Add a new file to your project in Visual Studio called log4net.config and be sure to set a property for the file. Set “Copy to Output Directory” to “Copy Always”. This is important because we need the log4net.config file to be copied to the bin folder when you build and run your app.
The config file is used when you cause a call to the LoggerManager
class. This is usually caused by calling LogManager.GetLogger.
See http://logging.apache.org/log4net/release/sdk/log4net.Config.XmlConfiguratorAttribute.html for more details; specifically the note:
Log4net will only look for assembly level configuration attributes once. When using the log4net assembly level attributes to control the configuration of log4net you must ensure that the first call to any of the LoggerManager methods is made from the assembly with the configuration attributes.
A quote from the Apache log4net site:
Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked. http://logging.apache.org/log4net/release/manual/configuration.html
Here is a good article on how to make sure the configuration is set up and run properly: http://www.fooji.net/blog/post/2010/04/27/logging-log4net-e28093-part-iii.aspx
Basically, you need to either make a logging call (which configures log4net if it isn't already configured) or utilize the DOMConfigurator to manually configure log4net.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With