<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Helsenorge.Messaging.Abstractions;
using Helsenorge.Messaging.ServiceBus.Receivers;
using Helsenorge.Registries.Abstractions;
using Microsoft.Extensions.Logging;

namespace Helsenorge.Messaging
{
	/// <summary>
	/// Default implementation for <see cref="IMessagingServer"/>
	/// This must be hosted as a singleton in order to leverage connection pooling against the message bus
	/// </summary>
	public sealed class MessagingServer : MessagingCore, IMessagingServer, IMessagingNotification
	{
		private readonly ILogger _logger;
		private readonly ILoggerFactory _loggerFactory;
		private readonly ConcurrentBag<MessageListener> _listeners = new ConcurrentBag<MessageListener>();
		private readonly ConcurrentBag<Task> _tasks = new ConcurrentBag<Task>();
		private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();

		private Action<IncomingMessage> _onAsynchronousMessageReceived;
		private Action<IncomingMessage> _onAsynchronousMessageReceivedStarting;
		private Action<IncomingMessage> _onAsynchronousMessageReceivedCompleted;

		private Func<IncomingMessage, XDocument> _onSynchronousMessageReceived;
		private Action<IncomingMessage> _onSynchronousMessageReceivedCompleted;
		private Action<IncomingMessage> _onSynchronousMessageReceivedStarting;

		private Action<IMessagingMessage> _onErrorMessageReceived;
		private Action<IMessagingMessage, Exception> _onUnhandledException;
		private Action<IMessagingMessage, Exception> _onHandledException;

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="settings">Set of options to use</param>
		/// <param name="logger">Logger used for generic messages</param>
		/// <param name="loggerFactory">Logger Factory</param>
		/// <param name="collaborationProtocolRegistry">Reference to the collaboration protocol registry</param>
		/// <param name="addressRegistry">Reference to the address registry</param>
		public MessagingServer(
			MessagingSettings settings,
			ILogger logger,
			ILoggerFactory loggerFactory,
			ICollaborationProtocolRegistry collaborationProtocolRegistry,
			IAddressRegistry addressRegistry) : base(settings, collaborationProtocolRegistry, addressRegistry)
		{
			if (logger == null) throw new ArgumentNullException(nameof(logger));
			if (loggerFactory == null) throw new ArgumentNullException(nameof(loggerFactory));

			_logger = logger;
			_loggerFactory = loggerFactory;
		}

		/// <summary>
		/// Start the server
		/// </summary>
		public void Start()
		{
			_logger.LogInformation("Messaging Server starting up");
			for (var i = 0; i < Settings.ServiceBus.Asynchronous.ProcessingTasks; i++)
			{
				_listeners.Add(new AsynchronousMessageListener(ServiceBus, _loggerFactory.CreateLogger($"AsyncListener_{i}"), this));
			}
			for (var i = 0; i < Settings.ServiceBus.Synchronous.ProcessingTasks; i++)
			{<commit_msg>Ryddet i feil med Microsoft.Extentsions.Logging.Abstractions innført ved rollback av tidligere kode<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Helsenorge.Messaging.Abstractions;
using Helsenorge.Messaging.ServiceBus.Receivers;
using Helsenorge.Registries.Abstractions;
using Microsoft.Extensions.Logging;

namespace Helsenorge.Messaging
{
	/// <summary>
	/// Default implementation for <see cref="IMessagingServer"/>
	/// This must be hosted as a singleton in order to leverage connection pooling against the message bus
	/// </summary>
	public sealed class MessagingServer : MessagingCore, IMessagingServer, IMessagingNotification
	{
		private readonly ILogger _logger;
		private readonly ILoggerFactory _loggerFactory;
		private readonly ConcurrentBag<MessageListener> _listeners = new ConcurrentBag<MessageListener>();
		private readonly ConcurrentBag<Task> _tasks = new ConcurrentBag<Task>();
		private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();

		private Action<IncomingMessage> _onAsynchronousMessageReceived;
		private Action<IncomingMessage> _onAsynchronousMessageReceivedStarting;
		private Action<IncomingMessage> _onAsynchronousMessageReceivedCompleted;

		private Func<IncomingMessage, XDocument> _onSynchronousMessageReceived;
		private Action<IncomingMessage> _onSynchronousMessageReceivedCompleted;
		private Action<IncomingMessage> _onSynchronousMessageReceivedStarting;

		private Action<IMessagingMessage> _onErrorMessageReceived;
		private Action<IMessagingMessage, Exception> _onUnhandledException;
		private Action<IMessagingMessage, Exception> _onHandledException;

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="settings">Set of options to use</param>
		/// <param name="logger">Logger used for generic messages</param>
		/// <param name="loggerFactory">Logger Factory</param>
		/// <param name="collaborationProtocolRegistry">Reference to the collaboration protocol registry</param>
		/// <param name="addressRegistry">Reference to the address registry</param>
		public MessagingServer(
			MessagingSettings settings,
			ILogger logger,
			ILoggerFactory loggerFactory,
			ICollaborationProtocolRegistry collaborationProtocolRegistry,
			IAddressRegistry addressRegistry) : base(settings, collaborationProtocolRegistry, addressRegistry)
		{
			if (logger == null) throw new ArgumentNullException(nameof(logger));
			if (loggerFactory == null) throw new ArgumentNullException(nameof(loggerFactory));

			_logger = logger;
			_loggerFactory = loggerFactory;
		}

		/// <summary>
		/// Start the server
		/// </summary>
		public void Start()
		{
			_logger.LogInformation("Messaging Server starting up");

			for (var i = 0; i < Settings.ServiceBus.Asynchronous.ProcessingTasks; i++)
			{
				_listeners.Add(new AsynchronousMessageListener(ServiceBus, _loggerFactory.CreateLogger($"AsyncListener_{i}"), this));
			}
			for (var i = 0; i < Settings.ServiceBus.Synchronous.ProcessingTasks; i++)
			{
{'fraction_non_alphanumeric': 0.09155728407065306, 'fraction_numerical': 0.0006481931615621455, 'mean_word_length': 5.89608938547486, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 63, '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': '2483591', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1640, 'n_words': 412}