<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />

LoggerExtensions

public static class LoggerExtensions
ILogger extension methods for common scenarios.
using System; using System.Runtime.CompilerServices; namespace Microsoft.Extensions.Logging { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class LoggerExtensions { private static readonly Func<FormattedLogValues, Exception, string> _messageFormatter = MessageFormatter; [System.Runtime.CompilerServices.NullableContext(2)] public static void LogDebug([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, EventId eventId, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Debug, eventId, exception, message, args); } public static void LogDebug(this ILogger logger, EventId eventId, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Debug, eventId, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogDebug([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Debug, exception, message, args); } public static void LogDebug(this ILogger logger, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Debug, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogTrace([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, EventId eventId, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Trace, eventId, exception, message, args); } public static void LogTrace(this ILogger logger, EventId eventId, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Trace, eventId, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogTrace([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Trace, exception, message, args); } public static void LogTrace(this ILogger logger, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Trace, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogInformation([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, EventId eventId, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Information, eventId, exception, message, args); } public static void LogInformation(this ILogger logger, EventId eventId, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Information, eventId, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogInformation([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Information, exception, message, args); } public static void LogInformation(this ILogger logger, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Information, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogWarning([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, EventId eventId, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Warning, eventId, exception, message, args); } public static void LogWarning(this ILogger logger, EventId eventId, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Warning, eventId, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogWarning([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Warning, exception, message, args); } public static void LogWarning(this ILogger logger, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Warning, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogError([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, EventId eventId, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Error, eventId, exception, message, args); } public static void LogError(this ILogger logger, EventId eventId, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Error, eventId, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogError([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Error, exception, message, args); } public static void LogError(this ILogger logger, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Error, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogCritical([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, EventId eventId, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Critical, eventId, exception, message, args); } public static void LogCritical(this ILogger logger, EventId eventId, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Critical, eventId, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void LogCritical([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Critical, exception, message, args); } public static void LogCritical(this ILogger logger, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(LogLevel.Critical, message, args); } public static void Log(this ILogger logger, LogLevel logLevel, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(logLevel, 0, null, message, args); } public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, [System.Runtime.CompilerServices.Nullable(2)] string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(logLevel, eventId, null, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void Log([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, LogLevel logLevel, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { logger.Log(logLevel, 0, exception, message, args); } [System.Runtime.CompilerServices.NullableContext(2)] public static void Log([System.Runtime.CompilerServices.Nullable(1)] this ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, string message, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { System.ThrowHelper.ThrowIfNull(logger, "logger"); logger.Log(logLevel, eventId, new FormattedLogValues(message, args), exception, _messageFormatter); } [return: System.Runtime.CompilerServices.Nullable(2)] public static IDisposable BeginScope(this ILogger logger, string messageFormat, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params object[] args) { System.ThrowHelper.ThrowIfNull(logger, "logger"); return logger.BeginScope(new FormattedLogValues(messageFormat, args)); } private static string MessageFormatter(FormattedLogValues state, Exception error) { return state.ToString(); } } }