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;
2using System.Collections.Generic;
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 void Dispose()
52 {
53 this.disposed = true;
54
55 this.DisposeOutput();
56 base.Dispose();
57 }
58
63 public override Task Queue(Event Event)
64 {
65 if (this.disposed)
66 return Task.CompletedTask;
67
68 lock (this.synchObject)
69 {
70 this.BeforeWrite();
71 try
72 {
73 if (!(this.output is null))
74 {
75 this.output.Write(Event.Timestamp.ToString("d"));
76 this.output.Write(", ");
77 this.output.Write(Event.Timestamp.ToString("T"));
78 this.output.Write('\t');
79 this.output.Write(Event.Type.ToString());
80 this.output.Write('\t');
81 this.output.Write(Event.Level.ToString());
82
83 if (!string.IsNullOrEmpty(Event.EventId))
84 {
85 this.output.Write('\t');
86 this.output.Write(Event.EventId);
87 }
88
89 if (!string.IsNullOrEmpty(Event.Object))
90 {
91 this.output.Write('\t');
92 this.output.Write(Event.Object);
93 }
94
95 if (!string.IsNullOrEmpty(Event.Actor))
96 {
97 this.output.Write('\t');
98 this.output.Write(Event.Actor);
99 }
100
101 this.output.WriteLine("\r\n");
102
103 if (!string.IsNullOrEmpty(Event.Module))
104 {
105 this.output.Write('\t');
106 this.output.Write(Event.Module);
107 }
108
109 if (!string.IsNullOrEmpty(Event.Facility))
110 {
111 this.output.Write('\t');
112 this.output.Write(Event.Facility);
113 }
114
115 if (!(Event.Tags is null) && Event.Tags.Length > 0)
116 {
117 this.output.WriteLine("\r\n");
118
119 foreach (KeyValuePair<string, object> Tag in Event.Tags)
120 {
121 this.output.Write('\t');
122 this.output.Write(Tag.Key);
123 this.output.Write('=');
124
125 if (!(Tag.Value is null))
126 this.output.Write(Tag.Value.ToString());
127 }
128 }
129
130 this.output.WriteLine("\r\n");
131 this.output.WriteLine(Event.Message);
132
133 if (Event.Type >= EventType.Critical && !string.IsNullOrEmpty(Event.StackTrace))
134 {
135 this.output.WriteLine("\r\n");
136 this.output.WriteLine(Event.StackTrace);
137 }
138
139 this.output.Flush();
140 }
141 }
142 catch (Exception)
143 {
144 try
145 {
146 this.DisposeOutput();
147 }
148 catch (Exception)
149 {
150 // Ignore
151 }
152 }
153 finally
154 {
155 this.AfterWrite();
156 }
157 }
158
159 return Task.CompletedTask;
160 }
161
165 public virtual bool CanDisposeOutput => true;
166
170 public virtual void DisposeOutput()
171 {
172 if (this.CanDisposeOutput)
173 {
174 this.output?.Flush();
175 this.output?.Dispose();
176 }
177
178 this.output = null;
179 }
180 }
181}
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
Outputs sniffed data to a text writer.
override void Dispose()
IDisposable.Dispose
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 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:25
EventType
Type of event.
Definition: EventType.cs:7