[Log4net] Exception while reading ConfigurationSettings
We were using Log4net logging framework to log exceptions in our web application and encountered below exception during the log:
Reasons:
There can be many reasons right from misplacing Log4net config settings to not loading the config file.
Fix:
Below is the step by step approach on how to use Log4net framework.
Install Log4net NuGet package:
- From Visual Studio, open Tools -> NuGet Package Manager -> Manage NuGet packages for solution…
- Search for ‘Log4net’ and install the package
Add configurations in App.config/Web.config:
- In your App.config/Web.config file, add a new config section named ‘log4net’. Make sure <configSections> is the first node under root <configuration> node.
- Add <log4net> node with details like Path where you would like to store the logs and other properties.
- Your App.config/Web.config file looks as below:
<configuration>
<configSections>
<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler,Log4net”/>
</configSections>
<log4net>
<appender name=”General” type=”log4net.Appender.RollingFileAppender”>
<file value=”D:\log.txt“/> <!–Physical path to log files–>
<appendToFile value=”true”/>
<rollingStyle value=”Size”/>
<!– Specifies the number of files that will be written. This is an example for ten files. The files will be named engine.log.1, engine.log.2, engine.log.3, etc. –>
<maxSizeRollBackups value=”50″/>
<!– The File Size limit for each file. KB stands for Kilobytes, MB (MegaByte) is also an option. –>
<maximumFileSize value=”5MB”/>
<staticLogFileName value=”true”/>
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%date %-5level – %message%newline”/>
</layout>
</appender>
<root>
<appender-ref ref=”General”/>
</root>
</log4net>
<startup>
<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.6.1″ />
</startup>
</configuration>
Using Log4net in your code:
- We need to load the Log4net configurations defined App.config file in class file using below statement
- Below statement can be either placed in your Class file or project’s “AssemblyInfo.cs” file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
- Define LogManager Object using below statement
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
- Use Log.Info(“”) to log info and Log.Error(“”) to log exception details.
- With above specified statements your class file looks as below
using log4net;
// Load Log4net configurations defined App.config file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Log4Net{
class Program{
// Define LogManager Object
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);static void Main(string[] args){
try{
Log.Info(“Log Starts”);
// Throw error
throw new Exception(“Exception!!!!”);
}
catch (Exception ex){
Log.Error(“Error – ” + ex.Message);
}
finally{
Log.Info(“Log Ends”);
Console.ReadLine();
}
}}
- Execute the logic and you should get a log file stored in specified location with logs
Refer article for Log4net best practices.
🙂