/// <summary>
        /// Send information about a request handled by the application.
        /// </summary>
        /// <param name="telemetryHelper">The <see cref="ITelemetryHelper"/> being extended.s</param>
        /// <param name="name">The request name.</param>
        /// <param name="token">The token with user identifiable information.</param>
        /// <param name="startTime">The time when the page was requested.</param>
        /// <param name="duration">The time taken by the application to handle the request.</param>
        /// <param name="responseCode">The response status code.</param>
        /// <param name="wasSuccessfull">True if the request was handled successfully by the application.</param>
        /// <param name="properties">Named string values you can use to search and classify events.</param>
        public static void TrackRequest(this ITelemetryHelper telemetryHelper, string name, string token, DateTimeOffset startTime, TimeSpan duration, string responseCode, bool wasSuccessfull, IDictionary <string, string> properties = null)
        {
            Uri url;

            try
            {
                url = new Uri(string.Format("cqrs://{0}", name));
            }
            catch
            {
                url = null;
            }

            string sessionId;

            try
            {
                sessionId = string.Format("{0}::{1}", properties["CorrelationId"], token);
            }
            catch
            {
                sessionId = null;
            }

            telemetryHelper.TrackRequest(name, url, token, startTime, duration, responseCode, wasSuccessfull, properties, sessionId);
        }
Esempio n. 2
0
        /// <summary>
        /// Send information about a request handled by the application.
        /// </summary>
        /// <param name="telemetryHelper">The <see cref="ITelemetryHelper"/> being extended.s</param>
        /// <param name="name">The request name.</param>
        /// <param name="token">The token with user identifiable information.</param>
        /// <param name="startTime">The time when the page was requested.</param>
        /// <param name="duration">The time taken by the application to handle the request.</param>
        /// <param name="responseCode">The response status code.</param>
        /// <param name="wasSuccessfull">True if the request was handled successfully by the application.</param>
        /// <param name="properties">Named string values you can use to search and classify events.</param>
        public static void TrackRequest <TAuthenticationToken>(this ITelemetryHelper telemetryHelper, string name, TAuthenticationToken token, DateTimeOffset startTime, TimeSpan duration, string responseCode, bool wasSuccessfull, IDictionary <string, string> properties = null)
            where TAuthenticationToken : ISingleSignOnToken
        {
            Uri url;

            try
            {
                url = new Uri(string.Format("cqrs://{0}", name));
            }
            catch
            {
                url = null;
            }

            telemetryHelper.TrackRequest(name, url, token == null ? null : token.Serialise(), startTime, duration, responseCode, wasSuccessfull, properties);
        }
Esempio n. 3
0
        /// <summary>
        /// Build a message handler that implements telemetry capturing as well as off thread handling.
        /// </summary>
        public virtual Action <TMessage> BuildTelemeteredActionHandler <TMessage, TAuthenticationToken>(ITelemetryHelper telemetryHelper, Action <TMessage> handler, bool holdMessageLock, string source)
            where TMessage : IMessage
        {
            Action <TMessage> registerableMessageHandler = message =>
            {
                DateTimeOffset startedAt      = DateTimeOffset.UtcNow;
                Stopwatch      mainStopWatch  = Stopwatch.StartNew();
                string         responseCode   = "200";
                bool           wasSuccessfull = true;

                string telemetryName       = message.GetType().FullName;
                var    telemeteredMessage  = message as ITelemeteredMessage;
                string messagePrefix       = null;
                object authenticationToken = null;
                var    @event = message as IEvent <TAuthenticationToken>;
                if (@event != null)
                {
                    messagePrefix       = "Event/";
                    telemetryName       = string.Format("{0}/{1}/{2}", telemetryName, @event.GetIdentity(), @event.Id);
                    authenticationToken = @event.AuthenticationToken;
                }
                else
                {
                    var command = message as ICommand <TAuthenticationToken>;
                    if (command != null)
                    {
                        messagePrefix       = "Command/";
                        telemetryName       = string.Format("{0}/{1}/{2}", telemetryName, command.GetIdentity(), command.Id);
                        authenticationToken = command.AuthenticationToken;
                    }
                }

                if (telemeteredMessage != null)
                {
                    telemetryName = telemeteredMessage.TelemetryName;
                }

                telemetryHelper.TrackEvent(string.Format("Cqrs/Handle/{0}{1}/Started", messagePrefix, telemetryName));

                try
                {
                    handler(message);
                }
                catch (Exception exception)
                {
                    telemetryHelper.TrackException(exception);
                    wasSuccessfull = false;
                    responseCode   = "500";
                    throw;
                }
                finally
                {
                    telemetryHelper.TrackEvent(string.Format("Cqrs/Handle/{0}{1}/Finished", messagePrefix, telemetryName));

                    mainStopWatch.Stop();
                    if (authenticationToken is ISingleSignOnToken)
                    {
                        telemetryHelper.TrackRequest
                        (
                            string.Format("Cqrs/Handle/{0}{1}", messagePrefix, telemetryName),
                            (ISingleSignOnToken)authenticationToken,
                            startedAt,
                            mainStopWatch.Elapsed,
                            responseCode,
                            wasSuccessfull,
                            new Dictionary <string, string> {
                            { "Type", source }
                        }
                        );
                    }
                    else if (authenticationToken is Guid)
                    {
                        telemetryHelper.TrackRequest
                        (
                            string.Format("Cqrs/Handle/{0}{1}", messagePrefix, telemetryName),
                            (Guid?)authenticationToken,
                            startedAt,
                            mainStopWatch.Elapsed,
                            responseCode,
                            wasSuccessfull,
                            new Dictionary <string, string> {
                            { "Type", source }
                        }
                        );
                    }
                    else if (authenticationToken is int)
                    {
                        telemetryHelper.TrackRequest
                        (
                            string.Format("Cqrs/Handle/{0}{1}", messagePrefix, telemetryName),
                            (int?)authenticationToken,
                            startedAt,
                            mainStopWatch.Elapsed,
                            responseCode,
                            wasSuccessfull,
                            new Dictionary <string, string> {
                            { "Type", source }
                        }
                        );
                    }
                    else
                    {
                        string token = authenticationToken == null ? null : authenticationToken.ToString();
                        telemetryHelper.TrackRequest
                        (
                            string.Format("Cqrs/Handle/{0}{1}", messagePrefix, telemetryName),
                            token,
                            startedAt,
                            mainStopWatch.Elapsed,
                            responseCode,
                            wasSuccessfull,
                            new Dictionary <string, string> {
                            { "Type", source }
                        }
                        );
                    }

                    telemetryHelper.Flush();
                }
            };

            return(BuildActionHandler(registerableMessageHandler, holdMessageLock));
        }
Esempio n. 4
0
        /// <summary>
        /// Build a message handler that implements telemetry capturing as well as off thread handling.
        /// </summary>
        public virtual Action <TMessage> BuildTelemeteredActionHandler <TMessage, TAuthenticationToken>(ITelemetryHelper telemetryHelper, Action <TMessage> handler, bool holdMessageLock, string source)
            where TMessage : IMessage
        {
            Action <TMessage> registerableMessageHandler = message =>
            {
                DateTimeOffset startedAt      = DateTimeOffset.UtcNow;
                Stopwatch      mainStopWatch  = Stopwatch.StartNew();
                string         responseCode   = "200";
                bool           wasSuccessfull = true;

                string telemetryName      = message.GetType().FullName;
                var    telemeteredMessage = message as ITelemeteredMessage;
                string messagePrefix      = null;
                var    @event             = message as IEvent <TAuthenticationToken>;
                if (@event != null)
                {
                    messagePrefix = "Event/";
                    telemetryName = string.Format("{0}/{1}", telemetryName, @event.Id);
                }
                else
                {
                    var command = message as ICommand <TAuthenticationToken>;
                    if (command != null)
                    {
                        messagePrefix = "Command/";
                        telemetryName = string.Format("{0}/{1}", telemetryName, command.Id);
                    }
                }

                if (telemeteredMessage != null)
                {
                    telemetryName = telemeteredMessage.TelemetryName;
                }

                telemetryHelper.TrackEvent(string.Format("Cqrs/Handle/{0}{1}/Started", messagePrefix, telemetryName));

                try
                {
                    handler(message);
                }
                catch (Exception exception)
                {
                    telemetryHelper.TrackException(exception);
                    wasSuccessfull = false;
                    responseCode   = "500";
                    throw;
                }
                finally
                {
                    telemetryHelper.TrackEvent(string.Format("Cqrs/Handle/{0}{1}/Finished", messagePrefix, telemetryName));

                    mainStopWatch.Stop();
                    telemetryHelper.TrackRequest
                    (
                        string.Format("Cqrs/Handle/{0}{1}", messagePrefix, telemetryName),
                        startedAt,
                        mainStopWatch.Elapsed,
                        responseCode,
                        wasSuccessfull,
                        new Dictionary <string, string> {
                        { "Type", source }
                    }
                    );

                    telemetryHelper.Flush();
                }
            };

            Action <TMessage> registerableHandler = registerableMessageHandler;

            if (!holdMessageLock)
            {
                registerableHandler = message =>
                {
                    Task.Factory.StartNewSafely(() =>
                    {
                        registerableMessageHandler(message);
                    });
                };
            }

            return(registerableHandler);
        }