Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TextFileEventSink.cs
1using System;
2using System.IO;
3
4namespace Waher.Events.Files
5{
10 {
11 private StreamWriter file;
12 private readonly string fileName;
13 private readonly int deleteAfterDays;
14
30 public TextFileEventSink(string ObjectID, string FileName)
31 : this(ObjectID, FileName, 7)
32 {
33 }
34
52 public TextFileEventSink(string ObjectID, string FileName, int DeleteAfterDays)
53 : base(ObjectID, null)
54 {
55 this.file = null;
56 this.output = null;
57 this.fileName = FileName;
58 this.deleteAfterDays = DeleteAfterDays;
59
60 string FolderName = Path.GetDirectoryName(FileName);
61
62 if (!Directory.Exists(FolderName))
63 {
64 Log.Informational("Creating folder.", FolderName);
65 Directory.CreateDirectory(FolderName);
66 }
67 }
68
72 protected override void BeforeWrite()
73 {
74 if (!(this.file is null))
75 {
76 try
77 {
78 this.file?.Dispose();
79 }
80 catch (Exception)
81 {
82 // Ignore
83 }
84
85 this.file = null;
86 this.output = null;
87 }
88
89 string s = XmlFileEventSink.GetFileName(this.fileName);
90
91 if (File.Exists(s))
92 this.output = this.file = File.AppendText(s);
93 else
94 {
95 this.output = this.file = File.CreateText(s);
96
97 if (this.deleteAfterDays < int.MaxValue)
98 {
99 string FolderName = Path.GetDirectoryName(s);
100 if (string.IsNullOrEmpty(FolderName))
101 FolderName = ".";
102
103 string[] Files = Directory.GetFiles(FolderName, "*.*");
104
105 foreach (string FileName in Files)
106 {
107 if ((DateTime.Now - File.GetLastWriteTime(FileName)).TotalDays >= this.deleteAfterDays)
108 {
109 try
110 {
111 File.Delete(FileName);
112 }
113 catch (Exception ex)
114 {
115 Log.Exception(ex);
116 }
117 }
118 }
119 }
120 }
121 }
122
126 protected override void AfterWrite()
127 {
128 this.file.Dispose();
129 this.file = null;
130 }
131
135 public override void DisposeOutput()
136 {
137 base.DisposeOutput();
138
139 this.file?.Dispose();
140 this.file = null;
141 }
142 }
143}
Outputs sniffed data to a text file.
TextFileEventSink(string ObjectID, string FileName)
Outputs sniffed data to a text file.
TextFileEventSink(string ObjectID, string FileName, int DeleteAfterDays)
Outputs sniffed data to a text file.
override void AfterWrite()
Method is called after writing something to the text file.
override void DisposeOutput()
Disposes of the current output.
override void BeforeWrite()
Method is called before writing something to the text file.
Outputs sniffed data to a text writer.
Outputs sniffed data to an XML file.
static string GetFileName(string TemplateFileName)
Gets the name of a file, given a file name template.
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
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:334
virtual string ObjectID
Object ID, used when logging events.
Definition: LogObject.cs:25