Browse Source

Improved log output. #114

undefined
Savorboard 6 years ago
parent
commit
b12e376ded
3 changed files with 28 additions and 31 deletions
  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 View File

@@ -67,15 +67,13 @@ namespace DotNetCore.CAP
} }
else 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); await SetFailedState(message, result.Exception, out bool stillRetry);


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


await SendAsync(message); await SendAsync(message);
} }
@@ -109,20 +107,11 @@ namespace DotNetCore.CAP
private Task SetFailedState(CapPublishedMessage message, Exception ex, out bool stillRetry) private Task SetFailedState(CapPublishedMessage message, Exception ex, out bool stillRetry)
{ {
IState newState = new FailedState(); 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); AddErrorReasonToContent(message, ex);
@@ -166,14 +155,18 @@ namespace DotNetCore.CAP
_logger.MessageHasBeenSent(du.TotalSeconds); _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( var eventData = new BrokerPublishErrorEventData(
operationId, operationId,
"", "",
ServersAddress, ServersAddress,
topic,
values,
message.Name,
message.Content,
ex, ex,
startTime, startTime,
du); du);


+ 4
- 0
src/DotNetCore.CAP/Internal/PublisherSentFailedException.cs View File

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

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


+ 11
- 11
src/DotNetCore.CAP/LoggerExtensions.cs View File

@@ -17,10 +17,10 @@ namespace DotNetCore.CAP
private static readonly Action<ILogger, string, string, string, Exception> _modelBinderFormattingException; private static readonly Action<ILogger, string, string, string, Exception> _modelBinderFormattingException;
private static readonly Action<ILogger, Exception> _consumerFailedWillRetry; private static readonly Action<ILogger, Exception> _consumerFailedWillRetry;
private static readonly Action<ILogger, double, Exception> _consumerExecuted; 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, string, Exception> _exceptionOccuredWhileExecuting;
private static readonly Action<ILogger, double, Exception> _messageHasBeenSent; 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() 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}'." "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, LogLevel.Debug,
3, 3,
"Retrying send a message: {Retries}...");
"The {Retries}th retrying send a message failed. message id: {MessageId} ");


_consumerExecuted = LoggerMessage.Define<double>( _consumerExecuted = LoggerMessage.Define<double>(
LogLevel.Debug, LogLevel.Debug,
@@ -78,17 +78,17 @@ namespace DotNetCore.CAP
_exceptionOccuredWhileExecuting = LoggerMessage.Define<string>( _exceptionOccuredWhileExecuting = LoggerMessage.Define<string>(
LogLevel.Error, LogLevel.Error,
6, 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>( _messageHasBeenSent = LoggerMessage.Define<double>(
LogLevel.Debug, LogLevel.Debug,
4, 4,
"Message published. Took: {Seconds} secs."); "Message published. Took: {Seconds} secs.");


_messagePublishException = LoggerMessage.Define<int>(
_messagePublishException = LoggerMessage.Define<int, string>(
LogLevel.Error, LogLevel.Error,
6, 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) public static void ConsumerExecutionFailedWillRetry(this ILogger logger, Exception ex)
@@ -96,9 +96,9 @@ namespace DotNetCore.CAP
_consumerFailedWillRetry(logger, ex); _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) public static void MessageHasBeenSent(this ILogger logger, double seconds)
@@ -106,9 +106,9 @@ namespace DotNetCore.CAP
_messageHasBeenSent(logger, seconds, null); _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) public static void ConsumerExecuted(this ILogger logger, double seconds)


Loading…
Cancel
Save