Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TextWriterEventSink.cs
1using System;
3using System.IO;
4using System.Threading.Tasks;
5
6namespace Waher.Events.Files
7{
11 public class TextWriterEventSink : EventSink, IDisposable
12 {
16 protected TextWriter output;
17
18 private readonly object synchObject = new object();
19 private bool disposed = false;
20
26 public TextWriterEventSink(string ObjectID, TextWriter Output)
27 : base(ObjectID)
28 {
29 this.output = Output;
30 }
31
35 protected virtual void BeforeWrite()
36 {
37 // Do nothing by default.
38 }
39
43 protected virtual void AfterWrite()
44 {
45 // Do nothing by default.
46 }
47
51 public override Task DisposeAsync()
52 {
53 this.disposed = true;
54
55 this.DisposeOutput();
56
57 return base.DisposeAsync();
58 }
59
64 public override Task Queue(Event Event)
65 {
66 if (this.disposed)
67 return Task.CompletedTask;
68
69 lock (this.synchObject)
70 {
71 this.BeforeWrite();
72 try
73 {
74 if (!(this.output is null))
75 {
76 this.output.Write(Event.Timestamp.ToString("d"));
77 this.output.Write(", ");
78 this.output.Write(Event.Timestamp.ToString("T"));
79 this.output.Write('\t');
80 this.output.Write(Event.Type.ToString());
81 this.output.Write('\t');
82 this.output.Write(Event.Level.ToString());
83
84 if (!string.IsNullOrEmpty(Event.EventId))
85 {
86 this.output.Write('\t');
87 this.output.Write(Event.EventId);
88 }
89
90 if (!string.IsNullOrEmpty(Event.Object))
91 {
92 this.output.Write('\t');
93 this.output.Write(Event.Object);
94 }
95
96 if (!string.IsNullOrEmpty(Event.Actor))
97 {
98 this.output.Write('\t');
99 this.output.Write(Event.Actor);
100 }
101
102 this.output.WriteLine("\r\n");
103
104 if (!string.IsNullOrEmpty(Event.Module))
105 {
106 this.output.Write('\t');
107 this.output.Write(Event.Module);
108 }
109
110 if (!string.IsNullOrEmpty(Event.Facility))
111 {
112 this.output.Write('\t');
113 this.output.Write(Event.Facility);
114 }
115
116 if (!(Event.Tags is null) && Event.Tags.Length > 0)
117 {
118 this.output.WriteLine("\r\n");
119
120 foreach (KeyValuePair<string, object> Tag in Event.Tags)
121 {
122 this.output.Write('\t');
123 this.output.Write(Tag.Key);
124 this.output.Write('=');
125
126 if (!(Tag.Value is null))
127 this.output.Write(Tag.Value.ToString());
128 }
129 }
130
131 this.output.WriteLine("\r\n");
132 this.output.WriteLine(Event.Message);
133
134 if (Event.Type >= EventType.Critical && !string.IsNullOrEmpty(Event.StackTrace))
135 {
136 this.output.WriteLine("\r\n");
137 this.output.WriteLine(Event.StackTrace);
138 }
139
140 this.output.Flush();
141 }
142 }
143 catch (Exception)
144 {
145 try
146 {
147 this.DisposeOutput();
148 }
149 catch (Exception)
150 {
151 // Ignore
152 }
153 }
154 finally
155 {
156 this.AfterWrite();
157 }
158 }
159
160 return Task.CompletedTask;
161 }
162
166 public virtual bool CanDisposeOutput => true;
167
171 public virtual void DisposeOutput()
172 {
173 if (this.CanDisposeOutput)
174 {
175 this.output?.Flush();
176 this.output?.Dispose();
177 }
178
179 this.output = null;
180 }
181 }
182}
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
EventLevel Level
Event Level.
Definition: Event.cs:127
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
Outputs sniffed data to a text writer.
virtual void DisposeOutput()
Disposes of the current output.
virtual void BeforeWrite()
Method is called before writing something to the text file.
virtual void AfterWrite()
Method is called after writing something to the text file.
TextWriterEventSink(string ObjectID, TextWriter Output)
Outputs sniffed data to a text writer.
TextWriter output
Text writer object.
override Task DisposeAsync()
IDisposableAsync.DisposeAsync
override Task Queue(Event Event)
Queues an event to be output.
virtual bool CanDisposeOutput
If output can be disposed.
virtual string ObjectID
Object ID, used when logging events.
Definition: LogObject.cs:26
EventType
Type of event.
Definition: EventType.cs:7