/// <summary>
 /// Attaches the diagnostic component so it can be monitored
 /// </summary>
 public void AttachMonitor(HealthMonitor monitor)
 {
     // bind to healthMonitor
     healthMonitor = monitor;
     healthMonitor.EdgeServerCompleted += healthMonitor_EdgeServerChanged;
     healthMonitor.EventCreated += healthMonitor_EventCreated;
     healthMonitor.ReportSampledData += healthMonitor_ReportSampledData;
     healthMonitor.LatencyAlert += healthMonitor_LatencyAlert;
     healthMonitor.ReportTraceLogs += healthMonitor_ReportTraceLogs;
     // guarantee that we're not using an old instance anymore
     streamLoadedLog = null;
 }
        void healthMonitor_EventCreated(object sender, SimpleEventArgs<SmoothStreamingEvent> e)
        {
            try
            {
                switch (e.Result.EventType)
                {
                    case EventType.StreamStarted:
                        // we keep this object around for reference
                        var streamStartedLog = new VideoStartLog();
                        PopulateVideoEventLog(streamStartedLog);
                        if (streamLoadedLog != null)
                        {
                            streamStartedLog.EdgeIP = streamLoadedLog.EdgeIP;
                            streamStartedLog.ClientIP = streamLoadedLog.ClientIP;
                            streamStartedLog.VideoUrl = streamLoadedLog.VideoUrl;
                            streamStartedLog.MaxBitRate = streamLoadedLog.MaxBitRate;
                        }
                        streamStartedLog.MediaElementId = e.Result.MediaElementId;
                        streamStartedLog.IsLive = e.Result.IsLive;
                        SendLog(streamStartedLog);
                        break;
                    case EventType.StreamLoaded:
                        // we keep this object around for reference
                        streamLoadedLog = new VideoLoadLog();
                        PopulateVideoEventLog(streamLoadedLog);
                        streamLoadedLog.EdgeIP = healthMonitor.EdgeServer;
                        streamLoadedLog.ClientIP = healthMonitor.ClientIP;
                        streamLoadedLog.VideoUrl = e.Result.Data1;
                        streamLoadedLog.MediaElementId = e.Result.MediaElementId;
                        streamLoadedLog.IsLive = e.Result.IsLive;
                        {
                            double maxBitRate = 0;
                            if (double.TryParse(e.Result.Data2, out maxBitRate))
                                streamLoadedLog.MaxBitRate = maxBitRate;
                        }
                        //streamStartedLog.CDNBlocked = healthMonitor.AnonymousProxy;
                        SendLog(streamLoadedLog);
                        break;
                    case EventType.StreamEnded:
                        VideoStopLog streamEndedLog = new VideoStopLog();
                        PopulateVideoEventLog(streamEndedLog);
                        streamEndedLog.MediaElementId = e.Result.MediaElementId;
                        streamEndedLog.IsLive = e.Result.IsLive;
                        //streamEndedLog.VideoUrl = e.Result.Data1;
                        SendLog(streamEndedLog);
                        break;
                    case EventType.ClipStarted:
                        VideoClipStartedLog clipStartLog = new VideoClipStartedLog();
                        PopulateVideoEventLog(clipStartLog);
                        //clipStartLog.VideoUrl = e.Result.Data1;
                        clipStartLog.MediaElementId = e.Result.MediaElementId;
                        clipStartLog.IsLive = e.Result.IsLive;
                        SendLog(clipStartLog);
                        break;
                    case EventType.ClipEnded:
                        VideoClipEndedLog clipEndLog = new VideoClipEndedLog();
                        PopulateVideoEventLog(clipEndLog);
                        clipEndLog.MediaElementId = e.Result.MediaElementId;
                        clipEndLog.IsLive = e.Result.IsLive;
                        //clipEndLog.VideoUrl = e.Result.Data1;
                        SendLog(clipEndLog);
                        break;
                    case EventType.MediaFailed:
                        MediaFailedLog mediaFailed = new MediaFailedLog();
                        PopulateVideoEventLog(mediaFailed);
                        mediaFailed.Reason = e.Result.Data1;
                        mediaFailed.EdgeIP = healthMonitor.EdgeServer;
                        mediaFailed.IsLive = e.Result.IsLive;
                        mediaFailed.MediaElementId = e.Result.MediaElementId;

                        //mediaFailed.VideoUrl = e.Result.Data2;
                        //mediaFailed.VideoTimelineMarker = Convert.ToInt64(e.Result.Data3);
                        SendLog(mediaFailed);
                        break;
                    case EventType.FullScreenChanged:
                        Log fullScreenLog;
                        if (Convert.ToBoolean(Convert.ToInt32(e.Result.Value)))
                            fullScreenLog = CreateVideoLog(VideoLogTypes.FullScreenEntered);
                        else
                            fullScreenLog = CreateVideoLog(VideoLogTypes.FullScreenExit);
                        SendLog(fullScreenLog);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                LoggingService.Current.BroadcastException(ex);
            }
        }