Advertisement
wjcustode

The Ideal Logging System

Jul 23rd, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace LoggingSystem
  5. {
  6.     public enum NotificationLevel
  7.     {
  8.         Noise,
  9.         Warning,
  10.         Exception
  11.     }
  12.  
  13.     public class Notification
  14.     {
  15.         public DateTime TimeStamp { get; set; }
  16.  
  17.         public NotificationLevel Level { get; set; }
  18.  
  19.         public string Message { get; set; }
  20.  
  21.         public string StackTrace { get; set; }
  22.  
  23.         public Notification(Exception err)
  24.         {
  25.             TimeStamp = DateTime.Now;
  26.  
  27.             StackTrace = err.StackTrace;
  28.  
  29.             Level = NotificationLevel.Exception;
  30.  
  31.             Message = err.Message;
  32.         }
  33.  
  34.         public Notification(NotificationLevel level, string message)
  35.         {
  36.             TimeStamp = DateTime.Now;
  37.  
  38. #if DEBUG
  39.             StackTrace = new System.Diagnostics.StackTrace().ToString();
  40. #endif
  41.  
  42.             Level = level;
  43.  
  44.             Message = message;
  45.         }
  46.  
  47.         public override string ToString()
  48.         {
  49.             return string.Format("{0}: {1}", Level, Message);
  50.         }
  51.     }
  52.  
  53.     public interface INotificationSubscriber
  54.     {
  55.         void ReceiveNotification(Notification notification);
  56.     }
  57.  
  58.     public class LoggingService
  59.     {
  60.         public LoggingService()
  61.         {
  62.             _notifications = new List<Notification>();
  63.  
  64.             _subscribers = new List<INotificationSubscriber>();
  65.         }
  66.  
  67.         public void Notify(Exception err)
  68.         {
  69.             _notifications.Add(new Notification(err));
  70.         }
  71.  
  72.         public void Notify(NotificationLevel level, string format, params object[] args)
  73.         {
  74.             _notifications.Add(new Notification(level, string.Format(format, args)));
  75.         }
  76.  
  77.         public void Notify(string format, params object[] args)
  78.         {
  79.             _notifications.Add(new Notification(NotificationLevel.Noise, string.Format(format, args)));
  80.         }
  81.  
  82.         public void Subscribe(INotificationSubscriber subscriber)
  83.         {
  84.             _subscribers.Add(subscriber);
  85.         }
  86.  
  87.         public void Unsubscribe(INotificationSubscriber subscriber)
  88.         {
  89.             _subscribers.Remove(subscriber);
  90.         }
  91.  
  92.         protected void NotifySubscribers(Notification notification)
  93.         {
  94.             foreach (var subscriber in _subscribers)
  95.             {
  96.                 subscriber.ReceiveNotification(notification);
  97.             }
  98.         }
  99.  
  100.         private readonly List<INotificationSubscriber> _subscribers;
  101.  
  102.         private readonly List<Notification> _notifications;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement