Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Logging using log4net

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 75
    Comment on it

    Log4net is an open source library that allows .NET applications to log information in many ways. Here we are going to see an example for logging in a file.

     

    Few steps to follow to log in a file:

     

    1) Add log4Net dll in the application.

     

    2) Add few pieces of information in the config file in order to make it work properly with log4net.

     

     <log4net debug="true">
        <root>
          <level value="ALL" />
          <appender-ref ref="file" />
        </root>
        <appender name="file" type="log4net.Appender.RollingFileAppender, log4net">
          <file value="LogBackUp\Log.txt" />
          <appendToFile value="true" />
          <rollingStyle value="Size" />
          <maximumFileSize value="5MB" />
          <maxSizeRollBackups value="10" />
          <staticLogFileName value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%-5level] (%thread) %logger - %message%newline" />
          </layout>
        </appender>
      </log4net>

     

    3) Need to initialize Logger and place some line of code. In the given example, "OpenTicket" in the name of the class to which we have to do logging.

     private static ILog _logger = null;
            private static ILog Logger
            {
                get
                {
                    if (_logger == null)
                    {
                        _logger = LogManager.GetLogger(typeof(OpenTicket));
                        log4net.Config.XmlConfigurator.Configure();
                    }
                    return _logger;
                }
            }

     

    Hope, this example will help you!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: