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;
4
5namespace Waher.Events.Files
6{
11 {
12 private StreamWriter file;
13 private readonly FileNameTimeSequence fileSequence;
14 private readonly int deleteAfterDays;
15
31 public TextFileEventSink(string ObjectID, string FileName)
32 : this(ObjectID, FileName, 7)
33 {
34 }
35
53 public TextFileEventSink(string ObjectID, string FileName, int DeleteAfterDays)
54 : base(ObjectID, null)
55 {
56 this.file = null;
57 this.output = null;
58 this.fileSequence = new FileNameTimeSequence(FileName, true);
59 this.deleteAfterDays = DeleteAfterDays;
60
61 string FolderName = Path.GetDirectoryName(FileName);
62
63 if (!Directory.Exists(FolderName))
64 {
65 Log.Informational("Creating folder.", FolderName);
66 Directory.CreateDirectory(FolderName);
67 }
68 }
69
73 protected override void BeforeWrite()
74 {
75 if (!this.fileSequence.TryGetNewFileName(out string s))
76 return;
77
78 try
79 {
80 this.file?.Dispose();
81 }
82 catch (Exception)
83 {
84 // Ignore
85 }
86
87 this.file = null;
88 this.output = null;
89
90 this.output = this.file = File.CreateText(s);
91
92 if (this.deleteAfterDays < int.MaxValue)
93 {
94 string FolderName = Path.GetDirectoryName(s);
95
96 Runtime.IO.Files.DeleteOldFiles(FolderName, TimeSpan.FromDays(this.deleteAfterDays), SearchOption.AllDirectories, true);
97 }
98 }
99
103 public override void DisposeOutput()
104 {
105 base.DisposeOutput();
106
107 this.file?.Dispose();
108 this.file = null;
109 }
110 }
111}
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 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.
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.