Commit b12e376d authored by Savorboard's avatar Savorboard

Improved log output. #114

parent e53863a6
...@@ -67,15 +67,13 @@ namespace DotNetCore.CAP ...@@ -67,15 +67,13 @@ namespace DotNetCore.CAP
} }
else else
{ {
TracingError(operationId, message.Name, sendValues, result.Exception, startTime, stopwatch.Elapsed); TracingError(operationId, message, result, startTime, stopwatch.Elapsed);
_logger.MessagePublishException(message.Id, result.Exception);
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 ...@@ -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();
stillRetry = UpdateMessageForRetryAsync(message);
if (ex is PublisherSentFailedException) if (stillRetry)
{
stillRetry = false;
message.Retries = _options.FailedRetryCount; // not retry if PublisherSentFailedException
}
else
{ {
stillRetry = UpdateMessageForRetryAsync(message); _logger.ConsumerExecutionFailedWillRetry(ex);
if (stillRetry) return Task.CompletedTask;
{
_logger.ConsumerExecutionFailedWillRetry(ex);
return Task.CompletedTask;
}
} }
AddErrorReasonToContent(message, ex); AddErrorReasonToContent(message, ex);
...@@ -166,14 +155,18 @@ namespace DotNetCore.CAP ...@@ -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, message.Name,
values, message.Content,
ex, ex,
startTime, startTime,
du); du);
......
...@@ -7,6 +7,10 @@ namespace DotNetCore.CAP.Internal ...@@ -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)
{ {
} }
......
...@@ -17,10 +17,10 @@ namespace DotNetCore.CAP ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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)
......
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