5using System.Threading.Tasks;
19 private int eventLifetimeDays;
20 private string defaultFacility;
21 private string defaultFacilityDigest =
null;
53 : base(
"Persisted Event Log")
56 throw new ArgumentOutOfRangeException(
"The lifetime must be a positive number of days.", nameof(
EventLifetimeDays));
59 this.defaultFacility = DefaultFacility;
60 this.defaultFacilityDigest = this.ComputeDigest(DefaultFacilityKey);
62 if (CleanupTime.HasValue)
77 if (CleanupTime < TimeSpan.Zero || CleanupTime.TotalDays >= 1.0)
78 throw new ArgumentOutOfRangeException(
"Invalid time.", nameof(CleanupTime));
80 int MillisecondsPerDay = 1000 * 60 * 60 * 24;
81 int MsUntilNext = (int)((DateTime.Today.AddDays(1).Add(CleanupTime) - DateTime.Now).TotalMilliseconds + 0.5);
84 this.timer =
new Timer(this.DoCleanup,
null, MsUntilNext, MillisecondsPerDay);
93 if (!(this.timer is
null))
110 return base.DisposeAsync();
113 private async
void DoCleanup(
object P)
117 await this.
DeleteOld(DateTime.UtcNow.AddDays(-
this.eventLifetimeDays));
143 public static async Task<int>
DeleteOld(
string ObjectId, DateTime Limit)
150 KeyValuePair<string, object>[] Tags =
new KeyValuePair<string, object>[]
152 new KeyValuePair<string, object>(
"Limit", Limit),
153 new KeyValuePair<string, object>(
"NrEvents", NrEvents)
159 Log.
Informational(
"Deleting " + NrEvents.ToString() +
" events from the database.", ObjectId, Tags);
173 public Task<IEnumerable<PersistedEvent>>
GetEvents(
int Offset,
int MaxCount, DateTime From, DateTime To)
206 public Task<IEnumerable<PersistedEvent>>
GetEventsOfObject(
int Offset,
int MaxCount,
string Object, DateTime From, DateTime To)
223 public Task<IEnumerable<PersistedEvent>>
GetEventsOfActor(
int Offset,
int MaxCount,
string Actor, DateTime From, DateTime To)
240 public Task<IEnumerable<PersistedEvent>>
GetEventsOfEventId(
int Offset,
int MaxCount,
string EventId, DateTime From, DateTime To)
257 public Task<IEnumerable<PersistedEvent>>
GetEventsOfFacility(
int Offset,
int MaxCount,
string Facility, DateTime From, DateTime To)
279 PersistedEvent.Facility = this.defaultFacility;
293 if (this.defaultFacility != DefaultFacility)
295 if (!
string.IsNullOrEmpty(this.defaultFacility))
297 if (this.ComputeDigest(DefaultFacilityKey) != this.defaultFacilityDigest)
298 throw new UnauthorizedAccessException(
"Unauthorized to change the default facility.");
301 this.defaultFacility = DefaultFacility;
302 this.defaultFacilityDigest = this.ComputeDigest(DefaultFacilityKey);
306 private string ComputeDigest(
string Key)
311 internal static int ArchiveDays
315 if (registeredLog is
null)
319 if (Sink is PersistedEventLog PersistedEventLog)
321 registeredLog = PersistedEventLog;
326 if (registeredLog is
null)
330 return registeredLog.eventLifetimeDays;
Class representing an event.
Base class for event sinks.
Static class managing the application event log. Applications and services log events on this static ...
static IEventSink[] Sinks
Registered sinks.
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.
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.
virtual string ObjectID
Object ID, used when logging events.
Class representing a persisted event.
string Facility
Facility can be either a facility in the network sense or in the system sense.
Creates an even sink that stores incoming (logged) events in the local object database,...
Task< int > DeleteOld(DateTime Limit)
Deletes old events. This method is called once a day automatically. It can also be called manually to...
Task< IEnumerable< PersistedEvent > > GetEventsOfEventId(int Offset, int MaxCount, string EventId, DateTime From, DateTime To)
Gets events relating to a specific event identity between two timepoints, ordered by descending times...
Task< IEnumerable< PersistedEvent > > GetEventsOfObject(int Offset, int MaxCount, string Object, DateTime From, DateTime To)
Gets events beloinging to a specific object between two timepoints, ordered by descending timestamp.
Task< IEnumerable< PersistedEvent > > GetEventsOfActor(int Offset, int MaxCount, string Actor, DateTime From, DateTime To)
Gets events relating to a specific actor between two timepoints, ordered by descending timestamp.
PersistedEventLog(int EventLifetimeDays, TimeSpan? CleanupTime, string DefaultFacility, string DefaultFacilityKey)
Creates an even sink that stores incoming (logged) events in the local object database,...
int EventLifetimeDays
Number of days to store events in the database.
Task< IEnumerable< PersistedEvent > > GetEventsOfType(int Offset, int MaxCount, EventType Type, DateTime From, DateTime To)
Gets events of a specific type between two timepoints, ordered by descending timestamp.
PersistedEventLog(int EventLifetimeDays, TimeSpan? CleanupTime)
Creates an even sink that stores incoming (logged) events in the local object database,...
override Task DisposeAsync()
IDisposableAsync.DisposeAsync()
void TurnOnDailyPurge(int EventLifetimeDays, TimeSpan CleanupTime)
Turns on the daily purge of old events.
PersistedEventLog(int EventLifetimeDays)
Creates an even sink that stores incoming (logged) events in the local object database,...
Task< IEnumerable< PersistedEvent > > GetEvents(int Offset, int MaxCount, DateTime From, DateTime To)
Gets events between two timepoints, ordered by descending timestamp.
static async Task< int > DeleteOld(string ObjectId, DateTime Limit)
Deletes old events. This method is called once a day automatically. It can also be called manually to...
Task< IEnumerable< PersistedEvent > > GetEventsOfFacility(int Offset, int MaxCount, string Facility, DateTime From, DateTime To)
Gets events relating to a specific facility between two timepoints, ordered by descending timestamp.
bool TurnOffDailyPurge()
Turns off the daily purge of old events.
override async Task Queue(Event Event)
Queues an event to be output.
void SetDefaultFacility(string DefaultFacility, string DefaultFacilityKey)
Sets the default facility. The default facility can only be reset by a caller presenting the same key...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static async Task Delete(object Object)
Deletes an object in the database.
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
This filter selects objects that conform to all child-filters provided.
This filter selects objects that have a named field greater or equal to a given value.
This filter selects objects that have a named field lesser or equal to a given value.
Contains methods for simple hash calculations.
static string ComputeSHA256HashString(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Interface for all event sinks.