示例#1
0
        public static void MD5()
        {
            var str = "";

            Debug.WriteLine("src:{0},md5:{1}", str, EncryptionUtil.EncryptMD5(str));

            str = "asdf撒地方";
            Debug.WriteLine("src:{0},md5:{1}", str, EncryptionUtil.EncryptMD5(str));
        }
示例#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;
            }
        }
示例#3
0
 /// <summary>
 /// MD5加密
 /// <para>将一个对象用MD5的方式进行加密</para>
 /// </summary>
 /// <param name="content">要加密的对象</param>
 /// <returns></returns>
 public static string EncryptMD5(this object content)
 {
     return(EncryptionUtil.EncryptMD5(content));
 }