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

ActivityID is empty when running in Azure Emulator with IIS #24

Closed aboobolaky closed 10 years ago

aboobolaky commented 10 years ago

Hi, First of all, my use of the EventSourceProxy has been great so far. I did not have any issues, until I deployed the whole app to the cloud. I'm using the eventsource proxy to create etw events and using SLAB and it's related azure sink to put the results in table storage. For dev, im using the Full Emulator with IIS Express. All the log entries in the local development storage contain an activityid, which was all cool

However, when the azure emulator is configured to host the website in IIS, the rows do not contain activity id. My gut feeling is that the ActivityId is empty and SLAB will not create a new field

Going back to the standard implementation of an eventsource (without the EventSourceProxy), this behaviour disappears. The activityid can be seen regardless (whether it's emulator with iisexpress or emulator with iis)

I've created a really simple webapi and a cloud service project to test this and can be downloaded from http://1drv.ms/1zcI3yL

I'd be grateful if you could help on this.

Regards Aboo

aboobolaky commented 10 years ago

after some more investigation, i opened up perfview and started collecting the etw events. the activity id was present. whilst perfview was collecting the events, log entries with activity ids were recorded in the local storage (using IIS and azure emulator)

I'm going a bit bonkers on this.. does it look like this has something to do with IIS? It works without any issues with iis express.. :(

jonwagner commented 10 years ago

That's pretty odd. I'll have to download your test project and take a look. I should have time to try it out next week.

aboobolaky commented 10 years ago

Hi Jon, Yeah it’s very odd indeed. Please let me know what your thoughts are ☺

Regards Aboo

From: Jon Wagner [mailto:notifications@github.com] Sent: 28 July 2014 17:46 To: jonwagner/EventSourceProxy Cc: Aboo Bolaky Subject: Re: [EventSourceProxy] ActivityID is empty when running in Azure Emulator with IIS (#24)

That's pretty odd. I'll have to download your test project and take a look. I should have time to try it out next week.

— Reply to this email directly or view it on GitHubhttps://github.com/jonwagner/EventSourceProxy/issues/24#issuecomment-50364360.

jonwagner commented 10 years ago

Looking into this...

ESP manages its own activity scope, but if you don't define it, it looks at System.Diagnostics.Trace.CorrelactionManager.ActivityID. That seems to be Guid.Null when running under IIS.

I think that the last time I dove into the .NET code, I saw code that only sets the activity id if end-to-end tracing is enabled. That would explain why you're seeing the ID when perfview is enabled. Let me see if I can find that code again.

jonwagner commented 10 years ago

The .NET Framework is a mess when it comes to Trace.CorrleationManager.ActivityId. Many different places read from it, some places write to it, and some places clear it when you switch threads. ESP uses this as the default source of activity IDs.

I think you are on the right track with your ActivityHandler class. The best thing to do would be to propagate the activityID from the HttpRequestMessage into the ESP framework. You can do it with this code:

public class ActivityHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        using (var activity = new EventActivityScope(request.GetCorrelationId()))
        {
            return await base.SendAsync(request, cancellationToken);
        }
    }
}

It seems to work under IIS. Let me know if it works for you.

aboobolaky commented 10 years ago

Hi Jon, Apologies for the late reply. Based on my investigations, the reason why it works in IISExpress is because end-to-end tracing is enabled by default. I didn't want to mirror this option whilst running under IIS. Under IIS, the activity ID can be set using 2 options

1:

Using the EventActivityScope as mentioned in your previous post

2:

By explicitly setting the ActivityId using Trace.CorrelationManager.ActivityId in the Delegating Handler

protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { Guid correlationId = request.GetCorrelationId(); Trace.CorrelationManager.ActivityId = correlationId; return await base.SendAsync(request, cancellationToken); }

Thanks for the help :)