Skip to content

Commit

Permalink
Trace, Debug, Information... InterpolatedStringHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Oct 18, 2023
1 parent 94199e9 commit 90c92a1
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 71 deletions.
113 changes: 58 additions & 55 deletions src/ZLogger/ZLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,149 +1,152 @@
using System;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;

namespace ZLogger
{
public static partial class ZLoggerExtensions
{
public static void ZLog(this ILogger logger, LogLevel logLevel, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog(this ILogger logger, LogLevel logLevel, [InterpolatedStringHandlerArgument("logger", "logLevel")]ref ZLoggerInterpolatedStringHandler message)
{
ZLog(logger, logLevel, default, null, ref message);
}

public static void ZLog(this ILogger logger, LogLevel logLevel, EventId eventId, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog(this ILogger logger, LogLevel logLevel, EventId eventId, [InterpolatedStringHandlerArgument("logger", "logLevel")]ref ZLoggerInterpolatedStringHandler message)
{
ZLog(logger, logLevel, eventId, null, ref message);
}

public static void ZLog(this ILogger logger, LogLevel logLevel, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog(this ILogger logger, LogLevel logLevel, Exception? exception, [InterpolatedStringHandlerArgument("logger", "logLevel")]ref ZLoggerInterpolatedStringHandler message)
{
ZLog(logger, logLevel, default, exception, ref message);
}

public static void ZLog(this ILogger logger, LogLevel logLevel, EventId eventId, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog(this ILogger logger, LogLevel logLevel, EventId eventId, Exception? exception, [InterpolatedStringHandlerArgument("logger", "logLevel")]ref ZLoggerInterpolatedStringHandler message)
{
using var state = message.GetStateAndClear();
logger.Log(logLevel, eventId, state, exception, (state, ex) => state.ToString());
if (message.IsLoggerEnabled)
{
using var state = message.GetStateAndClear();
logger.Log(logLevel, eventId, state, exception, static (state, ex) => state.ToString());
}
}

public static void ZLogTrace(this ILogger logger, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogTrace(this ILogger logger, ref ZLoggerTraceInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Trace, default, null, ref message);
ZLog(logger, LogLevel.Trace, default, null, ref message.innerHandler);
}

public static void ZLogTrace(this ILogger logger, EventId eventId, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogTrace(this ILogger logger, EventId eventId, ref ZLoggerTraceInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Trace, eventId, null, ref message);
ZLog(logger, LogLevel.Trace, eventId, null, ref message.innerHandler);
}

public static void ZLogTrace(this ILogger logger, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogTrace(this ILogger logger, Exception? exception, ref ZLoggerTraceInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Trace, default, exception, ref message);
ZLog(logger, LogLevel.Trace, default, exception, ref message.innerHandler);
}

public static void ZLogTrace(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogTrace(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerTraceInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Trace, eventId, exception, ref message);
ZLog(logger, LogLevel.Trace, eventId, exception, ref message.innerHandler);
}

public static void ZLogDebug(this ILogger logger, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogDebug(this ILogger logger, ref ZLoggerDebugInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Debug, default, null, ref message);
ZLog(logger, LogLevel.Debug, default, null, ref message.innerHandler);
}

public static void ZLogDebug(this ILogger logger, EventId eventId, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogDebug(this ILogger logger, EventId eventId, ref ZLoggerDebugInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Debug, eventId, null, ref message);
ZLog(logger, LogLevel.Debug, eventId, null, ref message.innerHandler);
}

public static void ZLogDebug(this ILogger logger, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogDebug(this ILogger logger, Exception? exception, ref ZLoggerDebugInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Debug, default, exception, ref message);
ZLog(logger, LogLevel.Debug, default, exception, ref message.innerHandler);
}

public static void ZLogDebug(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogDebug(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerDebugInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Debug, eventId, exception, ref message);
ZLog(logger, LogLevel.Debug, eventId, exception, ref message.innerHandler);
}

public static void ZLogInformation(this ILogger logger, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogInformation(this ILogger logger, ref ZLoggerInformationInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Information, default, null, ref message);
ZLog(logger, LogLevel.Information, default, null, ref message.innerHandler);
}

public static void ZLogInformation(this ILogger logger, EventId eventId, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogInformation(this ILogger logger, EventId eventId, ref ZLoggerInformationInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Information, eventId, null, ref message);
ZLog(logger, LogLevel.Information, eventId, null, ref message.innerHandler);
}

public static void ZLogInformation(this ILogger logger, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogInformation(this ILogger logger, Exception? exception, ref ZLoggerInformationInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Information, default, exception, ref message);
ZLog(logger, LogLevel.Information, default, exception, ref message.innerHandler);
}

public static void ZLogInformation(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogInformation(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerInformationInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Information, eventId, exception, ref message);
ZLog(logger, LogLevel.Information, eventId, exception, ref message.innerHandler);
}

public static void ZLogWarning(this ILogger logger, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogWarning(this ILogger logger, ref ZLoggerWarningInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Warning, default, null, ref message);
ZLog(logger, LogLevel.Warning, default, null, ref message.innerHandler);
}

public static void ZLogWarning(this ILogger logger, EventId eventId, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogWarning(this ILogger logger, EventId eventId, ref ZLoggerWarningInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Warning, eventId, null, ref message);
ZLog(logger, LogLevel.Warning, eventId, null, ref message.innerHandler);
}

public static void ZLogWarning(this ILogger logger, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogWarning(this ILogger logger, Exception? exception, ref ZLoggerWarningInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Warning, default, exception, ref message);
ZLog(logger, LogLevel.Warning, default, exception, ref message.innerHandler);
}

public static void ZLogWarning(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogWarning(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerWarningInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Warning, eventId, exception, ref message);
ZLog(logger, LogLevel.Warning, eventId, exception, ref message.innerHandler);
}

public static void ZLogError(this ILogger logger, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogError(this ILogger logger, ref ZLoggerErrorInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Error, default, null, ref message);
ZLog(logger, LogLevel.Error, default, null, ref message.innerHandler);
}

public static void ZLogError(this ILogger logger, EventId eventId, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogError(this ILogger logger, EventId eventId, ref ZLoggerErrorInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Error, eventId, null, ref message);
ZLog(logger, LogLevel.Error, eventId, null, ref message.innerHandler);
}

public static void ZLogError(this ILogger logger, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogError(this ILogger logger, Exception? exception, ref ZLoggerErrorInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Error, default, exception, ref message);
ZLog(logger, LogLevel.Error, default, exception, ref message.innerHandler);
}

public static void ZLogError(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogError(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerErrorInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Error, eventId, exception, ref message);
ZLog(logger, LogLevel.Error, eventId, exception, ref message.innerHandler);
}

public static void ZLogCritical(this ILogger logger, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogCritical(this ILogger logger, ref ZLoggerCriticalInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Critical, default, null, ref message);
ZLog(logger, LogLevel.Critical, default, null, ref message.innerHandler);
}

public static void ZLogCritical(this ILogger logger, EventId eventId, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogCritical(this ILogger logger, EventId eventId, ref ZLoggerCriticalInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Critical, eventId, null, ref message);
ZLog(logger, LogLevel.Critical, eventId, null, ref message.innerHandler);
}

public static void ZLogCritical(this ILogger logger, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogCritical(this ILogger logger, Exception? exception, ref ZLoggerCriticalInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Critical, default, exception, ref message);
ZLog(logger, LogLevel.Critical, default, exception, ref message.innerHandler);
}

public static void ZLogCritical(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLogCritical(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerCriticalInterpolatedStringHandler message)
{
ZLog(logger, LogLevel.Critical, eventId, exception, ref message);
ZLog(logger, LogLevel.Critical, eventId, exception, ref message.innerHandler);
}
}
}
33 changes: 18 additions & 15 deletions src/ZLogger/ZLoggerExtensions.tt
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,56 @@
<#
var logLevels = new[] { "Trace", "Debug", "Information", "Warning", "Error", "Critical" };
#>
using System;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;

namespace ZLogger
{
public static partial class ZLoggerExtensions
{
public static void ZLog(this ILogger logger, LogLevel logLevel, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog(this ILogger logger, LogLevel logLevel, [InterpolatedStringHandlerArgument("logger", "logLevel")]ref ZLoggerInterpolatedStringHandler message)
{
ZLog(logger, logLevel, default, null, ref message);
}

public static void ZLog(this ILogger logger, LogLevel logLevel, EventId eventId, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog(this ILogger logger, LogLevel logLevel, EventId eventId, [InterpolatedStringHandlerArgument("logger", "logLevel")]ref ZLoggerInterpolatedStringHandler message)
{
ZLog(logger, logLevel, eventId, null, ref message);
}

public static void ZLog(this ILogger logger, LogLevel logLevel, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog(this ILogger logger, LogLevel logLevel, Exception? exception, [InterpolatedStringHandlerArgument("logger", "logLevel")]ref ZLoggerInterpolatedStringHandler message)
{
ZLog(logger, logLevel, default, exception, ref message);
}

public static void ZLog(this ILogger logger, LogLevel logLevel, EventId eventId, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog(this ILogger logger, LogLevel logLevel, EventId eventId, Exception? exception, [InterpolatedStringHandlerArgument("logger", "logLevel")]ref ZLoggerInterpolatedStringHandler message)
{
using var state = message.GetState();
logger.Log(logLevel, eventId, state, exception, (state, ex) => state.ToString());
if (message.IsLoggerEnabled)
{
using var state = message.GetStateAndClear();
logger.Log(logLevel, eventId, state, exception, static (state, ex) => state.ToString());
}
}
<# foreach(var logLevel in logLevels) { #>

public static void ZLog<#= logLevel #>(this ILogger logger, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog<#= logLevel #>(this ILogger logger, ref ZLogger<#= logLevel #>InterpolatedStringHandler message)
{
ZLog(logger, LogLevel.<#= logLevel #>, default, null, ref message);
ZLog(logger, LogLevel.<#= logLevel #>, default, null, ref message.innerHandler);
}

public static void ZLog<#= logLevel #>(this ILogger logger, EventId eventId, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog<#= logLevel #>(this ILogger logger, EventId eventId, ref ZLogger<#= logLevel #>InterpolatedStringHandler message)
{
ZLog(logger, LogLevel.<#= logLevel #>, eventId, null, ref message);
ZLog(logger, LogLevel.<#= logLevel #>, eventId, null, ref message.innerHandler);
}

public static void ZLog<#= logLevel #>(this ILogger logger, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog<#= logLevel #>(this ILogger logger, Exception? exception, ref ZLogger<#= logLevel #>InterpolatedStringHandler message)
{
ZLog(logger, LogLevel.<#= logLevel #>, default, exception, ref message);
ZLog(logger, LogLevel.<#= logLevel #>, default, exception, ref message.innerHandler);
}

public static void ZLog<#= logLevel #>(this ILogger logger, EventId eventId, Exception? exception, ref ZLoggerInterpolatedStringHandler message)
public static void ZLog<#= logLevel #>(this ILogger logger, EventId eventId, Exception? exception, ref ZLogger<#= logLevel #>InterpolatedStringHandler message)
{
ZLog(logger, LogLevel.<#= logLevel #>, eventId, exception, ref message);
ZLog(logger, LogLevel.<#= logLevel #>, eventId, exception, ref message.innerHandler);
}
<# } #>
}
Expand Down
Loading

0 comments on commit 90c92a1

Please sign in to comment.