-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathXmlFileEventSink.cs
More file actions
211 lines (191 loc) · 6.13 KB
/
XmlFileEventSink.cs
File metadata and controls
211 lines (191 loc) · 6.13 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Threading.Tasks;
using Waher.Runtime.IO;
namespace Waher.Events.Files
{
/// <summary>
/// Outputs sniffed data to an XML file.
/// </summary>
public class XmlFileEventSink : XmlWriterEventSink
{
private readonly XmlWriterSettings settings;
private readonly FileNameTimeSequence fileSequence;
private StreamWriter file;
private readonly string transform = null;
private readonly int deleteAfterDays;
/// <summary>
/// Outputs sniffed data to an XML file.
/// </summary>
/// <param name="ObjectID">Object ID</param>
/// <param name="FileName">File Name. The following strings will be replaced by current values:
///
/// %YEAR% = Current year.
/// %MONTH% = Current month.
/// %DAY% = Current day.
/// %HOUR% = Current hour.
/// %MINUTE% = Current minute.
/// %SECOND% = Current second.
///
/// NOTE: Make sure files are stored in a separate folder, as old files will be automatically deleted.
/// </param>
public XmlFileEventSink(string ObjectID, string FileName)
: this(ObjectID, FileName, string.Empty, 7)
{
}
/// <summary>
/// Outputs sniffed data to an XML file.
/// </summary>
/// <param name="ObjectID">Object ID</param>
/// <param name="FileName">File Name. The following strings will be replaced by current values:
///
/// %YEAR% = Current year.
/// %MONTH% = Current month.
/// %DAY% = Current day.
/// %HOUR% = Current hour.
/// %MINUTE% = Current minute.
/// %SECOND% = Current second.
///
/// NOTE: Make sure files are stored in a separate folder, as old files will be automatically deleted.
/// </param>
/// <param name="DeleteAfterDays">Number of days files will be kept. All files older than this
/// in the corresponding folder will be removed. Default value is 7 days.</param>
public XmlFileEventSink(string ObjectID, string FileName, int DeleteAfterDays)
: this(ObjectID, FileName, string.Empty, DeleteAfterDays)
{
}
/// <summary>
/// Outputs sniffed data to an XML file.
/// </summary>
/// <param name="ObjectID">Object ID</param>
/// <param name="FileName">File Name. The following strings will be replaced by current values:
///
/// %YEAR% = Current year.
/// %MONTH% = Current month.
/// %DAY% = Current day.
/// %HOUR% = Current hour.
/// %MINUTE% = Current minute.
/// %SECOND% = Current second.
///
/// NOTE: Make sure files are stored in a separate folder, as old files will be automatically deleted.
/// </param>
/// <param name="Transform">Transform file name.</param>
public XmlFileEventSink(string ObjectID, string FileName, string Transform)
: this(ObjectID, FileName, Transform, 7)
{
}
/// <summary>
/// Outputs sniffed data to an XML file.
/// </summary>
/// <param name="ObjectID">Object ID</param>
/// <param name="FileName">File Name. The following strings will be replaced by current values:
///
/// %YEAR% = Current year.
/// %MONTH% = Current month.
/// %DAY% = Current day.
/// %HOUR% = Current hour.
/// %MINUTE% = Current minute.
/// %SECOND% = Current second.
///
/// NOTE: Make sure files are stored in a separate folder, as old files will be automatically deleted.
/// </param>
/// <param name="Transform">Transform file name.</param>
/// <param name="DeleteAfterDays">Number of days files will be kept. All files older than this
/// in the corresponding folder will be removed. Default value is 7 days.</param>
public XmlFileEventSink(string ObjectID, string FileName, string Transform, int DeleteAfterDays)
: base(ObjectID, null)
{
this.file = null;
this.output = null;
this.fileSequence = new FileNameTimeSequence(FileName, true);
this.transform = Transform;
this.deleteAfterDays = DeleteAfterDays;
this.settings = new XmlWriterSettings
{
CloseOutput = true,
ConformanceLevel = ConformanceLevel.Document,
Encoding = Encoding.UTF8,
Indent = true,
IndentChars = "\t",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace,
NewLineOnAttributes = false,
OmitXmlDeclaration = false,
WriteEndDocumentOnClose = true
};
string FolderName = Path.GetDirectoryName(FileName);
if (!Directory.Exists(FolderName))
{
Log.Informational("Creating folder.", FolderName);
Directory.CreateDirectory(FolderName);
}
}
/// <summary>
/// Method is called before writing something to the text file.
/// </summary>
protected override async Task BeforeWrite()
{
if (!this.fileSequence.TryGetNewFileName(out string s))
return;
try
{
this.output?.WriteEndElement();
this.output?.WriteEndDocument();
this.output?.Flush();
this.file?.Dispose();
}
catch (Exception)
{
try
{
this.DisposeOutput();
}
catch (Exception)
{
// Ignore
}
}
this.file = null;
this.output = null;
this.file = File.CreateText(s);
this.output = XmlWriter.Create(this.file, this.settings);
this.output.WriteStartDocument();
if (!string.IsNullOrEmpty(this.transform))
{
if (File.Exists(this.transform))
{
try
{
byte[] XsltBin = await Runtime.IO.Files.ReadAllBytesAsync(this.transform);
this.output.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"data:text/xsl;base64," +
Convert.ToBase64String(XsltBin) + "\"");
}
catch (Exception)
{
this.output.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + this.transform + "\"");
}
}
else
this.output.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + this.transform + "\"");
}
this.output.WriteStartElement("EventOutput", EventExtensions.LogNamespace);
this.output.Flush();
if (this.deleteAfterDays < int.MaxValue)
{
string FolderName = Path.GetDirectoryName(s);
Runtime.IO.Files.DeleteOldFiles(FolderName, TimeSpan.FromDays(this.deleteAfterDays), SearchOption.AllDirectories, true);
}
}
/// <summary>
/// Disposes of the current output.
/// </summary>
public override void DisposeOutput()
{
base.DisposeOutput();
this.file?.Dispose();
this.file = null;
}
}
}