jonwagner / EventSourceProxy

EventSourceProxy (ESP) is the easiest way to add scalable Event Tracing for Windows (ETW) logging to your .NET program
Other
97 stars 20 forks source link

How to supply event Id? #26

Closed aliostad closed 10 years ago

aliostad commented 10 years ago

Hi and thanks for a great library. We are planning to use this in production.

There is one thing that we'd like to do and is the fact that we hardly ever log individualised traces, instead we have a single interface which we use as our logger eg:

    [EventSourceImplementation(Name = "Stuff")]
    public interface ILogger
    {
        [Event(1001, Level = EventLevel.Critical)]
        void WriteCritical(string message, int managedThreadId);

        [Event(1002, Level = EventLevel.Error)]
        void WriteError(string message, int managedThreadId);

        ...
    }

This works fine for us - other the fact that we have to hardcode the EventId at the attribute level. I understand that this attribute is provided by the .NET Framework (and is not optional) but what we need is to be able to pass EventId as a variable in the method so that along with the error, we can send the error event Id which is later used by the SCOM monitoring.

So my idea is that if a method has a integer parameter named e.g. etwEventId (which is unlikely to accidentally collide with another parameter not intended for this purpose), its value will override the event id defined in the attribute. Even better, this parameter can be defined as nullable with default of null so does not have to be supplied, eg.:

[Event(1002, Level = EventLevel.Error)]
void WriteError(string message, int managedThreadId, int? etwEventId = null);

I am happy to supply the PR but I guess you might want to implement it the way fits your library best hence I am raising it instead of just sending you PR.

So what are your thoughts?

agehrke commented 10 years ago

Doesn't that defeat the whole purpose of semantic logging?

If you only have like 4-5 logging methods why use EventSourceProxy at all? Just implement an EventSource manually?

aliostad commented 10 years ago

Mainly because CorrelationId does not flow in async methods when we do that. But using the library, it does, I guess it emits some IL to make sure it happens.

aliostad commented 10 years ago

We can use alternative methods of setting the correlation Id but we would love to keep using Trace.CorrelationManager.ActivityId

jonwagner commented 10 years ago

Unfortunately the underlying .NET EventSource class requires that EventId is constant when it creates the manifest for the event stream.

For the style of logging that you want, I'd recommend something like nLog or Log4Net

aliostad commented 10 years ago

OK then, it seems like we have been using the wrong tool. In any case, thanks again for the good work and keep it up.