Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WindowsEventLog.cs
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Runtime.InteropServices;
5using System.Text;
6using System.Threading.Tasks;
7
9{
14 {
15 private IntPtr eventLog = IntPtr.Zero;
16
24 public WindowsEventLog(string Source)
25 : this(Source, null)
26 {
27 }
28
37 public WindowsEventLog(string Source, string MachineName)
38 : base("Windows Event Log")
39 {
40 try
41 {
42 this.eventLog = Win32.RegisterEventSourceW(MachineName, Source);
43 if (this.eventLog == IntPtr.Zero)
44 {
45 this.eventLog = Win32.RegisterEventSourceW(MachineName, "Application");
46 if (this.eventLog == IntPtr.Zero)
47 throw new Win32Exception(Marshal.GetLastWin32Error());
48 }
49 }
50 catch (DllNotFoundException ex)
51 {
52 throw new PlatformNotSupportedException("Win32 APIs not found.", ex);
53 }
54 }
55
59 public override void Dispose()
60 {
61 base.Dispose();
62
63 if (this.eventLog != IntPtr.Zero)
64 {
65 Win32.DeregisterEventSource(this.eventLog);
66 this.eventLog = IntPtr.Zero;
67 }
68 }
69
71 public override Task Queue(Event Event)
72 {
74 uint EventId; // https://msdn.microsoft.com/en-us/library/aa363651(v=vs.85).aspx
75
76 switch (Event.Type)
77 {
78 case EventType.Alert:
79 case EventType.Critical:
80 case EventType.Emergency:
81 case EventType.Error:
82 Type = WindowsEventType.EVENTLOG_ERROR_TYPE;
83 EventId = 0b11100000000000000000000000000000;
84 break;
85
86 case EventType.Notice:
87 case EventType.Warning:
88 Type = WindowsEventType.EVENTLOG_WARNING_TYPE;
89 EventId = 0b10100000000000000000000000000000;
90 break;
91
92 case EventType.Debug:
93 case EventType.Informational:
94 default:
95 Type = WindowsEventType.EVENTLOG_INFORMATION_TYPE;
96 EventId = 0b01100000000000000000000000000000;
97 break;
98 }
99
100 // TODO: Identify successful and failed logins.
101
102 StringBuilder Message = new StringBuilder(Event.Message);
103 Message.AppendLine();
104 Message.AppendLine();
105
106 Message.Append("Timestamp: ");
107 Message.AppendLine(Event.Timestamp.ToString());
108 Message.Append("Type: ");
109 Message.AppendLine(Event.Type.ToString());
110 Message.Append("Level: ");
111 Message.AppendLine(Event.Level.ToString());
112
113 if (!string.IsNullOrEmpty(Event.EventId))
114 {
115 Message.Append("Event ID: ");
116 Message.AppendLine(Event.EventId);
117 }
118
119 if (!string.IsNullOrEmpty(Event.Object))
120 {
121 Message.Append("Object: ");
122 Message.AppendLine(Event.Object);
123 }
124
125 if (!string.IsNullOrEmpty(Event.Object))
126 {
127 Message.Append("Object: ");
128 Message.AppendLine(Event.Object);
129 }
130
131 if (!string.IsNullOrEmpty(Event.Actor))
132 {
133 Message.Append("Actor: ");
134 Message.AppendLine(Event.Actor);
135 }
136
137 if (!string.IsNullOrEmpty(Event.Module))
138 {
139 Message.Append("Module: ");
140 Message.AppendLine(Event.Module);
141 }
142
143 if (!string.IsNullOrEmpty(Event.Facility))
144 {
145 Message.Append("Facility: ");
146 Message.AppendLine(Event.Facility);
147 }
148
149 if (!(Event.Tags is null) && Event.Tags.Length > 0)
150 {
151 foreach (KeyValuePair<string, object> Tag in Event.Tags)
152 {
153 Message.AppendLine(Tag.Key);
154 Message.Append(": ");
155
156 if (!(Tag.Value is null))
157 Message.AppendLine(Tag.Value.ToString());
158 }
159 }
160
161 if (Event.Type >= EventType.Critical && !string.IsNullOrEmpty(Event.StackTrace))
162 {
163 Message.AppendLine();
164 Message.AppendLine("Stack Trace:");
165 Message.AppendLine(Event.StackTrace);
166 }
167
168 string s = Message.ToString();
169 List<string> Strings = new List<string>();
170 int i = 0;
171 int c = s.Length;
172
173 while (i < c)
174 {
175 if (c - i > 30000)
176 {
177 Strings.Add(s.Substring(i, 30000));
178 i += 30000;
179 }
180 else
181 {
182 Strings.Add(s.Substring(i));
183 i = c;
184 }
185 }
186
187 if (!Win32.ReportEventW(this.eventLog, Type, 0, EventId, IntPtr.Zero, 1, 0, Strings.ToArray(), IntPtr.Zero))
188 throw new Win32Exception(Marshal.GetLastWin32Error());
189
190 return Task.CompletedTask;
191 }
192 }
193}
Class representing an event.
Definition: Event.cs:10
string Message
Free-text event message.
Definition: Event.cs:131
EventType Type
Type of event.
Definition: Event.cs:121
string Object
Object related to the event.
Definition: Event.cs:136
EventLevel Level
Event Level.
Definition: Event.cs:126
string Actor
Actor responsible for the action causing the event.
Definition: Event.cs:141
string Module
Module where the event is reported.
Definition: Event.cs:156
DateTime Timestamp
Timestamp of event.
Definition: Event.cs:116
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
Definition: Event.cs:166
string EventId
Computer-readable Event ID identifying type of even.
Definition: Event.cs:146
string Facility
Facility can be either a facility in the network sense or in the system sense.
Definition: Event.cs:151
string StackTrace
Stack Trace of event.
Definition: Event.cs:161
Base class for event sinks.
Definition: EventSink.cs:9
Handles interaction with the Windows Event Log API.
Definition: Win32.cs:77
Defines an event sink that logs incoming events to a Windows Event Log.
override void Dispose()
IDisposable.Dispose()
WindowsEventLog(string Source)
Defines an event sink that logs incoming events to a Windows Event Log.
override Task Queue(Event Event)
Queues an event to be output.
WindowsEventLog(string Source, string MachineName)
Defines an event sink that logs incoming events to a Windows Event Log.
WindowsEventType
Windows event type enumeration.
Definition: Win32.cs:11
EventType
Type of event.
Definition: EventType.cs:7