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)]
Now, open the App.config file and enter required details for LogNet to work.
- <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>
Now, that the configuration is complete, let's write the code to create error so that we can log the same.
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);
- }
- catch (Exception ex)
- {
- log.Error("Error Message: " + ex.Message.ToString(), ex);
- }
- }
Let's just call this function in our main.
- static void Main(string[] args)
- {
- Testing();
- Console.ReadLine();
- }
On running the code, we notice a file named "mylogfile.txt" is now created at the location "C:\\Users\\a0685560\\Downloads\\mylogfile.txt".
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();
All these depicts different types of log message.
I would request you to drill into the other methods available in the Log4Net metadata file so as to get more insight into it.