Azure / diagnostics-eventflow

Microsoft Diagnostics EventFlow

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Application Insight Exception metadata type with EvenSource input

FunksMaName opened this issue · comments

I'm using EventSource as the input and AI as an output. Looking at the documentation
here, in the section that relates to applying metadata to AI exception type,

The name of the event property that carries the (unexpected) exception object. Note that (for maximum information fidelity) the expected type of the event property is System.Exception. In other words, the actual exception is expected to be part of event data, and not just a stringified version of it.

However, EventSource does not allow you to supply a System.Exception type, nor even an object as a message argument.

Any ideas on how to get this to work with EventSource? Is this even supported for this output type? I've looked at the tests related to outputs and there isn't anything there for AI exception logging. I'm hopping that I won't have to go down the route of writing a custom output to achieve this.

{ "type": "metadata", "include": "ProviderName == PersonalLoanWebService-ApplicationEvents && EventName == ApplicationError", "metadata": "exception", "exceptionProperty": "exception" }

Good question. I think you have a few options.

One is to use EventSource.Write method https://msdn.microsoft.com/en-us/library/system.diagnostics.tracing.eventsource.write(v=vs.110).aspx This should work well if you are using .NET Core, although I will be honest--I haven't tested it. Unfortunately with full framework there is a bug in the framework that prevents EventFlow from reading the event level correctly, so it is not a recommended way if you are using full (desktop) framework.

Second option is to use a different logging library (e.g. Microsoft.Extensions.Logging, Serilog) that allows you to pass arbitrary objects into EventFlow.

Yet another option is to use custom EventFlow input. That should be not nearly as troublesome as a custom output for AI. I think you could even integrate it with your EventSource e.g. by making the EventSource implement IObservable and using [NonEvent] methods to raise events that carry exception objects. This way you will have just one logging API, your EventSource, for the application to use.

Hope this is helpful

Update December 2018: when suggesting the first option (EventSource.Write) I did not realize that method puts serious restrictions on the kind of data it can consume. As pointed out by @erickbp below (thanks), this method cannot be used to attach Exception instances to EventFlow events.

Thanks for the input and solid suggestions. I am using framework 4.6.1 within ServiceFabric hence the first option won't work. I will go with the third option in my next iteration of the project in a couple of weeks time.

Hi, @karolz-ms
You said "One is to use EventSource.Write method". It's not clear to me how to pass an Exception object with this restrictions:

Write requires T to be an anonymous type or marked with the EventDataAttribute attribute. In addition, the properties of T must be a supported property type; either a simple framework type such as Int16, Int32, Int64, String, Guid, DateTime or DateTimeOffset, or an array of primitive types.

Am I looking at the wrong method?

Thank you

@erickbp you are correct and I was wrong. It is not possible to use EventSource.Write to produce an event carrying an Exception instance into EventFlow. Sorry about that!

The other two options are valid.

@karolz-ms thanks for clarifying, I followed your option number 2 (Serilog), thanks for suggesting it.

@erickbp cool. Just wanted to mention that EventFlow acquired a DiagnosticSource input recently, so that could be a good alternative if you want to keep non-framework dependencies to absolute minimum. That said, Serilog is great and has been supported by EventFlow almost since the beginning, so it is a fine choice too.

@karolz-ms good to know. Thank you