Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WebEventSink.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Content;
6using Waher.Events;
8
10{
14 public class WebEventSink : EventSink
15 {
16 private readonly DateTime created = DateTime.Now;
17 private readonly DateTime expires;
18 private readonly string[] privileges;
19 private readonly string userVariable;
20 private readonly string resource;
21 private string[] tabIds = null;
22 private DateTime tabIdTimestamp = DateTime.MinValue;
23
33 public WebEventSink(string SinkId, string PageResource, TimeSpan MaxLife, string UserVariable, params string[] Privileges)
34 : base(SinkId)
35 {
36 this.expires = DateTime.Now.Add(MaxLife);
37 this.resource = PageResource;
38 this.tabIds = null;
39 this.userVariable = UserVariable;
40 this.privileges = Privileges;
41 }
42
47 public override async Task Queue(Event Event)
48 {
49 try
50 {
51 DateTime Now = DateTime.Now;
52
53 if ((Now - this.tabIdTimestamp).TotalSeconds > 2 || this.tabIds is null || this.tabIds.Length == 0)
54 {
55 this.tabIds = ClientEvents.GetTabIDsForLocation(this.resource, true);
56 this.tabIdTimestamp = Now;
57 }
58
59 Dictionary<string, object>[] Tags;
60
61 if (Event.Tags is null)
62 Tags = null;
63 else
64 {
65 int i, c = Event.Tags.Length;
66 Tags = new Dictionary<string, object>[c];
67
68 for (i = 0; i < c; i++)
69 {
70 Tags[i] = new Dictionary<string, object>()
71 {
72 { "name", Event.Tags[i].Key },
73 { "value", Event.Tags[i].Value }
74 };
75 }
76 }
77
78 string Data = JSON.Encode(new KeyValuePair<string, object>[]
79 {
80 new KeyValuePair<string, object>("date", XML.Encode(Event.Timestamp.Date,true)),
81 new KeyValuePair<string, object>("time", Event.Timestamp.TimeOfDay.ToString()),
82 new KeyValuePair<string, object>("type", Event.Type.ToString()),
83 new KeyValuePair<string, object>("level", Event.Level.ToString()),
84 new KeyValuePair<string, object>("id", Event.EventId),
85 new KeyValuePair<string, object>("object", Event.Object),
86 new KeyValuePair<string, object>("actor", Event.Actor),
87 new KeyValuePair<string, object>("module", Event.Module),
88 new KeyValuePair<string, object>("facility", Event.Facility),
89 new KeyValuePair<string, object>("message", Event.Message),
90 new KeyValuePair<string, object>("stackTrace", Event.StackTrace),
91 new KeyValuePair<string, object>("tags", Tags)
92 }, false);
93
94 int Tabs = await ClientEvents.PushEvent(this.tabIds, "NewEvent", Data, true, this.userVariable, this.privileges);
95
96 if (Now >= this.expires || (Tabs <= 0 && (Now - this.created).TotalSeconds >= 5))
97 {
98 await ClientEvents.PushEvent(this.tabIds, "SinkClosed", string.Empty, false, this.userVariable, this.privileges);
99
100 Log.Unregister(this);
101 this.Dispose();
102 }
103 }
104 catch (Exception ex)
105 {
106 Log.Exception(ex);
107 }
108 }
109 }
110}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
Class representing an event.
Definition: Event.cs:10
string Message
Free-text event message.
Definition: Event.cs:131
EventType Type
Type of event.
Definition: Event.cs:121
string Object
Object related to the event.
Definition: Event.cs:136
EventLevel Level
Event Level.
Definition: Event.cs:126
string Actor
Actor responsible for the action causing the event.
Definition: Event.cs:141
string Module
Module where the event is reported.
Definition: Event.cs:156
DateTime Timestamp
Timestamp of event.
Definition: Event.cs:116
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
Definition: Event.cs:166
string EventId
Computer-readable Event ID identifying type of even.
Definition: Event.cs:146
string Facility
Facility can be either a facility in the network sense or in the system sense.
Definition: Event.cs:151
string StackTrace
Stack Trace of event.
Definition: Event.cs:161
Base class for event sinks.
Definition: EventSink.cs:9
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 bool Unregister(IEventSink EventSink)
Unregisters an event sink from the event log.
Definition: Log.cs:46
virtual void Dispose()
IDisposable.Dispose()
Definition: LogObject.cs:1134
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
Definition: ClientEvents.cs:51
static string[] GetTabIDsForLocation(string Location)
Gets the Tab IDs of all tabs that display a particular resource.
static Task< int > PushEvent(string[] TabIDs, string Type, object Data)
Puses an event to a set of Tabs, given their Tab IDs.
Sending events to the corresponding web page(s).
Definition: WebEventSink.cs:15
override async Task Queue(Event Event)
Queues an event to be output.
Definition: WebEventSink.cs:47
WebEventSink(string SinkId, string PageResource, TimeSpan MaxLife, string UserVariable, params string[] Privileges)
Sending sniffer events to the corresponding web page(s).
Definition: WebEventSink.cs:33