Savorboard 6 лет назад
Родитель
Сommit
852357cc49
3 измененных файлов: 28 добавлений и 31 удалений
  1. +13
    -20
      src/DotNetCore.CAP/IPublishMessageSender.Base.cs
  2. +4
    -0
      src/DotNetCore.CAP/Internal/PublisherSentFailedException.cs
  3. +11
    -11
      src/DotNetCore.CAP/LoggerExtensions.cs

+ 13
- 20
src/DotNetCore.CAP/IPublishMessageSender.Base.cs Просмотреть файл

@@ -67,15 +67,13 @@ namespace DotNetCore.CAP
}
else
{
TracingError(operationId, message.Name, sendValues, result.Exception, startTime, stopwatch.Elapsed);

_logger.MessagePublishException(message.Id, result.Exception);
TracingError(operationId, message, result, startTime, stopwatch.Elapsed);

await SetFailedState(message, result.Exception, out bool stillRetry);

if (stillRetry)
{
_logger.SenderRetrying(3);
_logger.SenderRetrying(message.Id, message.Retries);

await SendAsync(message);
}
@@ -109,20 +107,11 @@ namespace DotNetCore.CAP
private Task SetFailedState(CapPublishedMessage message, Exception ex, out bool stillRetry)
{
IState newState = new FailedState();

if (ex is PublisherSentFailedException)
{
stillRetry = false;
message.Retries = _options.FailedRetryCount; // not retry if PublisherSentFailedException
}
else
stillRetry = UpdateMessageForRetryAsync(message);
if (stillRetry)
{
stillRetry = UpdateMessageForRetryAsync(message);
if (stillRetry)
{
_logger.ConsumerExecutionFailedWillRetry(ex);
return Task.CompletedTask;
}
_logger.ConsumerExecutionFailedWillRetry(ex);
return Task.CompletedTask;
}

AddErrorReasonToContent(message, ex);
@@ -166,14 +155,18 @@ namespace DotNetCore.CAP
_logger.MessageHasBeenSent(du.TotalSeconds);
}

private void TracingError(Guid operationId, string topic, string values, Exception ex, DateTimeOffset startTime, TimeSpan du)
private void TracingError(Guid operationId, CapPublishedMessage message, OperateResult result, DateTimeOffset startTime, TimeSpan du)
{
var ex = new PublisherSentFailedException(result.ToString(), result.Exception);

_logger.MessagePublishException(message.Id, result.ToString(), ex);

var eventData = new BrokerPublishErrorEventData(
operationId,
"",
ServersAddress,
topic,
values,
message.Name,
message.Content,
ex,
startTime,
du);


+ 4
- 0
src/DotNetCore.CAP/Internal/PublisherSentFailedException.cs Просмотреть файл

@@ -7,6 +7,10 @@ namespace DotNetCore.CAP.Internal
{
public class PublisherSentFailedException : Exception
{
public PublisherSentFailedException(string message) : base(message)
{
}

public PublisherSentFailedException(string message, Exception ex) : base(message, ex)
{
}


+ 11
- 11
src/DotNetCore.CAP/LoggerExtensions.cs Просмотреть файл

@@ -17,10 +17,10 @@ namespace DotNetCore.CAP
private static readonly Action<ILogger, string, string, string, Exception> _modelBinderFormattingException;
private static readonly Action<ILogger, Exception> _consumerFailedWillRetry;
private static readonly Action<ILogger, double, Exception> _consumerExecuted;
private static readonly Action<ILogger, int, Exception> _senderRetrying;
private static readonly Action<ILogger, int, int, Exception> _senderRetrying;
private static readonly Action<ILogger, string, Exception> _exceptionOccuredWhileExecuting;
private static readonly Action<ILogger, double, Exception> _messageHasBeenSent;
private static readonly Action<ILogger, int, Exception> _messagePublishException;
private static readonly Action<ILogger, int, string, Exception> _messagePublishException;

static LoggerExtensions()
{
@@ -60,10 +60,10 @@ namespace DotNetCore.CAP
"When call subscribe method, a parameter format conversion exception occurs. MethodName:'{MethodName}' ParameterName:'{ParameterName}' Content:'{Content}'."
);

_senderRetrying = LoggerMessage.Define<int>(
_senderRetrying = LoggerMessage.Define<int, int>(
LogLevel.Debug,
3,
"Retrying send a message: {Retries}...");
"The {Retries}th retrying send a message failed. message id: {MessageId} ");

_consumerExecuted = LoggerMessage.Define<double>(
LogLevel.Debug,
@@ -78,17 +78,17 @@ namespace DotNetCore.CAP
_exceptionOccuredWhileExecuting = LoggerMessage.Define<string>(
LogLevel.Error,
6,
"An exception occured while trying to store a message: '{MessageId}'. ");
"An exception occured while trying to store a message. message id: {MessageId}");

_messageHasBeenSent = LoggerMessage.Define<double>(
LogLevel.Debug,
4,
"Message published. Took: {Seconds} secs.");

_messagePublishException = LoggerMessage.Define<int>(
_messagePublishException = LoggerMessage.Define<int, string>(
LogLevel.Error,
6,
"An exception occured while publishing a message: '{MessageId}'. ");
"An exception occured while publishing a message, reason:{Reason}. message id:{MessageId}");
}

public static void ConsumerExecutionFailedWillRetry(this ILogger logger, Exception ex)
@@ -96,9 +96,9 @@ namespace DotNetCore.CAP
_consumerFailedWillRetry(logger, ex);
}

public static void SenderRetrying(this ILogger logger, int retries)
public static void SenderRetrying(this ILogger logger, int messageId, int retries)
{
_senderRetrying(logger, retries, null);
_senderRetrying(logger, messageId, retries, null);
}

public static void MessageHasBeenSent(this ILogger logger, double seconds)
@@ -106,9 +106,9 @@ namespace DotNetCore.CAP
_messageHasBeenSent(logger, seconds, null);
}

public static void MessagePublishException(this ILogger logger, int messageId, Exception ex)
public static void MessagePublishException(this ILogger logger, int messageId, string reason, Exception ex)
{
_messagePublishException(logger, messageId, ex);
_messagePublishException(logger, messageId, reason, ex);
}

public static void ConsumerExecuted(this ILogger logger, double seconds)


Загрузка…
Отмена
Сохранить