Answers for "log4net config example, usage"

C#
0

log4net config example, usage

using System;


namespace library
{
    public class Worker
    {
        private readonly log4net.ILog _logger = log4net.LogManager.GetLogger(typeof(Worker));
        public void doWork()
        {
            _logger.Info("Worker starting...");
            throw new Exception("This is an exception that occurred to show how to log your exceptions with log4Net");
        }
    }
}
Posted by: Guest on August-16-2021
0

log4net config example, usage

using System;
using System.Diagnostics;

namespace application
{
    class Program
    {
        private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(typeof(Program));
        
        static void Main(string[] args)
        {
            var timer = Stopwatch.StartNew();// starting a timer do show how to log it later
            _logger.Info("application is starting...");
            _logger.Warn("Looks like nothing is really happening in this application");

            try
            {
                // doing something with the library
                var worker=new library.Worker();
                worker.doWork();
            }

            catch (Exception ex)
            {
                _logger.Error("Humm... something went as unexpected", ex);
            }
            finally
            {
                _logger.InfoFormat("application completed in {0}ms",timer.ElapsedMilliseconds);
            }
            
            Console.Write("press a key to terminate");
            Console.ReadKey();
        }
    }
}
Posted by: Guest on August-16-2021

C# Answers by Framework

Browse Popular Code Answers by Language