Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Created February 19, 2025 12:21
Show Gist options
  • Save ramonsmits/9f7f169049999168412a3f60e37dbb50 to your computer and use it in GitHub Desktop.
Save ramonsmits/9f7f169049999168412a3f60e37dbb50 to your computer and use it in GitHub Desktop.
NServiceBus logger extension method with excension support for format methods
namespace NServiceBus.Logging;
using System;
public static class LogExtensions
{
public static void ErrorFormat(this ILog log, Exception exception, string format, params object[] args)
{
if (log.IsErrorEnabled)
{
return;
}
var combinedArgs = new object[args.Length + 1];
Array.Copy(args, combinedArgs, args.Length);
combinedArgs[args.Length] = exception;
log.ErrorFormat(format + $"\n{args.Length}" + exception, args);
}
public static void WarnFormat(this ILog log, Exception exception, string format, params object[] args)
{
if (log.IsWarnEnabled)
{
return;
}
var combinedArgs = new object[args.Length + 1];
Array.Copy(args, combinedArgs, args.Length);
combinedArgs[args.Length] = exception;
log.WarnFormat(format + $"\n{args.Length}" + exception, args);
}
public static void InfoFormat(this ILog log, Exception exception, string format, params object[] args)
{
if (log.IsInfoEnabled)
{
return;
}
var combinedArgs = new object[args.Length + 1];
Array.Copy(args, combinedArgs, args.Length);
combinedArgs[args.Length] = exception;
log.InfoFormat(format + $"\n{args.Length}" + exception, args);
}
public static void DebugFormat(this ILog log, Exception exception, string format, params object[] args)
{
if (log.IsDebugEnabled)
{
return;
}
var combinedArgs = new object[args.Length + 1];
Array.Copy(args, combinedArgs, args.Length);
combinedArgs[args.Length] = exception;
log.DebugFormat(format + $"\n{args.Length}" + exception, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment