C# Log4Net configuration

- Add referece to log4net.dll file - Add log4net configuration to App.config file 

<?xml version="1.0" encoding="utf-8" ?>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
   <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\MyProject.log" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Size" />
      <param name="MaxSizeRollBackups" value="10" />
      <param name="MaximumFileSize" value="500KB" />
      <param name="StaticLogFileName" value="true" />
      <param name="Threshold" value="INFO"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c.%M():%L - %m%n" />
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <param name="Threshold" value="DEBUG"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c.%M():%L - %m%n" />
    <logger name="MyCompany">
      <appender-ref ref="RollingFileAppender" />      
      <appender-ref ref="ConsoleAppender" /> 

Please make sure than <logger name="MyCompany"> is the same as your project namespace. - Add the following line to AssemblyInfo.cs to execute log4net configuration

[assembly: log4net.Config.XmlConfigurator()]

Test Program

public class Program
  static private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  static void Main(string[] args)


Popular posts from this blog

Parse XML to dynamic object in C#

Parse XML to object model in C# using XSD utility