Skip to content

Commit

Permalink
Split ILogging interface to improve maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
montyclt committed Jun 1, 2024
1 parent 799f819 commit 0c18bdc
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 310 deletions.
28 changes: 28 additions & 0 deletions src/Foundation/Logging/ILogging.Caller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Diagnostics;
using IOKode.OpinionatedFramework.Facades;
using Microsoft.Extensions.Logging;

namespace IOKode.OpinionatedFramework.Logging;

public partial interface ILogging
{
private ILogger FromCaller()
{
var callerType = new StackFrame(2).GetMethod()!.DeclaringType!;

if (callerType == typeof(Log)) // From facade
{
callerType = new StackFrame(3).GetMethod()!.DeclaringType!;
}

// Handle the case where the caller is an async state machine
if (callerType is { IsNested: true, DeclaringType: not null } && callerType.Name.Contains('<'))
{
callerType = callerType.DeclaringType;
}

var logger = FromCategory(callerType);

return logger;
}
}
51 changes: 51 additions & 0 deletions src/Foundation/Logging/ILogging.Critical.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Microsoft.Extensions.Logging;

namespace IOKode.OpinionatedFramework.Logging;

public partial interface ILogging
{
/// <summary>
/// Formats and writes a critical log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Critical(EventId eventId, Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Critical, eventId, exception, message, args);
}

/// <summary>
/// Formats and writes a critical log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Critical(EventId eventId, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Critical, eventId, message, args);
}

/// <summary>
/// Formats and writes a critical log message.
/// </summary>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Critical(Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Critical, exception, message, args);
}

/// <summary>
/// Formats and writes a critical log message.
/// </summary>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Critical(string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Critical, message, args);
}
}
51 changes: 51 additions & 0 deletions src/Foundation/Logging/ILogging.Debug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Microsoft.Extensions.Logging;

namespace IOKode.OpinionatedFramework.Logging;

public partial interface ILogging
{
/// <summary>
/// Formats and writes a debug log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Debug(EventId eventId, Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Debug, eventId, exception, message, args);
}

/// <summary>
/// Formats and writes a debug log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Debug(EventId eventId, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Debug, eventId, message, args);
}

/// <summary>
/// Formats and writes a debug log message.
/// </summary>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Debug(Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Debug, exception, message, args);
}

/// <summary>
/// Formats and writes a debug log message.
/// </summary>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Debug(string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Debug, message, args);
}
}
51 changes: 51 additions & 0 deletions src/Foundation/Logging/ILogging.Error.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Microsoft.Extensions.Logging;

namespace IOKode.OpinionatedFramework.Logging;

public partial interface ILogging
{
/// <summary>
/// Formats and writes an error log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Error(EventId eventId, Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Error, eventId, exception, message, args);
}

/// <summary>
/// Formats and writes an error log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Error(EventId eventId, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Error, eventId, message, args);
}

/// <summary>
/// Formats and writes an error log message.
/// </summary>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Error(Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Error, exception, message, args);
}

/// <summary>
/// Formats and writes an error log message.
/// </summary>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Error(string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Error, message, args);
}
}
51 changes: 51 additions & 0 deletions src/Foundation/Logging/ILogging.Information.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Microsoft.Extensions.Logging;

namespace IOKode.OpinionatedFramework.Logging;

public partial interface ILogging
{
/// <summary>
/// Formats and writes an informational log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Info(EventId eventId, Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Information, eventId, exception, message, args);
}

/// <summary>
/// Formats and writes an informational log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Info(EventId eventId, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Information, eventId, message, args);
}

/// <summary>
/// Formats and writes an informational log message.
/// </summary>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Info(Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Information, exception, message, args);
}

/// <summary>
/// Formats and writes an informational log message.
/// </summary>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Info(string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Information, message, args);
}
}
51 changes: 51 additions & 0 deletions src/Foundation/Logging/ILogging.Trace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Microsoft.Extensions.Logging;

namespace IOKode.OpinionatedFramework.Logging;

public partial interface ILogging
{
/// <summary>
/// Formats and writes a trace log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Trace(EventId eventId, Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Trace, eventId, exception, message, args);
}

/// <summary>
/// Formats and writes a trace log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Trace(EventId eventId, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Trace, eventId, message, args);
}

/// <summary>
/// Formats and writes a trace log message.
/// </summary>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Trace(Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Trace, exception, message, args);
}

/// <summary>
/// Formats and writes a trace log message.
/// </summary>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Trace(string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Trace, message, args);
}
}
51 changes: 51 additions & 0 deletions src/Foundation/Logging/ILogging.Warning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using Microsoft.Extensions.Logging;

namespace IOKode.OpinionatedFramework.Logging;

public partial interface ILogging
{
/// <summary>
/// Formats and writes a warning log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Warn(EventId eventId, Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Warning, eventId, exception, message, args);
}

/// <summary>
/// Formats and writes a warning log message.
/// </summary>
/// <param name="eventId">The event id associated with the log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Warn(EventId eventId, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Warning, eventId, message, args);
}

/// <summary>
/// Formats and writes a warning log message.
/// </summary>
/// <param name="exception">The exception to log.</param>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Warn(Exception? exception, string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Warning, exception, message, args);
}

/// <summary>
/// Formats and writes a warning log message.
/// </summary>
/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
public void Warn(string? message, params object?[] args)
{
FromCaller().Log(LogLevel.Warning, message, args);
}
}
Loading

0 comments on commit 0c18bdc

Please sign in to comment.