Exemple #1
0
        /// <summary>
        /// 记录日志信息
        /// </summary>
        /// <param name="level">日志级别</param>
        /// <param name="srcName">日志源</param>
        /// <param name="logDescripton">日志描述</param>
        public static void OnLogEvent(LogTextLevel level, string srcName, string logDescripton)
        {
            // 没有使能日志, 快速返回
            if (!_isLogging)
            {
                return;
            }

            //去掉空格和数字
            //   string functionName = Regex.Replace(srcName, @"[\s<\d>]", string.Empty);

            DateTime NowTime = DateTime.Now;

            // 判断级别,是否将该条日志记录到日志文件中
            if ((int)LogLevel <= (int)level)
            {
                MemLog NewLog = new MemLog();
                NewLog.LogTime    = NowTime;
                NewLog.LogLevel   = level;
                NewLog.LogSrcName = srcName;
                NewLog.LogDesc    = logDescripton;

                //添加重复日志的过滤
                _logMem.Enqueue(NewLog);

                // 激活写日志文件线程
                _logSaveWait.Set();
            }
        }
Exemple #2
0
        /// <summary>
        /// 静态构造函数, 在第一次访问时调用
        /// </summary>
        static LogText()
        {
            _logPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Logs";

            try
            {
                // 读取配置文件设置
                Configuration AppCfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                // 获取自定义的logSettings配置区
                AppSettingsSection logSec = AppCfg.Sections["logSettings"] as AppSettingsSection;
                if (logSec == null)
                {
                    logSec = new AppSettingsSection();
                    AppCfg.Sections.Add("logSettings", logSec);
                    AppCfg.Save(ConfigurationSaveMode.Modified);
                }

                // 读是否记录日志配置
                if (logSec.Settings.AllKeys.Contains("IsLogging"))
                {
                    string CfgValue = logSec.Settings["IsLogging"].Value;
                    _isLogging = Convert.ToBoolean(CfgValue);
                }
                else
                {
                    IsLogging = true;
                }

                // 读是否输出到Output窗口配置
                if (logSec.Settings.AllKeys.Contains("IsPrintToOutput"))
                {
                    string CfgValue = logSec.Settings["IsPrintToOutput"].Value;
                    _isPrintToOutput = Convert.ToBoolean(CfgValue);
                }
                else
                {
                    IsPrintToOutput = true;
                }

                // 读是否输出到日志文件配置
                if (logSec.Settings.AllKeys.Contains("IsPrintToFile"))
                {
                    string CfgValue = logSec.Settings["IsPrintToFile"].Value;
                    _isPrintToFile = Convert.ToBoolean(CfgValue);
                }
                else
                {
                    IsPrintToFile = true;
                }

                //从配置文件中读取最大的缓存日志的记录条数
                if (logSec.Settings.AllKeys.Contains("maxLogCount"))
                {
                    string maxLogCountStr = logSec.Settings["maxLogCount"].Value;
                    if (!string.IsNullOrEmpty(maxLogCountStr))
                    {
                        maxLogCount = int.Parse(maxLogCountStr);
                    }
                }

                // 读日志记录级别配置
                if (logSec.Settings.AllKeys.Contains("LogLevel"))
                {
                    string CfgValue = logSec.Settings["LogLevel"].Value;
                    _logLevel = (LogTextLevel)Enum.Parse(typeof(LogTextLevel), CfgValue, true);
                }
                else
                {
                    //LogLevel = LogTextLevel.Info;
                    LogLevel = LogTextLevel.Debug;
                }
            }
            catch (System.Exception)
            {
                _isLogging       = true;
                _isPrintToOutput = false;
                _isPrintToFile   = true;
                _logLevel        = LogTextLevel.Info;
            }

            // 创建日志文件
            try
            {
                DirectoryInfo directory = new DirectoryInfo(_logPath);
                if (!directory.Exists)
                {
                    directory.Create();
                    //FileOperate.addpathPower(directory.FullName);
                }
                _logPath = _logPath + "\\Log_" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".txt";

                using (StreamWriter logFile = File.CreateText(_logPath))
                {
                    logFile.Close();
                }

                Task.Factory.StartNew(WriteThread, cts.Token);

                //_logSaveThread = new Thread(WriteThread);
                //_logSaveThread.Name = "LogWriteThread";
                //_logSaveThread.IsBackground = true;
                //_logSaveThread.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                // 如果创建目录失败, 例如没有目录权限, 忽略
            }
        }