1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
using System;
using System.IO;
using System.ComponentModel;
namespace ViperBytes.IO
{
/// <summary>
/// Handles a log file
/// </summary>
[Description("Handles a log file")]
public class LogWriter : Component
{
#region delegates
/// <summary>
/// Provides a method, wich handles events from written messages
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
[Description("Provides a method, wich handles events from written messages")]
public delegate void LogWriterEventHandler(object sender, LogWriterEventArgs e);
#endregion
#region fields
/// <summary>
/// Stores the log save path
/// </summary>
private string path = String.Empty;
/// <summary>
/// Stores the file name format
/// </summary>
private string fileFormat = "log_{0:d}.log";
/// <summary>
/// Stores the message format
/// </summary>
private string messageFormat = "{0:t}: {1}";
#endregion
#region events
/// <summary>
/// Fired, when a message was written
/// </summary>
[Description("Fired, when a message was written")]
public event LogWriterEventHandler MessageWritten;
/// <summary>
/// Fired, when a new log file was createdFired, when a message was written
/// </summary>
[Description("Fired, when a new log file was createdFired, when a message was written")]
public event EventHandler FileCreated;
#endregion
#region properties
/// <summary>
/// Specifyes the log save path
/// </summary>
[Description("Specifyes the log save path")]
public string Path
{
get
{
return this.path;
}
set
{
this.path = value;
}
}
/// <summary>
/// Specifyes the file name format
/// </summary>
[Description("Specifyes the file name format")]
[DefaultValue("log_{0:d}.log")]
public string FileFormat
{
get
{
return this.fileFormat;
}
set
{
this.fileFormat = value;
}
}
/// <summary>
/// Specifyes the message format
/// </summary>
[Description("Specifyes the message format")]
[DefaultValue("{0:t}: {1}")]
public string MessageFormat
{
get
{
return this.messageFormat;
}
set
{
this.messageFormat = value;
}
}
#endregion
#region constructor
/// <summary>
/// Constructor
/// </summary>
public LogWriter()
{ }
/// <summary>
/// Constructor
/// </summary>
/// <param name="path">Specifyes the log save path</param>
public LogWriter(string path)
{
this.path = path;
}
#endregion
#region public members
/// <summary>
/// Writes a message
/// </summary>
/// <param name="message">Specifyes the message to be written</param>
/// <returns>Wheather the writing progress was successful</returns>
public bool WriteMessage(string message)
{
// if directory not exists, cancel
if (!Directory.Exists(this.path))
throw new DirectoryNotFoundException("The directory specifyed in Path was not found");
// holds the actual date and time
DateTime time = DateTime.Now;
// creates the file name
string fileName;
if (String.IsNullOrEmpty(this.path))
fileName = System.IO.Path.DirectorySeparatorChar.ToString();
else
{
fileName = this.path;
if (fileName[fileName.Length - 1] != System.IO.Path.DirectorySeparatorChar)
fileName += System.IO.Path.DirectorySeparatorChar;
}
try
{
fileName += String.Format(this.fileFormat, time);
}
catch (FormatException)
{
throw new FormatException("FileFormat has the wrong format.");
}
catch
{
return false;
}
// build message line
string line;
try
{
// creates the line
line = String.Format(this.messageFormat, new object[] { time, message });
}
catch (FormatException)
{
throw new FormatException("MessageFormat has the wrong format.");
}
catch
{
return false;
}
try
{
StreamWriter writer;
// open file
if (!File.Exists(fileName))
{
writer = new StreamWriter(fileName);
OnFileCreated(EventArgs.Empty);
}
else
writer = new StreamWriter(fileName, true);
// write message line
writer.WriteLine(line);
// close file
writer.Close();
OnMessageWritten(new LogWriterEventArgs(time, message));
return true;
}
catch
{
return false;
}
}
#endregion
#region protected members
/// <summary>
/// Fires the MessageWritten event
/// </summary>
/// <param name="e"></param>
protected void OnMessageWritten(LogWriterEventArgs e)
{
if (MessageWritten != null)
MessageWritten(this, e);
}
/// <summary>
/// Fires the FileCreated event
/// </summary>
/// <param name="e"></param>
protected void OnFileCreated(EventArgs e)
{
if (FileCreated != null)
FileCreated(this, e);
}
#endregion
}
#region LogWriterEventArgs class
/// <summary>
/// LogWriterEventArgs stores information about the sent log message
/// </summary>
public class LogWriterEventArgs : EventArgs
{
#region fields
/// <summary>
/// Stores the message post time
/// </summary>
private DateTime time;
/// <summary>
/// Stores the message
/// </summary>
private string message;
#endregion
#region properties
/// <summary>
/// Specifyes the message post time
/// </summary>
[Description("Specifyes the message post time")]
public DateTime Time
{
get
{
return this.time;
}
}
/// <summary>
/// Specifyes the message
/// </summary>
[Description("Specifyes the message")]
public string Message
{
get
{
return this.message;
}
}
#endregion
#region constructor
/// <summary>
/// Constructor
/// </summary>
/// <param name="time">Specifyes the message post time</param>
/// <param name="message">Specifyes the message</param>
public LogWriterEventArgs(DateTime time, string message)
: base()
{
this.time = time;
this.message = message;
}
#endregion
}
#endregion
}
|