Bläddra i källkod

refactor mq log

master
Savorboard 7 år sedan
förälder
incheckning
365fbe6b35
2 ändrade filer med 41 tillägg och 15 borttagningar
  1. +1
    -1
      src/DotNetCore.CAP/IConsumerClient.cs
  2. +40
    -14
      src/DotNetCore.CAP/IConsumerHandler.Default.cs

+ 1
- 1
src/DotNetCore.CAP/IConsumerClient.cs Visa fil

@@ -33,6 +33,6 @@ namespace DotNetCore.CAP

event EventHandler<MessageContext> OnMessageReceived;

event EventHandler<string> OnError;
event EventHandler<LogMessageEventArgs> OnLog;
}
}

+ 40
- 14
src/DotNetCore.CAP/IConsumerHandler.Default.cs Visa fil

@@ -23,6 +23,7 @@ namespace DotNetCore.CAP
private readonly IServiceProvider _serviceProvider;

private Task _compositeTask;

private bool _disposed;

public ConsumerHandler(
@@ -44,17 +45,18 @@ namespace DotNetCore.CAP

foreach (var matchGroup in groupingMatches)
Task.Factory.StartNew(() =>
{
using (var client = _consumerClientFactory.Create(matchGroup.Key))
{
RegisterMessageProcessor(client);
{
using (var client = _consumerClientFactory.Create(matchGroup.Key))
{
RegisterMessageProcessor(client);

client.Subscribe(matchGroup.Value.Select(x => x.Attribute.Name));
client.Subscribe(matchGroup.Value.Select(x => x.Attribute.Name));

client.Listening(_pollingDelay, _cts.Token);
}
}, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
_compositeTask = Task.CompletedTask;
client.Listening(_pollingDelay, _cts.Token);
}
}, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

_compositeTask = Task.CompletedTask;
}

public void Dispose()
@@ -62,13 +64,10 @@ namespace DotNetCore.CAP
if (_disposed)
return;
_disposed = true;

_logger.ServerShuttingDown();
_cts.Cancel();

try
{
_compositeTask.Wait(TimeSpan.FromSeconds(10));
_compositeTask.Wait(TimeSpan.FromSeconds(2));
}
catch (AggregateException ex)
{
@@ -105,7 +104,34 @@ namespace DotNetCore.CAP
Pulse();
};

client.OnError += (sender, reason) => { _logger.MessageQueueError(reason); };
client.OnLog += WriteLog;
}

private void WriteLog(object sender, LogMessageEventArgs logmsg)
{
switch (logmsg.LogType)
{
case MqLogType.ConsumerCancelled:
_logger.LogWarning("RabbitMQ consumer cancelled. reason: " + logmsg.Reason);
break;
case MqLogType.ConsumerRegistered:
_logger.LogInformation("RabbitMQ consumer registered. " + logmsg.Reason);
break;
case MqLogType.ConsumerUnregistered:
_logger.LogWarning("RabbitMQ consumer unregistered. reason: " + logmsg.Reason);
break;
case MqLogType.ConsumerShutdown:
_logger.LogWarning("RabbitMQ consumer shutdown. reason:" + logmsg.Reason);
break;
case MqLogType.ConsumeError:
_logger.LogError("Kakfa client consume error. reason:" + logmsg.Reason);
break;
case MqLogType.ServerConnError:
_logger.LogCritical("Kafka server connection error. reason:" + logmsg.Reason);
break;
default:
throw new ArgumentOutOfRangeException();
}
}

private static void StoreMessage(IServiceScope serviceScope, MessageContext messageContext)


Laddar…
Avbryt
Spara