In this particular article, we are going to learn on how to use the Log4Net library for creating logs in a Console Application.
Let's begin with creating a Console application in VS2013(you can use any other version you wish) and then install the Log4Net library from the Nuget Package library.
After installing this package, open up AssemblyInfo.cs file under the Properties folder and add the log4net assembly information into it (under the other assembly information.).
- [assembly: log4net.Config.XmlConfigurator(Watch=true)]
- <configSections>
- <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" /> </configSections>
- <!-- Log4net Logging Setup -->
- <log4net>
- <appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
- <file value="C:\\Users\\a0685560\\Downloads\\mylogfile.txt" />
- <!-- the location where the log file would be created -->
- <appendToFile value="true" />
- <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="%date [%thread] %level %logger - %message%newline" /> </layout>
- <filter type="log4net.Filter.LevelRangeFilter">
- <levelMin value="INFO" />
- <levelMax value="FATAL" /> </filter>
- </appender>
- <root>
- <level value="DEBUG" />
- <appender-ref ref="FileAppender" /> </root>
- </log4net>
In our Program.cs file, let's create a function as below:
- internal static void Testing()
- {
- log4net.Config.BasicConfigurator.Configure();
- log4net.ILog log = log4net.LogManager.GetLogger(typeof (Program));
- try
- {
- string str = String.Empty;
- string subStr = str.Substring(0, 4); //this line will create error as the string "str" is empty.
- }
- catch (Exception ex)
- {
- log.Error("Error Message: " + ex.Message.ToString(), ex);
- }
- }
- static void Main(string[] args)
- {
- Testing();
- Console.ReadLine();
- }
2016-01-20 15:41:15,558 [8] ERROR TestingLog4Net.Program - Error Message: Index and length must refer to a location within the string.
Parameter name: length
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.Substring(Int32 startIndex, Int32 length)
at TestingLog4Net.Program.Testing() in c:\Users\Vipul\Documents\Visual Studio 2013\Projects\TestingLog4Net\TestingLog4Net\Program.cs:line 34
The log above contains the error message as well as the complete Stack information.
We have many different options for error logging, as mentioned below:
- log.Debug();
- log.Warn();
- log.Fatal();
I would request you to drill into the other methods available in the Log4Net metadata file so as to get more insight into it.

RohitPosted Nov 19, 2024, 1:09 PM
How to do the same but for WebApi in Dotnet Framework
Saravanan KPosted Dec 27, 2022, 3:34 PM
How to apply filter, Like How to exclude Info logs? Only write Error and Warning?
Manoj KumarPosted Dec 5, 2022, 4:57 AM
It'll be very much useful for beginners to learn how Log4Net works very Good article.
Arul DineshPosted May 16, 2022, 3:13 PM
Very nice article
Richard LovePosted Feb 5, 2019, 10:05 AM
This was great, quick and to the point. Do you have any recommendation for how to create robust error handling?
Padmaja VudathaPosted Feb 1, 2018, 7:00 AM
Thank you for the article
Devender SharmaPosted Jan 8, 2018, 2:24 AM
Very nice article......
Shubham KumarPosted Jan 21, 2016, 3:55 AM
nice