Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace LoggingSystem
- {
- public enum NotificationLevel
- {
- Noise,
- Warning,
- Exception
- }
- public class Notification
- {
- public DateTime TimeStamp { get; set; }
- public NotificationLevel Level { get; set; }
- public string Message { get; set; }
- public string StackTrace { get; set; }
- public Notification(Exception err)
- {
- TimeStamp = DateTime.Now;
- StackTrace = err.StackTrace;
- Level = NotificationLevel.Exception;
- Message = err.Message;
- }
- public Notification(NotificationLevel level, string message)
- {
- TimeStamp = DateTime.Now;
- #if DEBUG
- StackTrace = new System.Diagnostics.StackTrace().ToString();
- #endif
- Level = level;
- Message = message;
- }
- public override string ToString()
- {
- return string.Format("{0}: {1}", Level, Message);
- }
- }
- public interface INotificationSubscriber
- {
- void ReceiveNotification(Notification notification);
- }
- public class LoggingService
- {
- public LoggingService()
- {
- _notifications = new List<Notification>();
- _subscribers = new List<INotificationSubscriber>();
- }
- public void Notify(Exception err)
- {
- _notifications.Add(new Notification(err));
- }
- public void Notify(NotificationLevel level, string format, params object[] args)
- {
- _notifications.Add(new Notification(level, string.Format(format, args)));
- }
- public void Notify(string format, params object[] args)
- {
- _notifications.Add(new Notification(NotificationLevel.Noise, string.Format(format, args)));
- }
- public void Subscribe(INotificationSubscriber subscriber)
- {
- _subscribers.Add(subscriber);
- }
- public void Unsubscribe(INotificationSubscriber subscriber)
- {
- _subscribers.Remove(subscriber);
- }
- protected void NotifySubscribers(Notification notification)
- {
- foreach (var subscriber in _subscribers)
- {
- subscriber.ReceiveNotification(notification);
- }
- }
- private readonly List<INotificationSubscriber> _subscribers;
- private readonly List<Notification> _notifications;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement