LogLocalService.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using log4net;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace WMS.Util
  13. {
  14. public class LogLocalService
  15. {
  16. //public static readonly ConcurrentQueue<(LogType logType, object msg)> LogQueue = new ConcurrentQueue<(LogType logType, object msg)>();
  17. static LogLocalService()
  18. {
  19. log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
  20. //LogWrite();
  21. }
  22. private static object _isMqLock = new object();
  23. private static object _islock = new object();
  24. private static string fileLogPath = AppDomain.CurrentDomain.BaseDirectory + "logRecheck.txt";
  25. public static ILog GetLog(string logType)
  26. {
  27. try
  28. {
  29. if (string.IsNullOrEmpty(logType)) return null;
  30. lock (_islock)
  31. {
  32. //var repository = LogManager.GetRepository();
  33. //var appenders = repository.GetAppenders();
  34. //var targetApder = appenders.First(p => p.Name.Contains(logType)) as log4net.Appender.RollingFileAppender;
  35. //if (targetApder == null) return null;
  36. //targetApder.File = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"logs\\");
  37. //targetApder.MaximumFileSize = "1KB";
  38. //targetApder.RollingStyle = RollingFileAppender.RollingMode.Composite;
  39. //targetApder.ActivateOptions();
  40. ILog logger = LogManager.GetLogger($"{logType}");
  41. return logger;
  42. }
  43. }
  44. catch (Exception x)
  45. {
  46. return LogManager.GetLogger("Default");
  47. }
  48. }
  49. public static void Error(object ex)
  50. {
  51. var logError = GetLog(nameof(LogType.Error));
  52. if (logError != null)
  53. logError.Error(ex);
  54. }
  55. public static void Info(string infoMsg)
  56. {
  57. var logInfo = GetLog(nameof(LogType.Info));
  58. if (logInfo != null)
  59. logInfo.Info(infoMsg);
  60. }
  61. public static void Debug(string degbug)
  62. {
  63. var logDebug = GetLog(nameof(LogType.Debug));
  64. if (logDebug != null)
  65. logDebug.Debug(degbug);
  66. }
  67. public static void Warn(string warn)
  68. {
  69. var logWarn = GetLog(nameof(LogType.Warn));
  70. if (logWarn != null)
  71. logWarn.Warn(warn);
  72. }
  73. public static void Write(string message)
  74. {
  75. lock (_isMqLock)
  76. {
  77. System.IO.FileInfo info = new FileInfo(fileLogPath);
  78. if (!info.Exists)
  79. System.IO.File.Create(fileLogPath).Close();
  80. FileStream fs = new FileStream(fileLogPath, FileMode.Append, FileAccess.Write);
  81. using (StreamWriter sw = new StreamWriter(fs))
  82. {
  83. sw.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]");
  84. sw.WriteLine(message);
  85. sw.WriteLine();
  86. sw.Flush();
  87. }
  88. fs.Close();
  89. }
  90. }
  91. public static IEnumerable<string> Read()
  92. {
  93. if (!File.Exists(fileLogPath)) yield break;
  94. using (FileStream fs = new FileStream(fileLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  95. {
  96. using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
  97. {
  98. string logContext = sr.ReadToEnd();
  99. var matches = Regex.Matches(logContext.Replace("\r\n", ""), @"(\{).*?(\})", RegexOptions.IgnoreCase);
  100. foreach (Match m in matches) yield return m.Value;
  101. }
  102. }
  103. }
  104. public static void Clear()
  105. {
  106. if (File.Exists(fileLogPath))
  107. new FileStream(fileLogPath, FileMode.Truncate, FileAccess.ReadWrite).Close();
  108. }
  109. //private static void LogWrite()
  110. //{
  111. // Task.Run(() =>
  112. // {
  113. // while (true)
  114. // {
  115. // if (!LogQueue.Any())
  116. // {
  117. // Thread.Sleep(1000);
  118. // continue;
  119. // }
  120. // if (LogQueue.TryDequeue(out var data))
  121. // {
  122. // switch (data.logType)
  123. // {
  124. // case LogType.Error:
  125. // var logError = GetLog(nameof(LogType.Error));
  126. // if (logError != null)
  127. // logError.Error(data.msg);
  128. // break;
  129. // case LogType.Debug:
  130. // var logDebug = GetLog(nameof(LogType.Debug));
  131. // if (logDebug != null)
  132. // logDebug.Debug(data.msg);
  133. // break;
  134. // case LogType.Warn:
  135. // var logWarn = GetLog(nameof(LogType.Warn));
  136. // if (logWarn != null)
  137. // logWarn.Warn(data.msg);
  138. // break;
  139. // default:
  140. // var logInfo = GetLog(nameof(LogType.Info));
  141. // if (logInfo != null)
  142. // logInfo.Info(data.msg);
  143. // break;
  144. // }
  145. // }
  146. // }
  147. // });
  148. //}
  149. }
  150. public enum LogType
  151. {
  152. Info,
  153. Error,
  154. Trace,
  155. Debug,
  156. Fatal,
  157. Warn
  158. }
  159. }