Log4netHelper.cs 3.87 KB
using System.Diagnostics;
using System.Reflection;
using log4net;
using log4net.Repository;

namespace Hh.Mes.Common.log
{
    /// <summary>
    ///Log4
    /// </summary>
    public class Log4NetHelper
    {
        /// <summary>
        /// log4net 仓储
        /// </summary>
        public  ILoggerRepository Repository { get; set; }

        private static volatile Log4NetHelper _instance = null;
        private static readonly object LockHelper = new object();

        /// <summary> Gets the instance
        /// </summary>
        public static Log4NetHelper Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (LockHelper)
                    {
                        if (_instance == null)
                        {
                            _instance = new Log4NetHelper();
                        }
                    }
                }
                return _instance;
            }
        }
        private Log4NetHelper()
        {

        }

        /// <summary>
        /// 获取日志初始化器
        /// </summary>
        /// <returns></returns>
        private ILog Init(string type)
        {
            return LogManager.GetLogger(Repository.Name, type);
        }


        public  void Error(string message)
        {
            StackTrace trace = new StackTrace();
            var className = trace.GetFrame(1).GetMethod().DeclaringType;
            //获取方法名称
            MethodBase method = trace.GetFrame(1).GetMethod();
            var type = "className:" + className.Namespace + "\r\n\rMethodName:" + method.Name;
            //记录日志
            WriteLog(LogLevel.Error, message, type);
        }

        public  void Warning(string message)
        {
            StackTrace trace = new StackTrace();
            var className = trace.GetFrame(1).GetMethod().DeclaringType;
            MethodBase method = trace.GetFrame(1).GetMethod();
            var type = "className:" + className.Namespace + "\r\n\rMethodName:" + method.Name;
            WriteLog(LogLevel.Warning, message, type);
        }


        public  void Info(string message)
        {
            StackTrace trace = new StackTrace();
            var className = trace.GetFrame(1).GetMethod().DeclaringType;
            MethodBase method = trace.GetFrame(1).GetMethod();
            var type = "className:" + className.Namespace + "\r\n\rMethodName:" + method.Name;
            WriteLog(LogLevel.Info, message, type);
        }


        public  void Debug(string message)
        {
            StackTrace trace = new StackTrace();
            var className = trace.GetFrame(1).GetMethod().DeclaringType;
            MethodBase method = trace.GetFrame(1).GetMethod();
            var type = "className:" + className.Namespace + "\r\n\rMethodName:" + method.Name;
            WriteLog(LogLevel.Debug, message, type);
        }


        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="logLevel">日志等级</param>
        /// <param name="message">日志信息</param>
        /// <param name="type">类名 方法名</param>
        private  void WriteLog(LogLevel logLevel, string message, string type)
        {
            ILog log = Init(type);
            switch (logLevel)
            {
                case LogLevel.Debug:
                    log.Debug(message);
                    break;
                case LogLevel.Error:
                    log.Error(message);
                    break;
                case LogLevel.Info:
                    log.Info(message);
                    break;
                case LogLevel.Warning:
                    log.Warn(message);
                    break;
            }

        }
    }


    /// <summary>
    /// 日志等级
    /// </summary>
    public enum LogLevel
    {
        Error,
        Debug,
        Warning,
        Info
    }
}