comparison Logging/Logger.cs @ 0:961030992bd2

Initial commit of IBBoard libraries
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 11:13:48 +0000
parents
children 0352fa33ee8f
comparison
equal deleted inserted replaced
-1:000000000000 0:961030992bd2
1 using System;
2 using System.IO;
3 using System.Text;
4 using System.Collections.Generic;
5
6 namespace IBBoard.Logging
7 {
8 public enum LogLevel {Debug = 1, Info = 2, Warning = 3, Error = 4, Critical = 5}
9 /// <summary>
10 /// Summary description for Logger.
11 /// </summary>
12 public abstract class Logger : TextWriter
13 {
14
15 protected LogLevel logLevel;
16 protected Stream stream;
17 protected UTF8Encoding encoding = new UTF8Encoding();
18 private List<LogItem> logMessages = new List<LogItem>();
19
20 public delegate void LogUpdatedDelegate(LogItem item);
21 public event LogUpdatedDelegate LogUpdatedEvent;
22
23 protected Logger(Stream logStream)
24 {
25 logLevel = LogLevel.Error;
26 stream = logStream;
27
28 //null stream means we're not actually logging out so we don't need to check it is writable
29 if (stream!=null)
30 {
31 if (!stream.CanWrite)
32 {
33 throw new ArgumentException("Log stream was not writable");
34 }
35 }
36
37 LogMessageString("Log started at " + String.Format("{0:HH:mm:ss, yyyy-MM-dd}", DateTime.Now));
38 }
39
40 ~Logger()
41 {
42 LogMessageString("Log closed at " + String.Format("{0:HH:mm:ss, yyyy-MM-dd}", DateTime.Now));
43 }
44
45 public LogLevel LogLevel
46 {
47 get { return logLevel; }
48 set { logLevel = value; }
49 }
50
51 public void Log(Exception ex, LogLevel level)
52 {
53 Log(ex.Message, ex.StackTrace, level);
54 }
55
56 public void Log(string message, LogLevel level)
57 {
58 Log(message, "", level);
59 }
60
61 private void Log(string message, string stacktrace, LogLevel level)
62 {
63 if (level >= LogLevel)
64 {
65 LogItem item = new LogItem(level, message, stacktrace);
66 LogMessage(item);
67 logMessages.Add(item);
68 OnLogUpdated(item);
69 }
70 }
71
72 private void OnLogUpdated(LogItem item)
73 {
74 if (LogUpdatedEvent!=null)
75 {
76 LogUpdatedEvent(item);
77 }
78 }
79
80 protected abstract void LogMessage(LogItem item);
81 protected abstract void LogMessageString(string str);
82
83 public void ResetInternalLog()
84 {
85 logMessages = new List<IBBoard.Logging.LogItem>();
86 LogMessageString("Log reset at " + String.Format("{0:yyyy-MM-dd-HHmmss}", DateTime.Now));
87 }
88
89 public int LogLength
90 {
91 get { return logMessages.Count; }
92 }
93
94 public LogItem GetLogItem(int index)
95 {
96 if (index < LogLength)
97 {
98 return logMessages[index];
99 }
100 else
101 {
102 throw new IndexOutOfRangeException();
103 }
104 }
105
106 public LogItem[] GetLogItems(LogLevel minLogLevel)
107 {
108 List<LogItem> logItems = new List<LogItem>();
109
110 foreach (LogItem item in logMessages)
111 {
112 if (item.Level>=minLogLevel)
113 {
114 logItems.Add(item);
115 }
116 }
117
118 return logItems.ToArray();
119 }
120
121 //Allow the Logger to be used as the output point for System.Console.Error. In this case assume all messages are Error level
122 public override Encoding Encoding {
123 get { return encoding; }
124 }
125
126 public override void Write (object value)
127 {
128 if (value is Exception)
129 {
130 Log((Exception)value, LogLevel.Error);
131 }
132 }
133
134 public override void Write (string value)
135 {
136 Log(value, LogLevel.Error);
137 }
138
139 public override void WriteLine(object value)
140 {
141 Write(value);
142 }
143
144 public override void WriteLine (string value)
145 {
146 Write(value);
147 }
148
149 }
150 }