Created
February 19, 2025 12:21
-
-
Save ramonsmits/9f7f169049999168412a3f60e37dbb50 to your computer and use it in GitHub Desktop.
NServiceBus logger extension method with excension support for format methods
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
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