Log4net Sample for .NET Logging

Log4net is a .NET component from Apache Foundation, this provides easy mechanism for logging messages. Log4net provides the following seven levels of logging:
1. ALL
7. OFF

Advantages of Log4Net :
  • Logging behavior can be set at runtime using a configuration file. Configuration files are in XML format. 
  • Log4net is thread-safe.
  • It  categorizes logging into levels: DEBUG, INFO, WARN, ERROR and FATAL.
  • The target of the log output as well as the writing strategy can be altered by writing a new appender class.
  • It  supports multiple output appenders per logger

Download the Log4Net --> https://logging.apache.org/log4net/

Sample Application:
1) Add the Reference for log4net.dll in your project.
2) Add the following line to AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "log4net", Watch = true)]

ConfigFileExtension Attribute :

  • If specified, this is the extension for the configuration file.
  • The assembly file name is used as the base name with this extension appended.
  • If the assembly is loaded from the a file TestApp.exe and the ConfigFileExtension property is set to log4net then the configuration file name is TestApp.exe.log4net. This is equivalent to setting the ConfigFile property to TestApp.exe.log4net.

Add the following code in the configuration file(App.config or Web.config file)

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />   

    <appender name="InfoLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="Logs\SampleApp_Debug" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="datePattern" value="_yyyyMMdd'.txt'" />
      <param name="StaticLogFileName" value="false" />
      <param name="Threshold" value="DEBUG" />
      <layout type="log4net.Layout.PatternLayout,log4net">
           <conversionPattern value="%date - [%thread] - %level - %message%newline" />
        <footer value="
" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="DEBUG" />
      <filter class="log4net.Filter.DenyAllFilter" />
    <appender name="ErrorLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="Logs\ SampleApp _Error" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="datePattern" value="_yyyyMMdd'.txt'" />
      <param name="StaticLogFileName" value="false" />
      <param name="Threshold" value="ERROR" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <conversionPattern value="%date - [%thread] - %level - %message%newline" />
        <footer value="
" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="WARN" />
        <levelMax value="ERROR" />
      <level value="debug" />
      <appender-ref ref="InfoLogFileAppender" />
      <appender-ref ref="ErrorLogFileAppender" />

Now we need to get the logger in our code and do the actual logging using the appenders we have defined in the .config file.

log4net.Config.BasicConfigurator.Configure(); ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Debug("This is a Sample debug message");
log.Warn("This is a Sample warn message");
log.Error("This is a Sample error message");
log.Fatal("This is a Sample fatal message");

What is GST

GST (Goods and Service Tax) is a comprehensive tax on the supply of goods and services at each stage of any transaction. Read More

Income Tax Information

An income tax is a tax imposed by government on income earned by you. Income tax is a key source of funds that the government uses to fund its activities and serve the public. Read More

General Insurance Companies in India

General insurance is insurance for valuables other than our life and health. General insurance covers the insurer against damage, loss and theft of your valuables. Read More

Types of Bank Loans In India

Loan means lending money from one individual or entity to another. A loan has three components – principal or the borrowed amount, rate of interest and tenure or duration for which the loan is availed. Read More

List of Banks in India

The Reserve Bank of India is the central Bank that is fully owned by the Government of India. It is governed by a central board (headed by a Governor) appointed by the Central Government. Read More

List of Educational Institutions in India

The following list of comprehensive websites on higher education in India. These websites will provide detailed information on education system in India. Read More