What would a Log4Net Wrapper class look like?

后端 未结 9 1044
失恋的感觉
失恋的感觉 2020-12-02 05:19

I have been looking for a logging framework for .net (c#) and decided to give log4net a go after reading up on a few question/answer threads here on stackoverflow. I see peo

9条回答
  •  悲哀的现实
    2020-12-02 05:50

    I have successfully isolated log4net dependency into a single project. If you intend to do the same, here is what my wrapper class look like:

    using System;
    
    namespace Framework.Logging
    {
        public class Logger
        {
            private readonly log4net.ILog _log;
    
            public Logger()
            {
                _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            }
    
            public Logger(string name)
            {
                _log = log4net.LogManager.GetLogger(name);
            }
    
            public Logger(Type type)
            {
                _log = log4net.LogManager.GetLogger(type);
            }
    
            public void Debug(object message, Exception ex = null)
            {
                if (_log.IsDebugEnabled)
                {
                    if (ex == null)
                    {
                        _log.Debug(message);
                    }
                    else
                    {
                        _log.Debug(message, ex);
                    }
                }
            }
    
            public void Info(object message, Exception ex = null)
            {
                if (_log.IsInfoEnabled)
                {
                    if (ex == null)
                    {
                        _log.Info(message);
                    }
                    else
                    {
                        _log.Info(message, ex);
                    }
                }
            }
    
            public void Warn(object message, Exception ex = null)
            {
                if (_log.IsWarnEnabled)
                {
                    if (ex == null)
                    {
                        _log.Warn(message);
                    }
                    else
                    {
                        _log.Warn(message, ex);
                    }
                }
            }
    
            public void Error(object message, Exception ex = null)
            {
                if (_log.IsErrorEnabled)
                {
                    if (ex == null)
                    {
                        _log.Error(message);
                    }
                    else
                    {
                        _log.Error(message, ex);
                    }
                }
            }
    
            public void Fatal(object message, Exception ex = null)
            {
                if (_log.IsFatalEnabled)
                {
                    if (ex == null)
                    {
                        _log.Fatal(message);
                    }
                    else
                    {
                        _log.Fatal(message, ex);
                    }
                }
            }
        }
    }
    

    And dont forget to add this in the AssemblyInfo.cs of the interfacing project (took me a good few hours to find this)

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

    And put your log4net configuration xml in log4net.config file, set it as Content, Copy Always

提交回复
热议问题