Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Created July 25, 2023 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramonsmits/2e344fc4f03f68f8d7792a46dde61262 to your computer and use it in GitHub Desktop.
Save ramonsmits/2e344fc4f03f68f8d7792a46dde61262 to your computer and use it in GitHub Desktop.
NServiceBus - Report processing durations for failed messages
//
// 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