Semantic Logging Extensibility and Reactive Extensions (Rx)

If you already played with new Semantic Logging Application Block which I strongly recommend if you are using the new typed events in NET 4.5, perhaps you have noticed the wide extensibility provided in both in-process and out-of-process scenarios. 

Not only you can write your own " sinks" to add new event repositories or listeners but also complex filtering capabilities with the Observables pattern that allow the use of Rx as shown in the new Quickstarts added in the Wave 2 release of Entlib v6.

Let me show how you can beyond something that is by design like the event payload format (JSON) of the DB sinks and change that format (and any other event data) as you wish.

Let's say that you set your DB sink like this:

 

this.dbListener = SqlDatabaseLog.CreateListener(...[all args here])

 

Now it would be great if this helper method provides an additional argument with some lambda that can dictate the way we format or set the data to be written.

So writing a simple class similar to the provided helper method "CreateListener" and extending the event stream with the Rx Query Library package we can do something like this:

         

public static SinkSubscription<SqlDatabaseSink> LogToSqlDatabaseFormatted(this IObservable<EventEntry> eventStream, [same args as 'LogToSqlDatabase'], Func<EventEntry, string> payloadFormatter = null)

{

   var sink = new SqlDatabaseSink([same args]);

   // using System.Reactive.Linq extension

   IDisposable subscription = eventStream.Select(entry => ConvertToEventRecord(entry, payloadFormatter)).Subscribe(sink);

   return new SinkSubscription<SqlDatabaseSink>(subscription, sink);

In this case, 'ConvertToEventRecord' will simply map the properties between EventRecord and EventEntry along with the custom process for the payload property with 'payloadFormatter'.

For more details and also an example of a custom sink with out-of-process implementation, you can download a sample solution (see Attachment at the bottom) that showcase this using NuGet packages for the required binaries but the Etw library which is included in the Semantic Logging Service

No Comments