Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EventSinkToSniffer.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Events;
7
9{
14 {
15 private readonly ISniffer sniffer;
16
22 : base(Guid.NewGuid().ToString())
23 {
24 this.sniffer = Sniffer;
25 }
26
30 public ISniffer Sniffer => this.sniffer;
31
36 public override Task Queue(Event Event)
37 {
38 StringBuilder sb = new StringBuilder();
39 bool First = true;
40
41 sb.Append(Event.Message);
42 this.Append(sb, "Object", Event.Object, ref First);
43 this.Append(sb, "Actor", Event.Actor, ref First);
44 this.Append(sb, "EventId", Event.EventId, ref First);
45 this.Append(sb, "Facility", Event.Facility, ref First);
46 this.Append(sb, "Module", Event.Module, ref First);
47 this.Append(sb, "StackTrace", Event.StackTrace, ref First);
48
49 if (!(Event.Tags is null))
50 {
51 foreach (KeyValuePair<string, object> P in Event.Tags)
52 this.Append(sb, P.Key, P.Value?.ToString(), ref First);
53 }
54
55 string Message = sb.ToString();
56
57 switch (Event.Type)
58 {
59 case EventType.Informational:
60 case EventType.Notice:
61 case EventType.Debug:
62 default:
63 this.sniffer.Information(Event.Timestamp, Message);
64 break;
65
66 case EventType.Warning:
67 this.sniffer.Warning(Event.Timestamp, Message);
68 break;
69
70 case EventType.Error:
71 this.sniffer.Error(Event.Timestamp, Message);
72 break;
73
74 case EventType.Alert:
75 case EventType.Critical:
76 case EventType.Emergency:
77 this.sniffer.Exception(Event.Timestamp, Message);
78 break;
79 }
80
81 return Task.CompletedTask;
82 }
83
84 private void Append(StringBuilder sb, string Key, string Value, ref bool First)
85 {
86 if (!string.IsNullOrEmpty(Value))
87 {
88 if (First)
89 {
90 First = false;
91 sb.AppendLine();
92 }
93
94 sb.AppendLine();
95 sb.Append(Key);
96 sb.Append(": ");
97 sb.Append(Value);
98 }
99 }
100
101 }
102}
Class representing an event.
Definition: Event.cs:11
string Message
Free-text event message.
Definition: Event.cs:132
EventType Type
Type of event.
Definition: Event.cs:122
string Object
Object related to the event.
Definition: Event.cs:137
string Actor
Actor responsible for the action causing the event.
Definition: Event.cs:142
string Module
Module where the event is reported.
Definition: Event.cs:157
DateTime Timestamp
Timestamp of event.
Definition: Event.cs:117
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
Definition: Event.cs:167
string EventId
Computer-readable Event ID identifying type of even.
Definition: Event.cs:147
string Facility
Facility can be either a facility in the network sense or in the system sense.
Definition: Event.cs:152
string StackTrace
Stack Trace of event.
Definition: Event.cs:162
Base class for event sinks.
Definition: EventSink.cs:9
Event sink that forwards logged events to a sniffer.
EventSinkToSniffer(ISniffer Sniffer)
Event sink that forwards logged events to a sniffer.
override Task Queue(Event Event)
Queues an event to be output.
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:10
EventType
Type of event.
Definition: EventType.cs:7