写的不错,打赏一下

Logger

 public static object locker = new object();
        public static LogConfiguration LogConfig
        {
            set;
            get;
        }

        static Logger()
        {
            if (LogConfig == null)
            {
                LogConfig = new LogConfiguration();
                InitializeLog(ConfigurationManager.AppSettings["LogConfigPath"]);
            }

        }

        public static void Debug(string message)
        {
            var configLevel = LogConfig.Level.ToUpper().Trim();
            if (configLevel.Equals(LogLevel.DEBUG.ToString()))
            {
                WriteToFile(message, LogLevel.DEBUG);
            }

        }

        public static void Warn(string message)
        {
            var configLevel = LogConfig.Level.ToUpper().Trim();
            if (configLevel.Equals(LogLevel.ERROR.ToString()))
            {
                return;
            }
            else
            {
                WriteToFile(message, LogLevel.WARN);
            }
        }

        public static void Error(string message)
        {
            WriteToFile(message, LogLevel.ERROR);
        }


        private static void WriteToFile(string message, LogLevel logLevel)
        {
            var logMessage = LogConfig.Format.Replace("%m", message);
            logMessage = logMessage.Replace("%d", DateTime.Now.ToString(LogConfig.DateFormat.Message));
            logMessage = logMessage.Replace("%p", logLevel.ToString());
            logMessage = logMessage.Replace("%n", @"\r\n");
            logMessage = logMessage.Replace("%c", "");

            lock (locker)
            {
                try
                {
                    using (StreamWriter writer = new StreamWriter(LogFileName, true, Encoding.Default))
                    {
                        writer.WriteLine(logMessage);
                    }

                }
                catch (Exception ex)
                {
                    string msg = string.Format("Write log failed.\r\n\r\n Error Info: {0}. \r\n\r\n Log Info: {1}", ex.ToString(), message);
                    WriteEventLog("NESO_LoggingComponent", msg, EventLogEntryType.Error);
                }
            }
        }

        private static string LogFileName
        {
            get
            {
                var path = string.IsNullOrEmpty(LogConfig.Path) ? @"C:\" : LogConfig.Path;

                if (!path.EndsWith(@"\"))
                {
                    path += "\\";
                }
                if (!Directory.Exists(LogConfig.Path))
                {
                    Directory.CreateDirectory(LogConfig.Path);
                }
                var fileName = LogConfig.NameFormat;

                var logNameDateFormat = DateTime.Now.ToString(LogConfig.DateFormat.FileName);

                return string.Concat(path, fileName.Replace("%d", logNameDateFormat));
            }
        }

        #region Process Log config
        private static void InitializeLog(string logConfigPath)
        {
            try
            {
                if (LogConfig == null)
                {
                    LogConfig = new LogConfiguration();
                }
                if (string.IsNullOrEmpty(logConfigPath))
                {
                    LogConfig.DateFormat = new DateFormat();
                    LogConfig.DateFormat.FileName = "yyyyMMdd";
                    LogConfig.DateFormat.Message = "yyyy-MM-dd HH:mm:ss";
                    LogConfig.Format = "[%p]%d:%m";
                    LogConfig.Level = LogLevel.ERROR.ToString();
                    LogConfig.NameFormat = "neso_log%d.txt";
                    if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["LogPath"]))
                    {
                        LogConfig.Path = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "log");
                    }
                    else
                    {
                        LogConfig.Path = ConfigurationManager.AppSettings["LogPath"];
                    }
                }
                else
                {
                    string path = string.Concat(AppDomain.CurrentDomain.BaseDirectory.Trim('\\'), logConfigPath);
                    LogConfig = XmlHelper.ConvertToObject<LogConfiguration>(path);

                }

            }
            catch (Exception e)
            {
                throw new Exception(string.Format("init log failed, {0}", e.Message));
            }
        }
        #endregion        
        private static void WriteEventLog(string source, string content, EventLogEntryType type)
        {
            try
            {
                if (!EventLog.SourceExists(source))
                {
                    EventLog.CreateEventSource(source, "Newegg NESO Log");
                }
                using (EventLog errorLog = new EventLog())
                {
                    errorLog.Source = source;
                    errorLog.WriteEntry(content, type);
                }
            }
            catch (Exception ex)
            {
                try
                {
                    using (EventLog log = new EventLog("Application", ".", "Framework.Core"))
                    {
                        log.WriteEntry(ex.ToString(), EventLogEntryType.Error);
                    }
                }
                catch
                {
                }
            }
        }
    }

    public enum LogLevel
    {
        DEBUG, WARN, ERROR
    }

  

posted @ 2016-10-19 14:38  不负春光,努力生长  阅读(434)  评论(0编辑  收藏  举报