123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using log4net;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Threading.Tasks;
- namespace WMS.Util
- {
- public class LogLocalService
- {
- //public static readonly ConcurrentQueue<(LogType logType, object msg)> LogQueue = new ConcurrentQueue<(LogType logType, object msg)>();
- static LogLocalService()
- {
- log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
- //LogWrite();
- }
-
- private static object _isMqLock = new object();
- private static object _islock = new object();
- private static string fileLogPath = AppDomain.CurrentDomain.BaseDirectory + "logRecheck.txt";
- public static ILog GetLog(string logType)
- {
- try
- {
- if (string.IsNullOrEmpty(logType)) return null;
- lock (_islock)
- {
- //var repository = LogManager.GetRepository();
- //var appenders = repository.GetAppenders();
- //var targetApder = appenders.First(p => p.Name.Contains(logType)) as log4net.Appender.RollingFileAppender;
- //if (targetApder == null) return null;
- //targetApder.File = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"logs\\");
- //targetApder.MaximumFileSize = "1KB";
- //targetApder.RollingStyle = RollingFileAppender.RollingMode.Composite;
- //targetApder.ActivateOptions();
- ILog logger = LogManager.GetLogger($"{logType}");
- return logger;
- }
- }
- catch (Exception x)
- {
- return LogManager.GetLogger("Default");
- }
- }
- public static void Error(object ex)
- {
- var logError = GetLog(nameof(LogType.Error));
- if (logError != null)
- logError.Error(ex);
- }
- public static void Info(string infoMsg)
- {
- var logInfo = GetLog(nameof(LogType.Info));
- if (logInfo != null)
- logInfo.Info(infoMsg);
- }
- public static void Debug(string degbug)
- {
- var logDebug = GetLog(nameof(LogType.Debug));
- if (logDebug != null)
- logDebug.Debug(degbug);
- }
- public static void Warn(string warn)
- {
- var logWarn = GetLog(nameof(LogType.Warn));
- if (logWarn != null)
- logWarn.Warn(warn);
- }
- public static void Write(string message)
- {
- lock (_isMqLock)
- {
- System.IO.FileInfo info = new FileInfo(fileLogPath);
- if (!info.Exists)
- System.IO.File.Create(fileLogPath).Close();
- FileStream fs = new FileStream(fileLogPath, FileMode.Append, FileAccess.Write);
- using (StreamWriter sw = new StreamWriter(fs))
- {
- sw.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]");
- sw.WriteLine(message);
- sw.WriteLine();
- sw.Flush();
- }
- fs.Close();
- }
- }
- public static IEnumerable<string> Read()
- {
- if (!File.Exists(fileLogPath)) yield break;
- using (FileStream fs = new FileStream(fileLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
- {
- using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
- {
- string logContext = sr.ReadToEnd();
- var matches = Regex.Matches(logContext.Replace("\r\n", ""), @"(\{).*?(\})", RegexOptions.IgnoreCase);
- foreach (Match m in matches) yield return m.Value;
- }
- }
- }
- public static void Clear()
- {
- if (File.Exists(fileLogPath))
- new FileStream(fileLogPath, FileMode.Truncate, FileAccess.ReadWrite).Close();
- }
- //private static void LogWrite()
- //{
- // Task.Run(() =>
- // {
- // while (true)
- // {
- // if (!LogQueue.Any())
- // {
- // Thread.Sleep(1000);
- // continue;
- // }
- // if (LogQueue.TryDequeue(out var data))
- // {
- // switch (data.logType)
- // {
- // case LogType.Error:
- // var logError = GetLog(nameof(LogType.Error));
- // if (logError != null)
- // logError.Error(data.msg);
- // break;
- // case LogType.Debug:
- // var logDebug = GetLog(nameof(LogType.Debug));
- // if (logDebug != null)
- // logDebug.Debug(data.msg);
- // break;
- // case LogType.Warn:
- // var logWarn = GetLog(nameof(LogType.Warn));
- // if (logWarn != null)
- // logWarn.Warn(data.msg);
- // break;
- // default:
- // var logInfo = GetLog(nameof(LogType.Info));
- // if (logInfo != null)
- // logInfo.Info(data.msg);
- // break;
- // }
- // }
- // }
- // });
- //}
- }
- public enum LogType
- {
- Info,
- Error,
- Trace,
- Debug,
- Fatal,
- Warn
- }
- }
|