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
        }
    

      

  • 相关阅读:
    android通过Canvas和Paint截取无锯齿圆形图片
    【转】mysql的cardinality异常,导致索引不可用
    mysql索引无效且sending data耗时巨大原因分析
    linux shell脚本通过参数名传递参数值
    git日志输出格式及两个版本之间差异列表
    jenkins结合ansible用shell实现自动化部署和回滚
    Linux下cp -rf总是提示覆盖的解决办法
    jenkins集成ansible注意事项Failed to connect to the host via ssh.
    ansible操作远程服务器报Error: ansible requires the stdlib json or simplejson module, neither was found!
    利用ssh-copy-id无需密码登录远程服务器
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/4556748.html
Copyright © 2011-2022 走看看