-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceLogEvent.cs
93 lines (77 loc) · 2.59 KB
/
ServiceLogEvent.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
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
using System;
using System.Diagnostics;
namespace CustomWatchdog
{
internal class ServiceLogEvent : IServiceLog
{
internal static IServiceLog Create(RecoveryConfigLog logDef)
{
var source = logDef.Path;
if (!EventLog.SourceExists(source))
EventLog.CreateEventSource(source, "Application");
return new ServiceLogEvent(new StringSourceProvider(source))
{
Level = logDef.Enums.Level
};
}
private readonly SourceProvider m_provider;
public ServiceLogEvent(CustomBatchWatchdog service)
: this(new ServiceSourceProvider(service))
{
Level = ServiceLogLevel.Info;
}
private ServiceLogEvent(SourceProvider provider)
{
m_provider = provider;
}
public string Source { get { return m_provider.Source; } }
public ServiceLogLevel Level { get; set; } = ServiceLogLevel.Info;
public void Write(ServiceLogLevel level, string evt)
{
switch (level)
{
case ServiceLogLevel.Info:
EventLog.WriteEntry(Source, evt, EventLogEntryType.Information, 0x03);
break;
case ServiceLogLevel.Warning:
EventLog.WriteEntry(Source, evt, EventLogEntryType.Warning, 0x01);
break;
case ServiceLogLevel.Error:
EventLog.WriteEntry(Source, evt, EventLogEntryType.Error, 0x02);
break;
case ServiceLogLevel.Trace:
case ServiceLogLevel.Debug:
default:
break;
}
}
public void Dispose()
{
}
public override string ToString()
{
return $"[Event][{Level} {Source}";
}
private abstract class SourceProvider
{
public abstract string Source { get; }
}
private class ServiceSourceProvider : SourceProvider
{
private readonly CustomBatchWatchdog m_service;
public ServiceSourceProvider(CustomBatchWatchdog service)
{
m_service = service;
}
public override string Source => m_service.EventLogSource;
}
private class StringSourceProvider : SourceProvider
{
public StringSourceProvider(string src)
{
Source = src;
}
public override string Source { get; }
}
}
}