Exemplo n.º 1
0
 public Logger(string logTitle) : this(LogConfiguration.GetConfiguration(), logTitle)
 {
 }
Exemplo n.º 2
0
        private static void WriteLogToFile()
        {
            _logConfiguration = LogConfiguration.GetConfiguration();
            if (_logQueue.Count < 1)
            {
                return;
            }
            StreamWriter writer = null;

            if (!_logConfiguration.NoLocalFile)
            {
                string logFileFullName = string.Format("{0}/{1}", _logConfiguration.LogPath, _logConfiguration.LogFileName);
                string logPath         = Path.GetDirectoryName(logFileFullName);
                if (!Directory.Exists(logPath))
                {
                    Directory.CreateDirectory(logPath);
                }
                writer = new StreamWriter(logFileFullName, true, Encoding.Default);
            }
            do
            {
                try
                {
                    if (_logQueue.Count == 0)
                    {
                        break;
                    }
                    LogInfo logInfo = null;
                    lock (_logQueue)
                    {
                        logInfo = _logQueue.Dequeue();
                    }
                    if (logInfo == null)
                    {
                        continue;
                    }
                    if (logInfo.LogLevel < _logConfiguration.LogLevel)
                    {
                        continue;
                    }
                    if (logInfo.InnerException != null)
                    {
                        if (string.IsNullOrEmpty(logInfo.LogMessage))
                        {
                            logInfo.LogMessage = string.Format("{0}\r\n{1}", logInfo.InnerException.Message, logInfo.InnerException.StackTrace);
                        }
                        else
                        {
                            logInfo.LogMessage = string.Format("{0}\r\n错误信息:{1}\r\n异常堆栈:{2}", logInfo.LogMessage, logInfo.InnerException.Message, logInfo.InnerException.StackTrace);
                        }
                        string  cacheKey  = EncryptionUtil.EncryptMD5(logInfo.LogMessage);
                        LogInfo cacheInfo = DictionaryUtil.Get <LogInfo>(_logCache, cacheKey);
                        if (cacheInfo == null)
                        {
                            _logCache[cacheKey] = cacheInfo;
                        }
                        else
                        {
                            cacheInfo.LogCount++;
                            if ((logInfo.LogTime - cacheInfo.LogTime).TotalMinutes < 5)
                            {
                                continue;
                            }
                            logInfo.LogMessage = string.Format("{0}(5分钟内触发了{1}次)", logInfo.LogMessage, cacheInfo.LogCount);
                            cacheInfo.LogCount = 0;
                            cacheInfo.LogTime  = logInfo.LogTime;
                        }
                    }
                    if (string.IsNullOrEmpty(logInfo.LogMessage))
                    {
                        continue;
                    }
                    string logMessage = string.Format("[{0:HH:mm:ss}][{1}]:\r\n{2}", logInfo.LogTime, logInfo.LogLevel, logInfo.LogMessage);
                    if (_logConfiguration.AddConsole)
                    {
                        Console.WriteLine(logMessage);
                    }
                    if (_logConfiguration.AddDebug)
                    {
                        Debug.Print(logMessage);
                    }
                    if (writer != null)
                    {
                        writer.WriteLine(logMessage);
                    }
                    if (CanUseEventLog && _logConfiguration.WriteToEventLog)
                    {
                        EventLogEntryType logType = EventLogEntryType.Information;
                        switch (logInfo.LogLevel)
                        {
                        case LogLevel.Warning:
                            logType = EventLogEntryType.Warning;
                            break;

                        case LogLevel.Exception:
                            logType = EventLogEntryType.Error;
                            break;

                        case LogLevel.Information:
                        default:
                            logType = EventLogEntryType.Information;
                            break;
                        }
                        if (logInfo.LogLevel != LogLevel.Track)
                        {
                            _log.WriteEntry(logMessage, logType);
                        }
                    }
                    OnWriteLog?.Invoke(logInfo.LogLevel, logMessage);
                }
                catch (Exception ex)
                {
                    WriteException(ex);
                }
                finally
                {
                    if (writer != null && _logQueue.Count == 0)
                    {
                        Thread.Sleep(100);
                    }
                }
            } while (true);
            if (writer != null)
            {
                writer.Close();
                writer.Dispose();
                writer = null;
            }
        }