using System;
using System.Collections.Generic;
using log4net;
using log4net.Config;
using System.IO;
using System.Reflection;
using System.Web;

namespace XmlDucumentAnalysis
{
    /// <summary>
    /// log4net封装类
    /// </summary>
    public class Log
    {
        #region 静态变量
        private const string DEFAULTLOGGERNAME = "ApplicationInfoLog";
        private static string LOGCONFIG = "log4net.config";//log4net配置文件名
        private static Dictionary<string, ILog> dicLogger = new Dictionary<string, ILog>();
        #endregion

        #region 静态构造函数

        static Log()
        {
            //设置log4net配置文件
            if (File.Exists(Log4NetConfigFile))
            {
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    XmlConfigurator.ConfigureAndWatch(new FileInfo(Log4NetConfigFile));
                }
                else
                {
                    XmlConfigurator.Configure(new FileInfo(Log4NetConfigFile));
                }
            }
        }

        #endregion

        /// <summary>
        /// 配置文件路径
        /// </summary>
        public static string Log4NetConfigFile
        {
            get
            {
                //if (System.Web.HttpContext.Current != null)
                //    return Path.Combine(System.Web.HttpContext.Current.Request.PhysicalApplicationPath, LOGCONFIG);
                //else
                return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), LOGCONFIG);
            }
        }

        #region Fatal
        /// <summary>
        /// 记录一个程序致命性错误
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exp"></param>
        public static void Fatal(string message, Exception exp)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsFatalEnabled)
                {
                    Logger.Fatal(message, exp);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Fatal() failed on logging '{0}'", message + "::" + exp.Message));
            }
        }

        /// <summary>
        /// 记录一个程序致命性错误
        /// </summary>
        /// <param name="message"></param>
        public static void Fatal(string message)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsFatalEnabled)
                {
                    Logger.Fatal(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Fatal() failed on logging '{0}'", message), e);
            }
        }
        #endregion

        #region Error
        /// <summary>
        /// 记录一个错误信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exp"></param>
        public static void Error(string message, Exception exp)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error(message, exp);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Error() failed on logging '{0}'", message + "::" + exp.Message));
            }
        }

        /// <summary>
        /// 记录一个错误信息
        /// </summary>
        /// <param name="message"></param>
        public static void Error(string message)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Error() failed on logging '{0}'", message), e);
            }
        }
        #endregion

        #region Warn
        /// <summary>
        /// 记录一个警告信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exp"></param>
        public static void Warn(string message, Exception exp)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsWarnEnabled)
                {
                    Logger.Warn(message, exp);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Warn() failed on logging '{0}'", message + "::" + exp.Message));
            }
        }

        /// <summary>
        /// 记录一个警告信息
        /// </summary>
        /// <param name="message"></param>
        public static void Warn(string message)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsWarnEnabled)
                {
                    Logger.Warn(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Warn() failed on logging '{0}'", message), e);
            }
        }
        #endregion

        #region Info
        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exp"></param>
        public static void Info(string message, Exception exp)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info(message, exp);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Info() failed on logging '{0}'", message + "::" + exp.Message));
            }
        }

        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="message"></param>
        public static void Info(string message)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Info() failed on logging '{0}'", message), e);
            }
        }
        #endregion

        #region Debug
        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exp"></param>
        public static void Debug(string message, Exception exp)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug(message, exp);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Debug() failed on logging '{0}'", message + "::" + exp.Message));
            }
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(string message)
        {
            Debug(message, DEFAULTLOGGERNAME);
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="LoggerName"></param>
        public static void Debug(string message, string LoggerName)
        {
            if (HttpContext.Current != null)
                message = System.Web.HttpContext.Current.Request.Url.ToString() + "\r\n" + message;

            ILog Logger = GetLogger(LoggerName);

            try
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug(message);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Debug() failed on logging '{0}'", message), e);
            }
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(Exception exp)
        {
            ILog Logger = GetLogger();
            try
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug(exp);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException(string.Format("Logger.Debug() failed on logging '{0}'", exp.Message));
            }
        }
        #endregion

        #region 其他
        /// <summary>
        /// 获取实现ILog接口实例
        /// </summary>
        /// <returns></returns>
        public static ILog GetLogger()
        {
            return GetLogger(DEFAULTLOGGERNAME);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="LoggerName"></param>
        /// <returns></returns>
        public static ILog GetLogger(string LoggerName)
        {
            if (!dicLogger.ContainsKey(LoggerName))
                dicLogger.Add(LoggerName, LogManager.GetLogger(LoggerName));
            return dicLogger[LoggerName];
        }
        #endregion

    }
}
