mfgames-cil/src/MfGames.Serilog.SpectreExpressions/ExpressionSpectreConsoleSinkExtensions.cs

51 lines
1.8 KiB
C#

using Serilog;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Templates;
using Serilog.Templates.Themes;
namespace MfGames.Serilog.SpectreExpressions;
public static class ExpressionSpectreConsoleSinkExtensions
{
private const string DefaultConsoleOutputTemplate = "[{@t:HH:mm:ss} {@l:u3}] {@m}\n{@x}";
/// <summary>
/// Write log events to the console using Spectre.Console.
/// </summary>
/// <param name="loggerConfiguration">Logger sink configuration.</param>
/// <param name="outputTemplate">
/// A message template describing the format used to write to the sink.
/// The default is "[{Timestamp:HH:mm:ss} {Level:u3}]
/// {Message:lj}{NewLine}{Exception}".
/// </param>
/// <param name="restrictedToMinimumLevel">
/// The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch" /> is
/// specified.
/// </param>
/// <param name="levelSwitch">
/// A switch allowing the pass-through minimum level
/// to be changed at runtime.
/// </param>
/// <param name="theme">The Serilog theme for the output.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration SpectreExpression(
this LoggerSinkConfiguration loggerConfiguration,
ExpressionTemplate? outputTemplate = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
LoggingLevelSwitch? levelSwitch = null,
TemplateTheme? theme = null
)
{
outputTemplate ??= new ExpressionTemplate(DefaultConsoleOutputTemplate, theme: theme);
return loggerConfiguration.Sink(
new ExpressionSpectreConsoleSink(outputTemplate),
restrictedToMinimumLevel,
levelSwitch
);
}
}