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>

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