public LogMessage(object source, LogEventCategory category, string message) { EventSource = source; Category = category; Message = message; Date = DateTime.Now; }
public LogMessage(LogEventCategory category, string message, Exception exception) { Category = category; Message = message; Date = DateTime.Now; Exception = exception; }
public void Write(LogEventCategory category, string message, Exception exception = null) { lock (_mutex) { var msg = new LogMessage(category, message, exception); Trace(msg); foreach (var h in _handlers) { if ((h.Value & category) > 0) { h.Key(msg); } } } }
public void Subscribe(LogMessageHandler handler, LogEventCategory categories) { lock (_mutex) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } if (_handlers.ContainsKey(handler)) { _handlers[handler] |= categories; } else { _handlers.Add(handler, categories); } } }
public void UnSubscribe(LogMessageHandler handler, LogEventCategory categories) { lock (_mutex) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } if (!_handlers.ContainsKey(handler)) { return; } _handlers[handler] ^= categories; if (_handlers[handler] == 0) { _handlers.Remove(handler); } } }
public void Subscribe(object logSource, LogEventCategory categories, LogMessageHandler handler) { lock (_mutex) { if (handler == null) throw new ArgumentNullException("handler"); if (logSource == null) throw new ArgumentNullException("logSource"); if (!_handlers.ContainsKey(logSource)) _handlers.Add(logSource, new List<HandlerCategoryPair>()); var handlerCategoryPair = _handlers[logSource].Where(i => i.Handler == handler).FirstOrDefault(); if (handlerCategoryPair != null) handlerCategoryPair.Categories |= categories; else { handlerCategoryPair = new HandlerCategoryPair() {Categories = categories, Handler = handler}; _handlers[logSource].Add(handlerCategoryPair); } } }
public void UnSubscribe(object logSource, LogEventCategory categories, LogMessageHandler handler) { lock (_mutex) { if (handler == null) throw new ArgumentNullException("handler"); if (logSource == null) throw new ArgumentNullException("logSource"); if (!_handlers.ContainsKey(logSource)) return; var handlerCategoryPair = _handlers[logSource].Where(i => i.Handler == handler).FirstOrDefault(); if (handlerCategoryPair == null) return; var newCategories = handlerCategoryPair.Categories ^ categories; if (newCategories == 0) { _handlers[logSource].Remove(handlerCategoryPair); if (_handlers[logSource].Count == 0) _handlers.Remove(logSource); } else { handlerCategoryPair.Categories = newCategories; } } }
public void Trace(LogEventCategory category, string message) { lock (_mutex) { var now = DateTime.Now; var formatted = string.Format("[{0:D2}:{1:D2}:{2:D2}.{3:D3}] [{4}] {5}", now.Hour, now.Minute, now.Second, now.Millisecond, category, message); Console.WriteLine(formatted); _streamWriter.WriteLine(formatted); _streamWriter.Flush(); } }