-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathTextWriterEventSink.cs
182 lines (156 loc) · 3.84 KB
/
TextWriterEventSink.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Waher.Events.Files
{
/// <summary>
/// Outputs sniffed data to a text writer.
/// </summary>
public class TextWriterEventSink : EventSink, IDisposable
{
/// <summary>
/// Text writer object.
/// </summary>
protected TextWriter output;
private readonly object synchObject = new object();
private bool disposed = false;
/// <summary>
/// Outputs sniffed data to a text writer.
/// </summary>
/// <param name="ObjectID">Object ID</param>
/// <param name="Output">Output</param>
public TextWriterEventSink(string ObjectID, TextWriter Output)
: base(ObjectID)
{
this.output = Output;
}
/// <summary>
/// Method is called before writing something to the text file.
/// </summary>
protected virtual void BeforeWrite()
{
// Do nothing by default.
}
/// <summary>
/// Method is called after writing something to the text file.
/// </summary>
protected virtual void AfterWrite()
{
// Do nothing by default.
}
/// <summary>
/// <see cref="IDisposableAsync.DisposeAsync"/>
/// </summary>
public override Task DisposeAsync()
{
this.disposed = true;
this.DisposeOutput();
return base.DisposeAsync();
}
/// <summary>
/// Queues an event to be output.
/// </summary>
/// <param name="Event">Event to queue.</param>
public override Task Queue(Event Event)
{
if (this.disposed)
return Task.CompletedTask;
lock (this.synchObject)
{
this.BeforeWrite();
try
{
if (!(this.output is null))
{
this.output.Write(Event.Timestamp.ToString("d"));
this.output.Write(", ");
this.output.Write(Event.Timestamp.ToString("T"));
this.output.Write('\t');
this.output.Write(Event.Type.ToString());
this.output.Write('\t');
this.output.Write(Event.Level.ToString());
if (!string.IsNullOrEmpty(Event.EventId))
{
this.output.Write('\t');
this.output.Write(Event.EventId);
}
if (!string.IsNullOrEmpty(Event.Object))
{
this.output.Write('\t');
this.output.Write(Event.Object);
}
if (!string.IsNullOrEmpty(Event.Actor))
{
this.output.Write('\t');
this.output.Write(Event.Actor);
}
this.output.WriteLine("\r\n");
if (!string.IsNullOrEmpty(Event.Module))
{
this.output.Write('\t');
this.output.Write(Event.Module);
}
if (!string.IsNullOrEmpty(Event.Facility))
{
this.output.Write('\t');
this.output.Write(Event.Facility);
}
if (!(Event.Tags is null) && Event.Tags.Length > 0)
{
this.output.WriteLine("\r\n");
foreach (KeyValuePair<string, object> Tag in Event.Tags)
{
this.output.Write('\t');
this.output.Write(Tag.Key);
this.output.Write('=');
if (!(Tag.Value is null))
this.output.Write(Tag.Value.ToString());
}
}
this.output.WriteLine("\r\n");
this.output.WriteLine(Event.Message);
if (Event.Type >= EventType.Critical && !string.IsNullOrEmpty(Event.StackTrace))
{
this.output.WriteLine("\r\n");
this.output.WriteLine(Event.StackTrace);
}
this.output.Flush();
}
}
catch (Exception)
{
try
{
this.DisposeOutput();
}
catch (Exception)
{
// Ignore
}
}
finally
{
this.AfterWrite();
}
}
return Task.CompletedTask;
}
/// <summary>
/// If output can be disposed.
/// </summary>
public virtual bool CanDisposeOutput => true;
/// <summary>
/// Disposes of the current output.
/// </summary>
public virtual void DisposeOutput()
{
if (this.CanDisposeOutput)
{
this.output?.Flush();
this.output?.Dispose();
}
this.output = null;
}
}
}