ProjectArcade
117 строк · 3.8 Кб
1using System;2using System.Threading.Tasks;3
4namespace DokanNet.Logging5{
6/// <summary>7/// Log to the console.8/// </summary>9public class ConsoleLogger : ILogger, IDisposable10{11private readonly string _loggerName;12private readonly System.Collections.Concurrent.BlockingCollection<Tuple<String, ConsoleColor>> _PendingLogs13= new System.Collections.Concurrent.BlockingCollection<Tuple<String, ConsoleColor>>();14
15private readonly Task _WriterTask = null;16private bool _disposed;17
18/// <summary>19/// Initializes a new instance of the <see cref="ConsoleLogger"/> class.20/// </summary>21/// <param name="loggerName">Optional name to be added to each log line.</param>22public ConsoleLogger(string loggerName = "")23{24_loggerName = loggerName;25_WriterTask = Task.Factory.StartNew(() =>26{27foreach (var tuple in _PendingLogs.GetConsumingEnumerable())28{29WriteMessage(tuple.Item1, tuple.Item2);30}31});32}33
34/// <inheritdoc />35public bool DebugEnabled { get { return true; } }36
37/// <inheritdoc />38public void Debug(string message, params object[] args)39{40EnqueueMessage(Console.ForegroundColor, message, args);41}42
43/// <inheritdoc />44public void Info(string message, params object[] args)45{46EnqueueMessage(Console.ForegroundColor, message, args);47}48
49/// <inheritdoc />50public void Warn(string message, params object[] args)51{52EnqueueMessage(ConsoleColor.DarkYellow, message, args);53}54
55/// <inheritdoc />56public void Error(string message, params object[] args)57{58EnqueueMessage(ConsoleColor.Red, message, args);59}60
61/// <inheritdoc />62public void Fatal(string message, params object[] args)63{64EnqueueMessage(ConsoleColor.Red, message, args);65}66
67private void EnqueueMessage(ConsoleColor newColor, string message, params object[] args)68{69if (args.Length > 0)70message = string.Format(message, args);71
72_PendingLogs.Add(Tuple.Create(message, newColor));73}74
75private void WriteMessage(string message, ConsoleColor newColor)76{77lock (Console.Out) // we only need this lock because we want to have more than one logger in this version78{79var origForegroundColor = Console.ForegroundColor;80Console.ForegroundColor = newColor;81Console.WriteLine(message.FormatMessageForLogging(true, loggerName: _loggerName));82Console.ForegroundColor = origForegroundColor;83}84}85
86protected virtual void Dispose(bool disposing)87{88if (!_disposed)89{90if (disposing)91{92// dispose managed state (managed objects)93_PendingLogs.CompleteAdding();94_WriterTask.Wait();95}96
97// free unmanaged resources (unmanaged objects) and override finalizer98// set large fields to null99_disposed = true;100}101}102
103// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources104// ~ConsoleLogger()105// {106// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method107// Dispose(disposing: false);108// }109
110public void Dispose()111{112// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method113Dispose(disposing: true);114GC.SuppressFinalize(this);115}116}117}