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:

[TestMethod]

public void when_inspecting_myCompanyEventSource()

{

    EventSourceAnalyzer.InspectAll(MyCompanyEventSource.Log);

}       

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:

[Event]

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:

 

[Event]

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:

 

[TestMethod]

public void when_inspecting_myCompanyEventSource_with_typeMapping_off()

{

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

    analyzer.Inspect(MyCompanyEventSource.Log);

}

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

 

 

 

 

 

 

No Comments