NTP Analyzer  0.8.2
Analyze the operation of time servers
EventLog.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Diagnostics;
24 using System.Security;
25 
26 namespace Ntp.Common.Log
27 {
28  public sealed class EventLog : LogBase
29  {
30  public EventLog(string name, Severity threshold)
31  : base(threshold, true)
32  {
33  this.name = name;
34  }
35 
36  private const string Log = "Application";
37  private static bool initialized;
38  private readonly string name;
39 
40  public override void Close()
41  {
42  }
43 
44  public override void Initialize()
45  {
46  if (initialized)
47  return;
48 
49  CreateEventLog();
50  initialized = true;
51  }
52 
53  public override void Resume()
54  {
55  // EventLog does not support resume
56  }
57 
58  public override void Suspend()
59  {
60  // EventLog does not support suspend
61  }
62 
63  public override void WriteLine(string text, Severity severity)
64  {
65  if (!initialized)
66  Initialize();
67 
68  if (severity < Threshold)
69  return;
70 
71  WriteEventLogEntry(text, GetEventLogEntryType(severity), 0);
72  }
73 
74  public override void WriteLine(Exception exception, Severity severity)
75  {
76  if (!initialized)
77  Initialize();
78 
79  if (severity < Threshold)
80  return;
81 
82  if (!initialized)
83  Initialize();
84 
85  if (severity < Threshold)
86  return;
87 
88  WriteEventLogEntry(exception.ToString(), GetEventLogEntryType(severity), 0);
89  }
90 
91  public override void WriteLine(Exception exception)
92  {
93  WriteEventLogEntry(exception.ToString(), EventLogEntryType.Error, 1);
94  }
95 
96  #region EventLog Implementation
97 
98  private void CreateEventLog()
99  {
100  try
101  {
102  if (!global::System.Diagnostics.EventLog.SourceExists(name))
103  {
104  global::System.Diagnostics.EventLog.CreateEventSource(name, Log);
105  }
106  }
107  catch (SecurityException)
108  {
109  }
110  }
111 
112  private static EventLogEntryType GetEventLogEntryType(Severity severity)
113  {
114  switch (severity)
115  {
116  case Severity.Trace:
117  case Severity.Debug:
118  case Severity.Info:
119  case Severity.Notice:
120  return EventLogEntryType.Information;
121  case Severity.Warn:
122  return EventLogEntryType.Warning;
123  case Severity.Error:
124  return EventLogEntryType.Error;
125  default:
126  return EventLogEntryType.Error;
127  }
128  }
129 
130  private void WriteEventLogEntry(string text, EventLogEntryType type, int code)
131  {
132  try
133  {
134  global::System.Diagnostics.EventLog.WriteEntry(name, text, type, code);
135  }
136  catch (SecurityException)
137  {
138  }
139  }
140 
141  #endregion
142  }
143 }
static EventLogEntryType GetEventLogEntryType(Severity severity)
Definition: EventLog.cs:112
override void Close()
Definition: EventLog.cs:40
override void WriteLine(Exception exception)
Definition: EventLog.cs:91
override void Resume()
Definition: EventLog.cs:53
EventLog(string name, Severity threshold)
Definition: EventLog.cs:30
void WriteEventLogEntry(string text, EventLogEntryType type, int code)
Definition: EventLog.cs:130
static bool initialized
Definition: EventLog.cs:37
override void WriteLine(Exception exception, Severity severity)
Definition: EventLog.cs:74
readonly string name
Definition: EventLog.cs:38
override void WriteLine(string text, Severity severity)
Definition: EventLog.cs:63
override void Initialize()
Definition: EventLog.cs:44
override void Suspend()
Definition: EventLog.cs:58