2using System.Collections.Generic;
4using System.Threading.Tasks;
14 private const int TabWidth = 8;
15 private readonly
bool beep;
16 private readonly
bool includeStackTraces =
false;
17 private bool consoleWidthWorks =
true;
44 : base(
"Console Event Sink")
47 this.includeStackTraces = IncludeStackTraces;
53 ConsoleColor? ForegroundColor;
54 ConsoleColor? BackgroundColor;
56 StringBuilder sb =
new StringBuilder();
57 bool WriteLine =
true;
60 if (this.consoleWidthWorks)
65 WriteLine = Width > 0;
72 this.consoleWidthWorks =
false;
79 ForegroundColor = ConsoleColor.White;
80 BackgroundColor = ConsoleColor.DarkBlue;
84 ForegroundColor = ConsoleColor.Green;
85 BackgroundColor = ConsoleColor.Black;
89 ForegroundColor = ConsoleColor.White;
90 BackgroundColor = ConsoleColor.Black;
94 ForegroundColor = ConsoleColor.Yellow;
95 BackgroundColor = ConsoleColor.Black;
99 ForegroundColor = ConsoleColor.Red;
100 BackgroundColor = ConsoleColor.Black;
104 ForegroundColor = ConsoleColor.Yellow;
105 BackgroundColor = ConsoleColor.Red;
112 ForegroundColor = ConsoleColor.Yellow;
113 BackgroundColor = ConsoleColor.DarkRed;
120 ForegroundColor = ConsoleColor.White;
121 BackgroundColor = ConsoleColor.Magenta;
128 ForegroundColor =
null;
129 BackgroundColor =
null;
144 foreach (
string Part
in Parts)
150 i = sb.ToString().Length % TabWidth;
151 sb.Append(
new string(
' ', TabWidth - i));
164 i = sb.ToString().Length % Width;
166 sb.Append(
new string(
' ', Width - i));
169 if (this.includeStackTraces && !
string.IsNullOrEmpty(
Event.
StackTrace))
177 i = sb.ToString().Length % Width;
179 sb.Append(
new string(
' ', Width - i));
185 this.AddTag(sb, Width,
"Timestamp",
Event.
Timestamp.ToString(),
true, WriteLine);
186 this.AddTag(sb, Width,
"Level",
Event.
Level.ToString(),
false, WriteLine);
189 this.AddTag(sb, Width,
"Object",
Event.
Object,
false, WriteLine);
192 this.AddTag(sb, Width,
"Actor",
Event.
Actor,
false, WriteLine);
195 this.AddTag(sb, Width,
"Facility",
Event.
Facility,
false, WriteLine);
198 this.AddTag(sb, Width,
"Module",
Event.
Module,
false, WriteLine);
202 foreach (KeyValuePair<string, object> P
in Event.
Tags)
204 if (P.Value is Array A)
206 StringBuilder sb2 =
new StringBuilder();
209 foreach (
object Item
in A)
216 sb2.Append(Item.ToString());
219 this.AddTag(sb, Width, P.Key, sb2.ToString(),
false, WriteLine);
222 this.AddTag(sb, Width, P.Key, P.Value,
false, WriteLine);
230 i = sb.ToString().Length % Width;
232 sb.Append(
new string(
' ', Width - i));
237 Output.WriteLine(sb.ToString());
238 }, ForegroundColor, BackgroundColor);
240 return Task.CompletedTask;
243 private void AddTag(StringBuilder sb,
int Width,
string Key,
object Value,
bool First,
bool WriteLine)
245 string ValueStr = Value is
null ? string.Empty : Value.ToString();
249 int i = sb.ToString().Length % Width;
251 if (i + Key.Length + 1 + ValueStr.Length + (First ? 0 : 2) > Width)
259 sb.Append(
new string(
' ', Width - i + 3));
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.
string Message
Free-text event message.
EventType Type
Type of event.
string Object
Object related to the event.
EventLevel Level
Event Level.
string Actor
Actor responsible for the action causing the event.
string Module
Module where the event is reported.
DateTime Timestamp
Timestamp of event.
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
string EventId
Computer-readable Event ID identifying type of even.
string Facility
Facility can be either a facility in the network sense or in the system sense.
string StackTrace
Stack Trace of event.
Base class for event sinks.
Serializes output to System.Console.Out, and assures modules are not dead-locked in case the Console ...
static void Write(string value)
Queues a value to be written to the console output.
static void Beep()
Emits a Beep sounds.
static int WindowWidth
Width of window.