Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConsoleEventSink.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
6
8{
13 {
14 private const int TabWidth = 8;
15 private readonly bool beep;
16 private readonly bool includeStackTraces = false;
17 private bool consoleWidthWorks = true;
18
23 : this(true, false)
24 {
25 }
26
32 public ConsoleEventSink(bool Beep)
33 : this(Beep, false)
34 {
35 }
36
43 public ConsoleEventSink(bool Beep, bool IncludeStackTraces)
44 : base("Console Event Sink")
45 {
46 this.beep = Beep;
47 this.includeStackTraces = IncludeStackTraces;
48 }
49
51 public override Task Queue(Event Event)
52 {
53 ConsoleColor? ForegroundColor;
54 ConsoleColor? BackgroundColor;
55 int Width = 80;
56 StringBuilder sb = new StringBuilder();
57 bool WriteLine = true;
58 int i;
59
60 if (this.consoleWidthWorks)
61 {
62 try
63 {
64 Width = ConsoleOut.WindowWidth;
65 WriteLine = Width > 0;
66
67 if (!WriteLine)
68 Width = 80;
69 }
70 catch (Exception)
71 {
72 this.consoleWidthWorks = false;
73 }
74 }
75
76 switch (Event.Type)
77 {
78 case EventType.Debug:
79 ForegroundColor = ConsoleColor.White;
80 BackgroundColor = ConsoleColor.DarkBlue;
81 break;
82
83 case EventType.Informational:
84 ForegroundColor = ConsoleColor.Green;
85 BackgroundColor = ConsoleColor.Black;
86 break;
87
88 case EventType.Notice:
89 ForegroundColor = ConsoleColor.White;
90 BackgroundColor = ConsoleColor.Black;
91 break;
92
93 case EventType.Warning:
94 ForegroundColor = ConsoleColor.Yellow;
95 BackgroundColor = ConsoleColor.Black;
96 break;
97
98 case EventType.Error:
99 ForegroundColor = ConsoleColor.Red;
100 BackgroundColor = ConsoleColor.Black;
101 break;
102
103 case EventType.Critical:
104 ForegroundColor = ConsoleColor.Yellow;
105 BackgroundColor = ConsoleColor.Red;
106
107 if (this.beep)
109 break;
110
111 case EventType.Alert:
112 ForegroundColor = ConsoleColor.Yellow;
113 BackgroundColor = ConsoleColor.DarkRed;
114
115 if (this.beep)
117 break;
118
119 case EventType.Emergency:
120 ForegroundColor = ConsoleColor.White;
121 BackgroundColor = ConsoleColor.Magenta;
122
123 if (this.beep)
125 break;
126
127 default:
128 ForegroundColor = null;
129 BackgroundColor = null;
130 break;
131 }
132
133 if (!string.IsNullOrEmpty(Event.EventId))
134 {
135 sb.Append(Event.EventId);
136 sb.Append(": ");
137 }
138
139 if (Event.Message.IndexOf('\t') >= 0)
140 {
141 string[] Parts = Event.Message.Split('\t');
142 bool First = true;
143
144 foreach (string Part in Parts)
145 {
146 if (First)
147 First = false;
148 else
149 {
150 i = sb.ToString().Length % TabWidth;
151 sb.Append(new string(' ', TabWidth - i));
152 }
153
154 sb.Append(Part);
155 }
156 }
157 else
158 sb.Append(Event.Message);
159
160 if (WriteLine)
161 sb.AppendLine();
162 else
163 {
164 i = sb.ToString().Length % Width;
165 if (i > 0)
166 sb.Append(new string(' ', Width - i));
167 }
168
169 if (this.includeStackTraces && !string.IsNullOrEmpty(Event.StackTrace))
170 {
171 sb.Append(Event.StackTrace);
172
173 if (WriteLine)
174 sb.AppendLine();
175 else
176 {
177 i = sb.ToString().Length % Width;
178 if (i > 0)
179 sb.Append(new string(' ', Width - i));
180 }
181 }
182
183 sb.Append(" ");
184
185 this.AddTag(sb, Width, "Timestamp", Event.Timestamp.ToString(), true, WriteLine);
186 this.AddTag(sb, Width, "Level", Event.Level.ToString(), false, WriteLine);
187
188 if (!string.IsNullOrEmpty(Event.Object))
189 this.AddTag(sb, Width, "Object", Event.Object, false, WriteLine);
190
191 if (!string.IsNullOrEmpty(Event.Actor))
192 this.AddTag(sb, Width, "Actor", Event.Actor, false, WriteLine);
193
194 if (!string.IsNullOrEmpty(Event.Facility))
195 this.AddTag(sb, Width, "Facility", Event.Facility, false, WriteLine);
196
197 if (!string.IsNullOrEmpty(Event.Module))
198 this.AddTag(sb, Width, "Module", Event.Module, false, WriteLine);
199
200 if (!(Event.Tags is null) && Event.Tags.Length > 0)
201 {
202 foreach (KeyValuePair<string, object> P in Event.Tags)
203 {
204 if (P.Value is Array A)
205 {
206 StringBuilder sb2 = new StringBuilder();
207 bool First = true;
208
209 foreach (object Item in A)
210 {
211 if (First)
212 First = false;
213 else
214 sb2.Append(", ");
215
216 sb2.Append(Item.ToString());
217 }
218
219 this.AddTag(sb, Width, P.Key, sb2.ToString(), false, WriteLine);
220 }
221 else
222 this.AddTag(sb, Width, P.Key, P.Value, false, WriteLine);
223 }
224 }
225
226 if (WriteLine)
227 sb.AppendLine();
228 else
229 {
230 i = sb.ToString().Length % Width;
231 if (i > 0)
232 sb.Append(new string(' ', Width - i));
233 }
234
235 ConsoleOut.Write((Output) =>
236 {
237 Output.WriteLine(sb.ToString());
238 }, ForegroundColor, BackgroundColor);
239
240 return Task.CompletedTask;
241 }
242
243 private void AddTag(StringBuilder sb, int Width, string Key, object Value, bool First, bool WriteLine)
244 {
245 string ValueStr = Value is null ? string.Empty : Value.ToString();
246
247 if (Width > 0)
248 {
249 int i = sb.ToString().Length % Width;
250
251 if (i + Key.Length + 1 + ValueStr.Length + (First ? 0 : 2) > Width)
252 {
253 if (WriteLine)
254 {
255 sb.AppendLine();
256 sb.Append(" ");
257 }
258 else
259 sb.Append(new string(' ', Width - i + 3));
260
261 First = true;
262 }
263 }
264
265 if (!First)
266 sb.Append(", ");
267 else if (Width <= 0)
268 sb.Append(' ');
269
270 sb.Append(Key);
271 sb.Append('=');
272 sb.Append(Value);
273 }
274
275 }
276}
Outputs events to the console standard output.
ConsoleEventSink(bool Beep, bool IncludeStackTraces)
Outputs events to the console standard output.
ConsoleEventSink()
Outputs events to the console standard output.
ConsoleEventSink(bool Beep)
Outputs events to the console standard output.
override Task Queue(Event Event)
Queues an event to be output.
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
Serializes output to System.Console.Out, and assures modules are not dead-locked in case the Console ...
Definition: ConsoleOut.cs:26
static void Write(string value)
Queues a value to be written to the console output.
Definition: ConsoleOut.cs:110
static void Beep()
Emits a Beep sounds.
Definition: ConsoleOut.cs:586
static int WindowWidth
Width of window.
Definition: ConsoleOut.cs:48
EventType
Type of event.
Definition: EventType.cs:7