4using System.Threading.Tasks;
16 private int eventLifetimeDays;
17 private readonly
string queueName;
19 private bool errorState =
false;
41 TimeSpan? CleanupTime)
44 this.queueName = QueueName;
46 if (EventLifetimeDays <= 0 || !CleanupTime.HasValue)
49 this.eventLifetimeDays = 0;
53 if (CleanupTime.Value < TimeSpan.Zero || CleanupTime.Value.TotalDays >= 1.0)
54 throw new ArgumentOutOfRangeException(
"Invalid time.", nameof(CleanupTime));
56 int MillisecondsPerDay = 1000 * 60 * 60 * 24;
57 int MsUntilNext = (int)((DateTime.Today.AddDays(1).Add(CleanupTime.Value) - DateTime.Now).TotalMilliseconds + 0.5);
59 this.eventLifetimeDays = EventLifetimeDays;
60 this.timer =
new Timer(this.PurgeOldItems,
null, MsUntilNext, MillisecondsPerDay);
69 if (!(this.queue is
null))
75 this.timer?.Dispose();
78 await base.DisposeAsync();
87 if (this.queue is
null)
92 if (await this.queue.
Enqueue(Item,
this.errorState ? 0 : 10000))
93 this.errorState =
false;
94 else if (!this.errorState)
96 this.errorState =
true;
99 " is full and new events cannot be queued.",
this.ObjectID,
string.Empty,
114 if (this.queue is
null)
117 await this.queue.
Clear();
120 private async
void PurgeOldItems(
object P)
124 await this.PurgeOldItems(DateTime.UtcNow.AddDays(-
this.eventLifetimeDays));
145 Item = await this.queue.
Peek();
147 while (!(Item is
null))
155 Item = await this.queue.
Peek();
160 KeyValuePair<string, object>[] Tags =
new KeyValuePair<string, object>[]
162 new KeyValuePair<string, object>(
"Limit", Limit),
163 new KeyValuePair<string, object>(
"NrEvents", NrEvents)
169 Log.
Informational(
"Purging " + NrEvents.ToString() +
" events from the queue.",
this.ObjectID, Tags);
Class representing an event.
void Avoid(IEventSink EventSink)
If the event sink EventSink should be avoided when logging the event.
Base class for event sinks.
Static class managing the application event log. Applications and services log events on this static ...
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.
static async void Event(Event Event)
Logs an event. It will be distributed to registered event sinks.
virtual string ObjectID
Object ID, used when logging events.
Creates an even sink that queues incoming events in a local persisted queue, for processing by in ord...
override async Task DisposeAsync()
IDisposableAsync.DisposeAsync()
EventQueue(string ObjectID, string QueueName)
Creates an even sink that queues incoming events in a local persisted queue, for processing by in ord...
EventQueue(string ObjectID, string QueueName, int EventLifetimeDays, TimeSpan? CleanupTime)
Creates an even sink that queues incoming events in a local persisted queue, for processing by in ord...
async Task< int > PurgeOldItems(DateTime Limit)
Purges old events from the queue. This method is called once a day automatically. It can also be call...
override async Task Queue(Event Event)
Queues an event to be output.
async Task ClearQueue()
Clears the queue.
Class representing a queued event.
DateTime Timestamp
Timestamp of event.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static Task< IPersistedQueue > GetQueue(string QueueName)
Gets a persistent dictionary containing objects in a collection.
Task DisposeAsync()
Disposes of the object, asynchronously.
Inteface for persisted queues.
Task Clear()
Clears the queue.
Task< object > Peek()
Returns the next item available to be dequeued, without dequeueing it. If an item is not available,...
Task< bool > Enqueue(object Item)
Enqueues an item into the queue.
Task< object > Dequeue()
Dequeue an item from the queue. The task will wait for an item to be dequeued, or,...