Commit 852357cc authored by Savorboard's avatar Savorboard

Improved log output. #114

parent 488df1b0
......@@ -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);
......
......@@ -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)
{
}
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment