| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 | 
							- using System;
 
- using System.Collections.Concurrent;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Text.RegularExpressions;
 
- using System.Threading;
 
- using System.Threading.Tasks;
 
- using static System.DateTime;
 
- namespace Masuit.Tools.Logging;
 
- /// <summary>
 
- /// 日志组件
 
- /// </summary>
 
- public class LogManager
 
- {
 
-     private static readonly ConcurrentQueue<(string logPath, string msg)> LogQueue = new();
 
-     /// <summary>
 
-     /// 自定义事件
 
-     /// </summary>
 
-     public static event Action<LogInfo> Event;
 
-     static LogManager()
 
-     {
 
-         Task.Factory.StartNew(() =>
 
-         {
 
-             while (true)
 
-             {
 
-                 Pause.WaitOne(1000, true);
 
-                 var temp = new List<string[]>();
 
-                 foreach (var (logPath, msg) in LogQueue)
 
-                 {
 
-                     var logMergeContent = string.Concat(msg, Environment.NewLine, "----------------------------------------------------------------------------------------------------------------------", Environment.NewLine);
 
-                     var logArr = temp.FirstOrDefault(d => d[0].Equals(logPath));
 
-                     if (logArr != null)
 
-                     {
 
-                         logArr[1] = string.Concat(logArr[1], logMergeContent);
 
-                     }
 
-                     else
 
-                     {
 
-                         logArr = new[]
 
-                         {
 
-                             logPath,
 
-                             logMergeContent
 
-                         };
 
-                         temp.Add(logArr);
 
-                     }
 
-                     LogQueue.TryDequeue(out _);
 
-                 }
 
-                 foreach (var item in temp)
 
-                 {
 
-                     WriteText(item[0], item[1]);
 
-                 }
 
-             }
 
-         }, TaskCreationOptions.LongRunning);
 
-     }
 
-     private static AutoResetEvent Pause => new(false);
 
-     private static string _logDirectory;
 
-     /// <summary>
 
-     /// 日志存放目录,默认日志放在当前应用程序运行目录下的logs文件夹中
 
-     /// </summary>
 
-     public static string LogDirectory
 
-     {
 
-         get => _logDirectory ?? (Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory).Any(s => s.Contains("Web.config")) ? AppDomain.CurrentDomain.BaseDirectory + @"App_Data\Logs\" : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs"));
 
-         set
 
-         {
 
-             //自定义目录
 
-             if (!Directory.Exists(value))
 
-             {
 
-                 Directory.CreateDirectory(value);
 
-             }
 
-             _logDirectory = value;
 
-         }
 
-     }
 
-     /// <summary>
 
-     /// 写入Info级别的日志
 
-     /// </summary>
 
-     /// <param name="info"></param>
 
-     public static void Info(string info)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(info).ToUpper()}  {info}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Info,
 
-             Message = info,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入Info级别的日志
 
-     /// </summary>
 
-     /// <param name="source"></param>
 
-     /// <param name="info"></param>
 
-     public static void Info(string source, string info)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(info).ToUpper()}   {source}  {info}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Info,
 
-             Message = info,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入Info级别的日志
 
-     /// </summary>
 
-     /// <param name="source"></param>
 
-     /// <param name="info"></param>
 
-     public static void Info(Type source, string info)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(info).ToUpper()}   {source.FullName}  {info}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Info,
 
-             Message = info,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source.FullName
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入debug级别日志
 
-     /// </summary>
 
-     /// <param name="debug">异常对象</param>
 
-     public static void Debug(string debug)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(debug).ToUpper()}   {debug}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Debug,
 
-             Message = debug,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入debug级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="debug">异常对象</param>
 
-     public static void Debug(string source, string debug)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(debug).ToUpper()}   {source}  {debug}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Debug,
 
-             Message = debug,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入debug级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="debug">异常对象</param>
 
-     public static void Debug(Type source, string debug)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(debug).ToUpper()}   {source.FullName}  {debug}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Debug,
 
-             Message = debug,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source.FullName
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入error级别日志
 
-     /// </summary>
 
-     /// <param name="error">异常对象</param>
 
-     public static void Error(Exception error)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {error.Source}  {error.Message}{Environment.NewLine}{error.StackTrace}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Error,
 
-             Message = error.Message,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = error.Source,
 
-             Exception = error,
 
-             ExceptionType = error.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入error级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="error">异常对象</param>
 
-     public static void Error(Type source, Exception error)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {source.FullName}  {error.Message}{Environment.NewLine}{error.StackTrace}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Error,
 
-             Message = error.Message,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source.FullName,
 
-             Exception = error,
 
-             ExceptionType = error.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入error级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="error">异常信息</param>
 
-     public static void Error(Type source, string error)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {source.FullName}  {error}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Error,
 
-             Message = error,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source.FullName,
 
-             //Exception = error,
 
-             ExceptionType = error.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入error级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="error">异常对象</param>
 
-     public static void Error(string source, Exception error)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {source}  {error.Message}{Environment.NewLine}{error.StackTrace}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Error,
 
-             Message = error.Message,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source,
 
-             Exception = error,
 
-             ExceptionType = error.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入error级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="error">异常信息</param>
 
-     public static void Error(string source, string error)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(error).ToUpper()}   {source}  {error}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Error,
 
-             Message = error,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source,
 
-             //Exception = error,
 
-             ExceptionType = error.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入fatal级别日志
 
-     /// </summary>
 
-     /// <param name="fatal">异常对象</param>
 
-     public static void Fatal(Exception fatal)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {fatal.Source}  {fatal.Message}{Environment.NewLine}{fatal.StackTrace}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Fatal,
 
-             Message = fatal.Message,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = fatal.Source,
 
-             Exception = fatal,
 
-             ExceptionType = fatal.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入fatal级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="fatal">异常对象</param>
 
-     public static void Fatal(Type source, Exception fatal)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {source.FullName}  {fatal.Message}{Environment.NewLine}{fatal.StackTrace}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Fatal,
 
-             Message = fatal.Message,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source.FullName,
 
-             Exception = fatal,
 
-             ExceptionType = fatal.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入fatal级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="fatal">异常对象</param>
 
-     public static void Fatal(Type source, string fatal)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {source.FullName}  {fatal}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Fatal,
 
-             Message = fatal,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source.FullName,
 
-             //Exception = fatal,
 
-             ExceptionType = fatal.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入fatal级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="fatal">异常对象</param>
 
-     public static void Fatal(string source, Exception fatal)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {source}  {fatal.Message}{Environment.NewLine}{fatal.StackTrace}"));
 
-         var log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Fatal,
 
-             Message = fatal.Message,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source,
 
-             Exception = fatal,
 
-             ExceptionType = fatal.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     /// <summary>
 
-     /// 写入fatal级别日志
 
-     /// </summary>
 
-     /// <param name="source">异常源的类型</param>
 
-     /// <param name="fatal">异常对象</param>
 
-     public static void Fatal(string source, string fatal)
 
-     {
 
-         LogQueue.Enqueue((GetLogPath(), $"{Now}   [{Thread.CurrentThread.ManagedThreadId}]   {nameof(fatal).ToUpper()}   {source}  {fatal}"));
 
-         LogInfo log = new LogInfo()
 
-         {
 
-             LogLevel = LogLevel.Fatal,
 
-             Message = fatal,
 
-             Time = Now,
 
-             ThreadId = Thread.CurrentThread.ManagedThreadId,
 
-             Source = source,
 
-             ExceptionType = fatal.GetType().Name
 
-         };
 
-         Event?.Invoke(log);
 
-     }
 
-     private static string GetLogPath()
 
-     {
 
-         string newFilePath;
 
-         var logDir = string.IsNullOrEmpty(LogDirectory) ? Path.Combine(Environment.CurrentDirectory, "logs") : LogDirectory;
 
-         Directory.CreateDirectory(logDir);
 
-         const string extension = ".log";
 
-         var fileNameNotExt = Now.ToString("yyyyMMdd");
 
-         var fileNamePattern = string.Concat(fileNameNotExt, "(*)", extension);
 
-         var filePaths = Directory.GetFiles(logDir, fileNamePattern, SearchOption.TopDirectoryOnly).ToList();
 
-         if (filePaths.Count > 0)
 
-         {
 
-             int fileMaxLen = filePaths.Max(d => d.Length);
 
-             string lastFilePath = filePaths.Where(d => d.Length == fileMaxLen).OrderByDescending(d => d).FirstOrDefault();
 
-             if (new FileInfo(lastFilePath).Length > 1024 * 1024)
 
-             {
 
-                 var no = new Regex(@"(?is)(?<=\()(.*)(?=\))").Match(Path.GetFileName(lastFilePath)).Value;
 
-                 var parse = int.TryParse(no, out int tempno);
 
-                 var formatno = $"({(parse ? (tempno + 1) : tempno)})";
 
-                 var newFileName = string.Concat(fileNameNotExt, formatno, extension);
 
-                 newFilePath = Path.Combine(logDir, newFileName);
 
-             }
 
-             else
 
-             {
 
-                 newFilePath = lastFilePath;
 
-             }
 
-         }
 
-         else
 
-         {
 
-             var newFileName = string.Concat(fileNameNotExt, $"({0})", extension);
 
-             newFilePath = Path.Combine(logDir, newFileName);
 
-         }
 
-         return newFilePath;
 
-     }
 
-     private static void WriteText(string logPath, string logContent)
 
-     {
 
-         try
 
-         {
 
-             if (!File.Exists(logPath))
 
-             {
 
-                 File.CreateText(logPath).Close();
 
-             }
 
-             using var sw = File.AppendText(logPath);
 
-             sw.Write(logContent);
 
-         }
 
-         catch (Exception)
 
-         {
 
-             // ignored
 
-         }
 
-     }
 
- }
 
 
  |