Esempio n. 1
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. 2
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);
        }