Created
July 25, 2023 16:27
-
-
Save ramonsmits/2e344fc4f03f68f8d7792a46dde61262 to your computer and use it in GitHub Desktop.
NServiceBus - Report processing durations for failed messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// endpointConfiguration.Pipeline.Register(new ReportFailedDurationsBehavior(), nameof(ReportFailedDurationsBehavior)); | |
// | |
class ReportFailedDurationsBehavior : IBehavior<IIncomingLogicalMessageContext, IIncomingLogicalMessageContext> | |
{ | |
static readonly ILog Log = LogManager.GetLogger(typeof(ReportFailedDurationsBehavior)); | |
static readonly bool IsDebugEnabled = Log.IsDebugEnabled; | |
static readonly TimeSpan WarningThreshold = TimeSpan.FromSeconds(30); | |
public async Task Invoke(IIncomingLogicalMessageContext context, Func<IIncomingLogicalMessageContext, Task> next) | |
{ | |
var start = Stopwatch.StartNew(); | |
try | |
{ | |
await next(context).ConfigureAwait(false); | |
} | |
catch | |
{ | |
var duration = start.Elapsed; | |
if (WarningThreshold > duration) | |
{ | |
Log.WarnFormat("{0}: Failed after a processing duration {1} exceeds configured warning threshold ({2})", context.MessageId, duration, WarningThreshold); | |
} | |
if (IsDebugEnabled) | |
{ | |
Log.DebugFormat("{0}: Failed after a processing duration {1}", context.MessageId, duration); | |
} | |
throw; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment