public static Guid GetCorrelationId(this Exception ex) { List <object> correlationIds = ex.GetNamedProperty(InternalExceptionExtensions.CorrelationIdLabel); if (correlationIds.Any()) { return((Guid)(correlationIds.First())); } // If exception does not have CorrelationId assigned let's try to get it from System.Diagnostics.Trace.CorrelationManager.ActivityId if (System.Diagnostics.Trace.CorrelationManager.ActivityId == Guid.Empty) { // If System.Diagnostics.Trace.CorrelationManager.ActivityId is not set, then let's set it and use it System.Diagnostics.Trace.CorrelationManager.ActivityId = Guid.NewGuid(); } ex.AddCorrelationId(System.Diagnostics.Trace.CorrelationManager.ActivityId); return(System.Diagnostics.Trace.CorrelationManager.ActivityId); }