Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmlFileEventSink.cs
1using System;
2using System.IO;
3using System.Text;
4using System.Xml;
5using System.Threading.Tasks;
7
8namespace Waher.Events.Files
9{
14 {
15 private readonly XmlWriterSettings settings;
16 private readonly FileNameTimeSequence fileSequence;
17 private StreamWriter file;
18 private readonly string transform = null;
19 private readonly int deleteAfterDays;
20
36 public XmlFileEventSink(string ObjectID, string FileName)
37 : this(ObjectID, FileName, string.Empty, 7)
38 {
39 }
40
58 public XmlFileEventSink(string ObjectID, string FileName, int DeleteAfterDays)
59 : this(ObjectID, FileName, string.Empty, DeleteAfterDays)
60 {
61 }
62
79 public XmlFileEventSink(string ObjectID, string FileName, string Transform)
80 : this(ObjectID, FileName, Transform, 7)
81 {
82 }
83
102 public XmlFileEventSink(string ObjectID, string FileName, string Transform, int DeleteAfterDays)
103 : base(ObjectID, null)
104 {
105 this.file = null;
106 this.output = null;
107 this.fileSequence = new FileNameTimeSequence(FileName, true);
108 this.transform = Transform;
109 this.deleteAfterDays = DeleteAfterDays;
110
111 this.settings = new XmlWriterSettings
112 {
113 CloseOutput = true,
114 ConformanceLevel = ConformanceLevel.Document,
115 Encoding = Encoding.UTF8,
116 Indent = true,
117 IndentChars = "\t",
118 NewLineChars = "\r\n",
119 NewLineHandling = NewLineHandling.Replace,
120 NewLineOnAttributes = false,
121 OmitXmlDeclaration = false,
122 WriteEndDocumentOnClose = true
123 };
124
125 string FolderName = Path.GetDirectoryName(FileName);
126
127 if (!Directory.Exists(FolderName))
128 {
129 Log.Informational("Creating folder.", FolderName);
130 Directory.CreateDirectory(FolderName);
131 }
132 }
133
137 protected override async Task BeforeWrite()
138 {
139 if (!this.fileSequence.TryGetNewFileName(out string s))
140 return;
141
142 try
143 {
144 this.output?.WriteEndElement();
145 this.output?.WriteEndDocument();
146 this.output?.Flush();
147 this.file?.Dispose();
148 }
149 catch (Exception)
150 {
151 try
152 {
153 this.DisposeOutput();
154 }
155 catch (Exception)
156 {
157 // Ignore
158 }
159 }
160
161 this.file = null;
162 this.output = null;
163
164 this.file = File.CreateText(s);
165
166 this.output = XmlWriter.Create(this.file, this.settings);
167 this.output.WriteStartDocument();
168
169 if (!string.IsNullOrEmpty(this.transform))
170 {
171 if (File.Exists(this.transform))
172 {
173 try
174 {
175 byte[] XsltBin = await Runtime.IO.Files.ReadAllBytesAsync(this.transform);
176
177 this.output.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"data:text/xsl;base64," +
178 Convert.ToBase64String(XsltBin) + "\"");
179 }
180 catch (Exception)
181 {
182 this.output.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + this.transform + "\"");
183 }
184 }
185 else
186 this.output.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + this.transform + "\"");
187 }
188
189 this.output.WriteStartElement("EventOutput", EventExtensions.LogNamespace);
190 this.output.Flush();
191
192 if (this.deleteAfterDays < int.MaxValue)
193 {
194 string FolderName = Path.GetDirectoryName(s);
195
196 Runtime.IO.Files.DeleteOldFiles(FolderName, TimeSpan.FromDays(this.deleteAfterDays), SearchOption.AllDirectories, true);
197 }
198 }
199
203 public override void DisposeOutput()
204 {
205 base.DisposeOutput();
206
207 this.file?.Dispose();
208 this.file = null;
209 }
210 }
211}
const string LogNamespace
http://waher.se/Schema/EventOutput.xsd
Outputs sniffed data to an XML file.
XmlFileEventSink(string ObjectID, string FileName, int DeleteAfterDays)
Outputs sniffed data to an XML file.
override void DisposeOutput()
Disposes of the current output.
XmlFileEventSink(string ObjectID, string FileName, string Transform, int DeleteAfterDays)
Outputs sniffed data to an XML file.
XmlFileEventSink(string ObjectID, string FileName)
Outputs sniffed data to an XML file.
override async Task BeforeWrite()
Method is called before writing something to the text file.
XmlFileEventSink(string ObjectID, string FileName, string Transform)
Outputs sniffed data to an XML file.
Outputs sniffed data to an XML writer.
XmlWriter output
XML writer object.
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Definition: Log.cs:344
virtual string ObjectID
Object ID, used when logging events.
Definition: LogObject.cs:26
Class that generates a sequence of file names, based on the system time.