Monday, 17 November 2014

Fun (or rather not) with EventSource

I’ve been an advocate of good logging in applications (especially server side ones) for many years, and today I’m working on something for myself and wanted to use the latest and greatest event logging framework so chose System.Diagnostics.Tracing (which has been around since .NET 4 days). I’ve used it before but today I came across an issue that had me stumped for some time.

I’d created a custom event source, and derived this from an interface so that I could inject an implementation in at runtime…

  public interface ILogger
void T1(string message);
void T2(string message);

public class Logger : EventSource, ILogger
public void T1(string message) { this.WriteEvent(1, message); }
public void T2(string message) { this.WriteEvent(2, message); }

But when I used PerfView to view my events there were none. After a lot of head scratching I tried a sample from Vance’s blog (which seems to be the main place to get any information about this feature) and of course that worked first time. I did some more fiddling in code and then stumbled across the reason as I was debugging the code. It’s worthy of a blog post, as this may well catch someone else out.

I’d created the interface and implemented it in the EventSource derived class so that I could mock out the logger code for testing – however this was the part that caught me out. When the code runs, the EventSource class builds a manifest that contains details of the "events" that are written out – and this uses the following bit of reflection to get all the methods that we want to expose as "events" in the ETW trace…

  MethodInfo[] methods = eventSourceType.GetMethods(BindingFlags.NonPublic | 
BindingFlags.Public |
BindingFlags.Instance |

I’ve highlighted the offending enum value above. This is saying "find me all methods whether public or not, defined on this Instance ONLY". So, because I’d created an interface and then implemented that interface, my methods were not found by the code that generates the manifest, and hence I wasn’t getting anything useful in the ETL file.

The net effect of this is that I cannot directly use an interface to define an interface for the events I want to emit. There are a few ways around this I can think up…

  • Hard-code the logger class as in the examples on Vance’s posts. Nope, not going to fly, I this stuff to be mockable/testable!
  • Create an EventSource derived class and add shim methods to call the actual logging methods
  • Write my own version of EventSource, seems like a lot of work for little gain!
  • Create a shim class that forwards all calls to the EventSource derived class

Of these I picked the last, as I do want my event source code to be mockable, and it seemed to be the least bad of the options, so I ended up with the following class...

  public class ActualLogger : EventSource
public void T1(string message) { this.WriteEvent(1, message); }
public void T2(string message) { this.WriteEvent(2, message); }
public static ActualLogger Instance { get { return _instance; } }
static ActualLogger _instance = new ActualLogger();

public class Logger : ILogger
public void T1(string message) { ActualLogger.Instance.T1(message); }
public void T2(string message) { ActualLogger.Instance.T2(message); }

Well, something like that anyway!

As an aside, if you want to look at the manifest that is generated from your EventSource derived class, there's a static method on EventSource that can be useful...

    var manifest = EventSource.GenerateManifest(typeof(ActualLogger), typeof(ActualLogger).Assembly.Location);

Hope this helps someone!


custom dissertation writing service said...

i am reading your blog and i realise i would be needing the same deal for my work too. you seem sesible. i like how you write it is very thorough keep updating

Oliver Maurice said...

You should check this article out. Its completely about oxford style of writing and you will need this info if you decide to attend one of the greatest universities in the world

AngelaHolt said...

This is really very nice post you shared, i like the post, thanks for sharing.. Sp flash tools Leapdroid Deezloader

Tyson Harold said...

Nice post.

poppup said...

This is really very nice post you shared, i like the post, thanks for sharing.. Sp flash tools deezloader android

meldaresearchusa said...

From our custom affordable term papers, students can hire the best writers and be assured to receive the best custom papers when they search purchase custom research paper written by experts.

Assignment Help said...

Your article is very nice to thank you for share this informative blog.
Auditing Assignment Help

KeiraDoltan said...

Most of the student Fear of Missing Deadline and therefore stressful for any kind of Academic work. If You Hire, Help With Assignment Then you will be able to meet Deadline Without any burden.

meldaresearch said...

Your search for Custom Research Paper Writing Services ends here. Welcome to the home of best essay writers who are qualified in diverse fields. Our
Research Paper Writing Service
is proof that we are right near you. It is easy, place your order and get original plagiarism free assignments. What’s more, we keep everything confidential.