예제 #1
0
        /// <summary>
        /// Constructs the saga context, registering the context under the key
        /// <see cref="SagaContextItemKey"/> in the current message context
        /// (if one is available)
        /// </summary>
        public SagaContext(Guid id)
        {
            Id = id;

            if (MessageContext.HasCurrent)
            {
                messageContext = MessageContext.GetCurrent();
                messageContext.Items[SagaContextItemKey] = this;
            }
        }
        public static RebusConfigurer EnablePerMessageRunningTimeReporting(this RebusConfigurer configurer, TimeSpan threshold)
        {
            configurer.Events(e =>
            {
                e.MessageContextEstablished += (_, ctx) =>
                {
                    var sw = new System.Diagnostics.Stopwatch();

                    ctx.Items[StopwatchKey] = sw;
                    ctx.Disposed           += sw.Stop;

                    sw.Start();
                };

                e.AfterMessage += (_, ex, msg) =>
                {
                    if (!MessageContext.HasCurrent)
                    {
                        _Log.Warn("Missing message context at AfterMessage event?!");
                        return;
                    }

                    var ctx   = MessageContext.GetCurrent();
                    var tname = ctx.CurrentMessage?.GetType().Name;
                    var sw    = ctx.Items.GetValueOrDefault(StopwatchKey, null) as System.Diagnostics.Stopwatch;

                    if (sw == null)
                    {
                        _Log.Warn("Missing stopwatch at AfterMessage event?!");
                        return;
                    }

                    if (sw.Elapsed > threshold)
                    {
                        _Log.WarnFormat("Message processing took too long ({0}), for message of type '{1}' (threshold: {2})", sw.Elapsed, tname, threshold);
                    }
                    else
                    {
                        _Log.InfoFormat("Message processing took ({0}), for message of type '{1}'", sw.Elapsed, tname);
                    }
                };
            });
            return(configurer);
        }