I believe the first of the code examples is a short-hand Singleton implementation and I'm not sure if it is thread safe. As the class, EventSourceLogger derives from EventSource, which is not static, this class can not just be a static class itself (which would have been even more convenient - tradeoffs between singletion and static notwithstanding).
I reverted from a more thorough specific known thread safe implementation to this implementation for brevity - after liking the look of this approach in a colleagues code. What have I lost?
Current implementation
[EventSource(Name = "EventSourceLogger")]
public class EventSourceLogger : EventSource
{
public static readonly EventSourceLogger Logger = new EventSourceLogger();`
}
Previous implementation
[EventSource(Name = "EventSourceLogger")]
public class EventSourceLogger : EventSource
{
private static EventSourceLogger instance;
private EventSourceLogger()
{
}
public static EventSourceLogger Instance
{
get
{
if (instance == null)
{
instance = new EventSourceLogger();
}
return instance;
}
}
}