Hernan de Lahitte's blog

.NET Development from the trenches

  • 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


  • Your best friend for authoring ETW strongly typed events

    So you were reading some good tutorials on how to write your first typed event class but for some reason it may not work as expected? (Or many other issues as well)

    As I wrote in my last post about the new Entlib 6.0, there is helping class in the Semantic Logging Application Block named EventSourceAnalyzer that will help you check your class for multiple potential issues that may be hard to diagnose.

    Let's see how to write a very simple unit test that will do a full check of your class:


    public void when_inspecting_myCompanyEventSource()




    As you can see, your custom class will implement a singleton (Log static method) so this instance will be used by the analyzer to perform many checks and throw an error if it finds something wrong or may not be well implemented according to the EventSource guideline.

    This class also have an instance usage where you can turn on/off some knobs for adjust the check behavior to some custom scenarios. On of the performed checks is the type mapping between the event argument types and the WriteEvent call inside your event implementation.

    A standard event may be defined as follow:


    public void GuidNotMapped(Guid id)


        WriteEvent(1, id);


    Now in case you want to use on of the defined overloads of WriteEvent and avoid the casting to the params object[] overload described in section 5.6.2 in StronglyTypedEvents doc then you may have something like this:



    public void GuidMappedToString(Guid id)


        WriteEvent(1, id.ToString());



    So in this case, you may use on of the provided knobs described above and bypass the type mapping check:



    public void when_inspecting_myCompanyEventSource_with_typeMapping_off()


        var analyzer = new EventSourceAnalyzer() { ExcludeWriteEventTypeMapping = true };



    I hope you can find the EventSourceAnalizer a good companion for your custom typed events.








  • Enterprise Library 6.0 is RTW (and Unity 3.0)

    Finally the latest version of Enterprise Library 6.0 (and almost a decade of evolution from the first block).

    A great summary of all the new features along with some insights can be found in this post from Grigori.

    My plan is to write a series of post with some additional information and samples of the brand new Semantic Logging Application Block which leverage the new typed events in NET 4.5 and the high performance ETW tracing infrastructure.

    So I guess that if you are new to typed events in v4.5 and want to start authoring them, you may probably stop by Vance blog, an authority in this matter.

    You will soon realize that creating a typed event class and start producing events is very easy. However is also easy to introduce hard to diagnose errors. So my first post about the "hidden" goodies in this new block (SLAB for friends) will let you know how you can get a good helping hand for authoring your custom EventSource classes.

    One more link in codeplex with some discussion forums and additional links as well. Also Unity 3.0 has it's own MSDN page.


    Stay tuned!


  • Enterprise Library 6.0 and Semantic Logging

     Finally the Semantic Logging CTP from Enterprise Library 6.0 is out.

    In this blog post Grigori presents an insight overview of this new block along with some comments regarding how this blocks leverage the new features in NET 4.5 and ETW.

    Here is the Release Notes that summarizes the goal of this block along with a great video that shows hot to use it.

    In future post I will try to add some insight on the out-of-process mode that you can use SLAB (Semantic Logging Application Block).


  • Tracing to ETW with Enterprise Library v5

    While I was looking for a way to use ETW Tracing using the logging EntLib infrastructure, I found out that using a handy trace listener like SystemDiagnosticsTraceListenerData configured to use the built-in EventProviderTraceListener was all that I needed. With just setting the event provider ID which is simply a GUID added to the initializeData attribute and I'm good to go.

    This is an example of the config section configured with the above settings:   


    <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    type="System.Diagnostics.Eventing.EventProviderTraceListener, System.Core, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"

    traceOutputOptions="ThreadId" name="ETW Trace Listener" initializeData="{D2FAAB3F-5D61-42B5-A014-D1A658BEE0B7}" />



    Now you can simply use it by calling Logger.Write(message) o or any other logging call. You can also add this listener to any category you want and optionally use formatters as well.

    Remember that you can activate your event provider by opening and "Admin" command prompt and running:

    logman create trace myApplication -p {D2FAAB3F-5D61-42B5-A014-D1A658BEE0B7} -o .\etwtrace.etl -ow
    logman start myApplication

    You can check that your data collector set is up and running by inspecting with Performance Monitor (perfmon.msc) and look into "Data Collector Sets\User Defined). There you may check for many properties of your process and also verify the etl file location which by default is in Windows\System32 folder with a file name like "etwtrace_000001.etl".

    After you are done with the tracing, simply end by typing:

    logman stop aExpense
    logman delete aExpense

    The information saved to the .etl file may be consumed by different tools described below and we can also use the tracerpt to generate an xml file and inspect the traced data or analyzing with WPA (Windows Performance Analyzer).