-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventLog.cs
50 lines (45 loc) · 1.59 KB
/
EventLog.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OIMNTFS_Service
{
class EventLog
{
private System.Diagnostics.EventLog eventLog;
private string logName = "OIMNTFSServiceLog";
private int eventID = 1;
private string entry;
public EventLog(string source)
{
eventLog = new System.Diagnostics.EventLog(logName);
if (!System.Diagnostics.EventLog.SourceExists(source))
{
System.Diagnostics.EventLog.CreateEventSource(source, logName);
}
eventLog.Source = source;
eventLog.Log = logName;
entry = "";
Write("Log {0} started.", source);
}
public void Write(string format, params object[] args)
{
Flush();
var message = "\n>>> " + (args.Length == 0 ? format : string.Format(format, args)) + "\n";
eventLog.WriteEntry(message, System.Diagnostics.EventLogEntryType.Information, eventID++);
}
public void Buffer(string format, params object[] args)
{
entry += DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " - " + (args.Length == 0 ? format : string.Format(format, args)) + "\n";
}
public void Flush()
{
if (entry.Length > 0)
{
eventLog.WriteEntry(entry, System.Diagnostics.EventLogEntryType.Information, eventID++);
entry = "";
}
}
}
}