zoukankan      html  css  js  c++  java
  • 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", @"
    ");
                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.
    
     Error Info: {0}. 
    
     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
        }
    

      

  • 相关阅读:
    在终端中输出彩色文字
    卸载PythonToolKit的方法
    几款python集成开发环境
    关于Python中数据对象的可变性
    解决vim无法返回上次的位置
    evince无法返回上次浏览的位置
    解决xfce4桌面图标消失的问题
    xfce4的截屏插件
    linux 查看chm文档的软件
    [MySQL]group by 与 if 的统计技巧
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/4556748.html
Copyright © 2011-2022 走看看