private void ProfileEvent <E>(string eventName, object sender, E args)
            where E : EventArgs
        {
            if (!ProfilingEnabled)
            {
                return;
            }

            var message = new Messages.ConnectionActivity
            {
                ConnectionName      = this.Connection.ConnectionName,
                Name                = eventName,
                ElapsedMilliseconds = 1,
                Arguments           = args
            };

            InspectorContext.MessageBroker.Publish(message);

            var startTime = DateTime.UtcNow;

            Timeline(eventName, new TimerResult
            {
                StartTime = startTime,
                Offset    = startTime.Subtract(InspectorContext.TimerStrategy().RequestStart.ToUniversalTime()),
                Duration  = TimeSpan.FromMilliseconds(1)
            });
        }
        private T ProfileActivity <T>(string activityName, Func <T> activity, object arguments = null)
        {
            if (!ProfilingEnabled)
            {
                return(activity());
            }

            var startTime = DateTime.UtcNow;
            var stopwatch = Stopwatch.StartNew();
            var result    = activity();

            stopwatch.Stop();

            var message = new Messages.ConnectionActivity
            {
                ConnectionName      = this.Connection.ConnectionName,
                Name                = activityName,
                ElapsedMilliseconds = stopwatch.ElapsedMilliseconds,
                Arguments           = arguments,
                Results             = result
            };

            InspectorContext.MessageBroker.Publish(message);

            Timeline(activityName, new TimerResult
            {
                StartTime = startTime,
                Offset    = startTime.Subtract(InspectorContext.TimerStrategy().RequestStart.ToUniversalTime()),
                Duration  = stopwatch.Elapsed
            });

            return(result);
        }
        private Task ProfileActivity(string activityName, Func <Task> activity, object arguments = null)
        {
            if (!ProfilingEnabled)
            {
                return(activity());
            }

            var       startTime = DateTime.UtcNow;
            Stopwatch stopwatch = null;

            return(Task.Factory.StartNew(() =>
            {
                startTime = DateTime.UtcNow;
                stopwatch = Stopwatch.StartNew();
            }).ContinueWith(
                       _ => activity()
                       ).ContinueWith(task =>
            {
                stopwatch.Stop();

                var message = new Messages.ConnectionActivity
                {
                    ConnectionName = this.Connection.ConnectionName,
                    Name = activityName,
                    ElapsedMilliseconds = stopwatch.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L * 1000L)),
                    Arguments = arguments,
                    //Results = task.Result
                };

                InspectorContext.MessageBroker.Publish(message);

                Timeline(activityName, new TimerResult
                {
                    StartTime = startTime,
                    Offset = startTime.Subtract(InspectorContext.TimerStrategy().RequestStart.ToUniversalTime()),
                    Duration = stopwatch.Elapsed
                });

                return task;
            }));
        }