Example #1
0
 public LogMessage(object source, LogEventCategory category, string message)
 {
     EventSource = source;
     Category = category;
     Message = message;
     Date = DateTime.Now;
 }
Example #2
0
 public LogMessage(LogEventCategory category, string message, Exception exception)
 {
     Category  = category;
     Message   = message;
     Date      = DateTime.Now;
     Exception = exception;
 }
Example #3
0
 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);
             }
         }
     }
 }
Example #4
0
        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);
                }
            }
        }
Example #5
0
        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);
                }
            }
        }
Example #6
0
        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);
                }
            }
        }
Example #7
0
        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;
                }
            }
        }
Example #8
0
        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();
            }
        }