using log4net;
using System;

namespace MyLog4Net
{
    public class MyLogHelper
    {
        public delegate void MyLogEventHandler(object sender, MyLogEventArgs e);
        public static event MyLogEventHandler LogCommit;

        /// <summary>
        /// 普通日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void Info(string module, string message)
        {
            ILog log = LogManager.GetLogger(module);
            if (log.IsInfoEnabled)
            {
                log.Info(message);

                var e = new MyLogEventArgs();
                e.Module = module;
                e.Message = message;
                LogCommit?.Invoke(null, e);
            }
        }
        /// <summary>
        /// 错误日志带异常
        /// </summary>
        /// <param name="message">错误日志</param>
        public static void Error(string module, string message, Exception ex)
        {
            ILog log = LogManager.GetLogger(module);
            if (log.IsErrorEnabled)
            {
                log.Error(message, ex);

                var e = new MyLogEventArgs();
                e.Module = module;
                e.Message = message;
                LogCommit?.Invoke(null, e);
            }
        }

        /// <summary>
        /// 错误日志不带异常
        /// </summary>
        /// <param name="message">错误日志</param>
        public static void Error(string module, string message)
        {
            ILog log = LogManager.GetLogger(module);
            if (log.IsErrorEnabled)
            {
                log.Error(message);

                var e = new MyLogEventArgs();
                e.Module = module;
                e.Message = message;
                LogCommit?.Invoke(null, e);
            }
        }
    }

}
