This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class MyEventListener : EventListener | |
{ | |
protected override void OnEventSourceCreated(EventSource eventSource) | |
{ | |
base.OnEventSourceCreated(eventSource); | |
Debug.WriteLine("Listener attached to the source"); | |
} | |
protected override void OnEventWritten(EventWrittenEventArgs eventData) | |
{ | |
Debug.WriteLine("Event data: {0}", eventData.Payload[0]); | |
} | |
} |
The listener's OnEventWritten method is called when EventSource writes an event. But before that we need to specify what level of events we are interested in from the EventSource. This is called Enabling the events where listener is specified with the EventSource and EventLevel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var listener = new MyEventListener(); | |
listener.EnableEvents(MyEventSource.Log, EventLevel.Informational); | |
MyEventSource.Log.WriteLog("message logged"); |

Semantic Logging Application Block
Semantic Logging Application Block is provided in Enterprise Library 6 for supporting structured logging. It enhances EventSource to support logging events in SQL Server and Azure tables. Like other application blocks, this is also available as a Nuget package.

SLAB provides custom implementation of EventListener. The listener is Rx's IObservable. This is named as ObservableEventListener. Since this implements IObservable, it allows subscriptions from Event sinks. The sinks are IObserver (s) to observe on these event listeners.

The application block also provides a specialization of EventSource. This is named as SemanticLoggingEventSource.

In-Process Vs Out-Process
SLAB can be used in-process or out-process. We can use the Nuget package to use the libraries for using it in process. For out of process, it is hosted as a windows service. The service installer is available as a separate download. In the previous post, we discussed how we can use out-process logging with SLAB.
Event Text Formatter
SLAB supports JSON and XML based text formatting. The types are available in Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Formatters namespace available in Microsoft.Practices.EnterpriseLibrary.SemanticLogging assembly. The assembly is downloaded using EnterpriseLibrary.SemanticLogging nuget package.

Event Sinks & Factories
SLAB also provides Event Sinks and their factories. All of these event sinks are IObserver.

Let's install EnterpriseLibrary.SemanticLogging application block nuget package. The package should take care of all its dependencies installation as well, which is only comprised of Newtonsoft.Json package.

Let's also install Rx-Main nuget package for subscriptions using Reactive extensions based subscribers.

In the following code we are subscribing to ObservableEventListener. We can get the messages in the OnNext() method for the subscriber. Here we have also used OnCompleted() and OnError() of the subscriber.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var listener = new ObservableEventListener(); | |
listener.EnableEvents(MyEventSource.Log, EventLevel.Informational); | |
listener.Subscribe<EventEntry>( | |
a => Debug.WriteLine(a.Payload[0]), | |
e => Debug.WriteLine(e.Message), | |
() => Debug.WriteLine("Completed!")); | |
MyEventSource.Log.WriteLog("message logged"); | |
MyEventSource.Log.WriteLog("Another Message"); |

Download
No comments:
Post a Comment