<commit_before>﻿
using NServiceBus;
using NServiceBus.Logging;
using NServiceBus.MessageInterfaces;
using NServiceBus.ObjectBuilder;
using NServiceBus.Pipeline;
using NServiceBus.Pipeline.Contexts;
using NServiceBus.Sagas;
using NServiceBus.Settings;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aggregates.Extensions;

namespace Aggregates.Internal
{
    class AsyncronizedInvoke : IBehavior<IncomingContext>
    {
        private static ILog Logger = LogManager.GetLogger<AsyncronizedInvoke>();
        private readonly IBus _bus;
        private readonly ReadOnlySettings _settings;
        private readonly IMessageMapper _mapper;
        private readonly Int32 _slowAlert;


        public AsyncronizedInvoke(IBus bus, ReadOnlySettings settings, IMessageMapper mapper)
        {
            _bus = bus;
            _settings = settings;
            _mapper = mapper;
            _slowAlert = _settings.Get<Int32>("SlowAlertThreshold");
            
        }

        public void Invoke(IncomingContext context, Action next)
        {
            ActiveSagaInstance saga;

            if (context.TryGet(out saga) && saga.NotFound && saga.SagaType == context.MessageHandler.Instance.GetType())
            {
                next();
                return;
            }

            var messageHandler = context.Get<AsyncMessageHandler>();
            Task.Run((Func<Task>)(async () =>
            {
                var s = Stopwatch.StartNew();

                var handleContext = new HandleContext { Bus = _bus, Context = context, Mapper = _mapper };
                await messageHandler.Invocation(messageHandler.Handler, context.IncomingLogicalMessage.Instance, handleContext);

                if(s.ElapsedMilliseconds > _slowAlert)
                    Logger.WriteFormat(LogLevel.Warn, " - SLOW ALERT - Executing command {0} on handler {1} took {2} ms", context.IncomingLogicalMessage.MessageType.FullName, ((object)messageHandler.Handler).GetType().FullName, s.ElapsedMilliseconds);
                else
                    Logger.WriteFormat(LogLevel.Debug, "Executing command {0} on handler {1} took {2} ms", context.IncomingLogicalMessage.MessageType.FullName, ((object)messageHandler.Handler).GetType().FullName, s.ElapsedMilliseconds);
                
            })).Wait();

            next();
        }
    }
}<commit_msg>Additional log message before invoking handlers<commit_after>﻿
using NServiceBus;
using NServiceBus.Logging;
using NServiceBus.MessageInterfaces;
using NServiceBus.ObjectBuilder;
using NServiceBus.Pipeline;
using NServiceBus.Pipeline.Contexts;
using NServiceBus.Sagas;
using NServiceBus.Settings;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aggregates.Extensions;

namespace Aggregates.Internal
{
    class AsyncronizedInvoke : IBehavior<IncomingContext>
    {
        private static ILog Logger = LogManager.GetLogger<AsyncronizedInvoke>();
        private readonly IBus _bus;
        private readonly ReadOnlySettings _settings;
        private readonly IMessageMapper _mapper;
        private readonly Int32 _slowAlert;


        public AsyncronizedInvoke(IBus bus, ReadOnlySettings settings, IMessageMapper mapper)
        {
            _bus = bus;
            _settings = settings;
            _mapper = mapper;
            _slowAlert = _settings.Get<Int32>("SlowAlertThreshold");
            
        }

        public void Invoke(IncomingContext context, Action next)
        {
            ActiveSagaInstance saga;

            if (context.TryGet(out saga) && saga.NotFound && saga.SagaType == context.MessageHandler.Instance.GetType())
            {
                next();
                return;
            }

            var messageHandler = context.Get<AsyncMessageHandler>();
            Task.Run((Func<Task>)(async () =>
            {
                var s = Stopwatch.StartNew();
                Logger.WriteFormat(LogLevel.Debug, "Executing command {0} on handler {1}", context.IncomingLogicalMessage.MessageType.FullName, ((object)messageHandler.Handler).GetType().FullName);

                var handleContext = new HandleContext { Bus = _bus, Context = context, Mapper = _mapper };
                await messageHandler.Invocation(messageHandler.Handler, context.IncomingLogicalMessage.Instance, handleContext);
                s.Stop();

                if(s.ElapsedMilliseconds > _slowAlert)
                    Logger.WriteFormat(LogLevel.Warn, " - SLOW ALERT - Executing command {0} on handler {1} took {2} ms", context.IncomingLogicalMessage.MessageType.FullName, ((object)messageHandler.Handler).GetType().FullName, s.ElapsedMilliseconds);
                else
                    Logger.WriteFormat(LogLevel.Debug, "Executing command {0} on handler {1} took {2} ms", context.IncomingLogicalMessage.MessageType.FullName, ((object)messageHandler.Handler).GetType().FullName, s.ElapsedMilliseconds);
                
            })).Wait();

            next();
        }
    }
}
{'fraction_non_alphanumeric': 0.09235545207605743, 'fraction_numerical': 0.004268529297632906, 'mean_word_length': 2.658623136976579, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4663074', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1328, 'n_words': 299}