Events in FlexRule Server

Print Friendly, PDF & Email

◷ Reading Time: 6 minutes

Using domain events is the best way to collect relevant information and build Dashboards, Audit trails, Log entries, etc. FlexRule Server is an extensible event-driven platform, which means that when anything happens in FlexRule Server, a set of events will be published.

Any actions on the FlexRule Server by users or applications will raise domain events (i.e., events). You can subscribe to these events by their type and then channelize them to any data store and/or dashboard of your choice. This design maintains your ability to use any other platform (i.e., Splunk, Prometheus, datadoghq, Google Analytics, etc.) as your time-series event store. Or you can simply write a custom channel that pushes the events to your own custom location (e.g., MSSQL Server, MongoDB, etc.).

Events

FlexRule Server uses a publish-subscribe pattern. You can write a subscriber and listen to raising events. Events have two states, before and after which allows listeners to capture one or both states.

Events are in the form of Static Types or JSON objects and they have a timestampunique identifiercorrelation id, and duration.

There are different categories of events:

Package

  • PackageOwnershipChangedEvent
  • PackageSettingsUpdatedEvent
  • PackageDeletedEvent
  • PackageSavedEvent
  • PackageStatusChangedEvent

Service

  • ServiceExecutionRequestedEvent
  • ServiceExecutedEvent
  • ServiceDeletedEvent
  • ServiceUpdatedEvent
  • ServiceStatusChangedEvent

Security

  • CredentialRoleUpdatedEvent
  • RoleDeletedEvent
  • RoleCreatedEvent
  • RoleUpdatedEvent

File

  • FileChangesCommittedEvent
  • FilePinStrategyCahangedEvent

Agent

  • AgentCacheRefereshReceivedEvent
  • AgentListUpdatedEvent

Trace

  • AuthorizationTraceEvent
  • ConfigReadTraceEvent
  • DbTraceEvent
  • DomainServiceTraceEvent
  • ExceptionTraceEvent
  • ExceptionTraceEvent
  • HttpMessageTraceEvent
  • ValidationTraceEvent
  • WebApiTraceEvent
  • UnsuccessfulHttpResponseTraceEvent

Subscription

In FlexRule Server you can create your own event monitoring plugin and register it as an event listener in the configuration file on either the Master or Agents. Each of the listeners in the monitoring can subscribe to multiple events which then redirect that to specific channel.

Listeners

In monitoring the events section, use add to register listeners. monitoring/add has the attributes below:

  • event: name of an event from the above list. You can add multiple event names to listen to by separating them with a comma.
  • channel: the name of a channel that is registered in the channel section
  • level: allows you to store an event as an Error, Information, Warning, etc.

Channels

Inside channel registers different channels to which to push the events. channels/add has the attributes shown below:

  • initialData: allows you to provide data to initialize a channel (e.g., folder name, database connection, log service end-point address, etc.).
  • type: address of to the channel. You can use any TraceListener here. By default FlexRule Server uses two TraceListener for channels:
    • File based channel: System.Diagnostics.TextWriterTraceListener
    • Windows Event Log channel: System.Diagnostics.EventLogTraceListener
  • name: name of the channel to be referenced by listeners

Register

Below is the structure of listener and channels that can be managed by both Master and Agents.

<monitoring enable="true" includeDetails="true" prettifyDetails="true">
    <add event="..." channel="..." level="..." />
    <channels>
        <add initializeData="..." 
             type="..." 
             name="..." />
    </channels>
</monitoring>

For example below is a sample configuration that registers AuthorizationTraceEvent events to be stored in a file located at c:\securefolder\authorizationErrors.log

<monitoring enable="true" includeDetails="true" prettifyDetails="true">
    <add event="AuthorizationTraceEvent" channel="authorizationErrors" level="Error" />
    <channels>
        <add initializeData="c:\securefolder\authorizationErrors.log" 
             type="System.Diagnostics.TextWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
             name="authorizationErrors" />
    </channels>
</monitoring>

Custom Channels

When events are raised, you can receive signals in two forms:

  1. Static types
  2. JSON formats (string)
  3. Free text form (via GenericTraceEvent)

Build Custom Channel

In a custom channel, you can listen to events in two ways:

Using Write and WriteLine override of TraceListener. In these methods, the custom listener receives messages in a string with JSON structure or some other free text format if GenericTraceEvent is raised.

using FlexRule.Server.Events;
 
class MyCustomListener : TraceListener
{
    public MyCustomListener()
    {
    }
 
    public override void Write(string message)
    {
        //  message of the channel to process
    }
 
    public override void WriteLine(string message)
    {
        //  message of the channel to process
    }
}

Handle Custom Events

The channel can listen to specific events, using configuration or using your code. For using code, the DomainEvents.Register should be used to subscribe to an event. In this approach, the custom listener receives signals as .Net Types.

using FlexRule.Server.Events;
 
class MyCustomListener : TraceListener
{
    public MyCustomListener()
    {
        // TYPE_OF_EVENT is the event type
        // Subscribe to an event...
        DomainEvents.Register(typeof(TYPE_OF_EVENT), eventHander);
        // when the event occurs, the eventHandler is called. 
    }
 
    private void eventHander(TYPE_OF_EVENT domainEvent)
    {
        // TYPE_OF_EVENT is the event type which this listener is subscribed to
    }
 
    public override void Write(string message)
    {
        //  Do nothing
    }
 
    public override void WriteLine(string message)
    {
        //  Do nothing
    }
}

In the sample code above, TYPE_OF_EVENT is the place holder for the actual event type with the names that are listed in the section above.

Once the custom channel is derived from TraceListener, then to register a custom channel, use the a configuration on Master and/or Agents and configure them as shown below:

  1. Register it with a name in channels section.
  2. Assign the channel to events that you want to subscribe to using the monitoring/add section.
Updated on August 9, 2019

Was this article helpful?

Related Articles