• 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
        }
    

      

  • 相关阅读:
    浅析NetFilter和iptables
    关于skb_header_pointer函数
    Linux kernel 绝对路径之d_path篇
    几个内核函数:filp_open、filp_read、IS_ERR、ERR_PTR、PTR_ERR
    一文读懂数字签名
    Nginx配置文件nginx.conf中文详解(总结)
    nginx 重写 rewrite 基础及实例
    最完美解决Nginx部署ThinkPHP项目的办法
    nginx中的try_files指令解释
    js电话号码正则校验--座机和手机号
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/4556748.html
Copyright © 2020-2023  润新知