2using System.Collections.Generic;
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))
113 private async
void DoCleanup(
object P)
117 await this.
DeleteOld(DateTime.Now.AddDays(-
this.eventLifetimeDays));
143 public static async Task<int>
DeleteOld(
string ObjectId, DateTime Limit)
162 KeyValuePair<string, object>[] Tags =
new KeyValuePair<string, object>[]
164 new KeyValuePair<string, object>(
"Limit", Limit),
165 new KeyValuePair<string, object>(
"NrEvents", NrEvents)
171 Log.
Informational(
"Deleting " + NrEvents.ToString() +
" events from the database.", ObjectId, Tags);
185 public Task<IEnumerable<PersistedEvent>>
GetEvents(
int Offset,
int MaxCount, DateTime From, DateTime To)
218 public Task<IEnumerable<PersistedEvent>>
GetEventsOfObject(
int Offset,
int MaxCount,
string Object, DateTime From, DateTime To)
235 public Task<IEnumerable<PersistedEvent>>
GetEventsOfActor(
int Offset,
int MaxCount,
string Actor, DateTime From, DateTime To)
252 public Task<IEnumerable<PersistedEvent>>
GetEventsOfEventId(
int Offset,
int MaxCount,
string EventId, DateTime From, DateTime To)
269 public Task<IEnumerable<PersistedEvent>>
GetEventsOfFacility(
int Offset,
int MaxCount,
string Facility, DateTime From, DateTime To)
291 PersistedEvent.Facility = this.defaultFacility;
305 if (this.defaultFacility != DefaultFacility)
307 if (!
string.IsNullOrEmpty(this.defaultFacility))
309 if (this.ComputeDigest(DefaultFacilityKey) != this.defaultFacilityDigest)
310 throw new UnauthorizedAccessException(
"Unauthorized to change the default facility.");
313 this.defaultFacility = DefaultFacility;
314 this.defaultFacilityDigest = this.ComputeDigest(DefaultFacilityKey);
318 private string ComputeDigest(
string Key)
323 internal static int ArchiveDays
327 if (registeredLog is
null)
331 if (Sink is PersistedEventLog PersistedEventLog)
333 registeredLog = PersistedEventLog;
338 if (registeredLog is
null)
342 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...
override void Dispose()
IDisposable.Dispose()
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,...
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 Task< IEnumerable< object > > FindDelete(string Collection, params string[] SortOrder)
Finds objects in a given collection and deletes them in the same atomic operation.
static async Task InsertLazy(object Object)
Inserts an object into the database, if unlocked. If locked, object will be inserted at next opportun...
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
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.