85 lines
2.9 KiB
C#
85 lines
2.9 KiB
C#
using System.CommandLine;
|
|
using System.CommandLine.Invocation;
|
|
using System.Threading.Tasks;
|
|
using MfGames.ToolBuilder;
|
|
using Microsoft.Extensions.Logging;
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
namespace SampleTool.Commands;
|
|
|
|
public class LogCommand : Command, ICommandHandler, ITopCommand
|
|
{
|
|
private readonly ILogger<LogCommand> extensionLogger;
|
|
|
|
private readonly ILogger serilogContextLogger;
|
|
|
|
private readonly ILogger serilogLogger;
|
|
|
|
/// <inheritdoc />
|
|
public LogCommand(ILoggerFactory loggerFactory, ILogger serilogLogger)
|
|
: base("log", "Shows various logging messages using Serilog and Microsoft")
|
|
{
|
|
this.serilogLogger = serilogLogger;
|
|
this.serilogContextLogger = serilogLogger.ForContext<LogCommand>();
|
|
this.extensionLogger = loggerFactory.CreateLogger<LogCommand>();
|
|
this.Handler = this;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public int Invoke(InvocationContext context)
|
|
{
|
|
return this.InvokeAsync(context).Result;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<int> InvokeAsync(InvocationContext context)
|
|
{
|
|
// Show the serilog logging.
|
|
this.serilogLogger.Verbose("Serilog Verbose()");
|
|
this.serilogLogger.Debug("Serilog Debug()");
|
|
this.serilogLogger.Information("Serilog Information()");
|
|
this.serilogLogger.Warning("Serilog Warning()");
|
|
this.serilogLogger.Error("Serilog Error()");
|
|
this.serilogLogger.Fatal("Serilog Fatal()");
|
|
|
|
// Show serilog with context.
|
|
this.serilogContextLogger.Information("Serilog Information() with context");
|
|
|
|
// Show the extension logging.
|
|
this.extensionLogger.LogTrace("System.Extension.Logging LogTrace");
|
|
this.extensionLogger.LogDebug("System.Extension.Logging LogDebug");
|
|
this.extensionLogger.LogInformation("System.Extension.Logging LogInformation");
|
|
this.extensionLogger.LogWarning("System.Extension.Logging LogWarning");
|
|
this.extensionLogger.LogError("System.Extension.Logging LogError");
|
|
this.extensionLogger.LogCritical("System.Extension.Logging LogCritical");
|
|
|
|
// Show Serilog working through logging extensions.
|
|
var hash = new
|
|
{
|
|
Number = 1,
|
|
String = "String",
|
|
Inner = new { Nested = true }
|
|
};
|
|
|
|
this.serilogLogger.Information(
|
|
"Serilog Contextual parameters {Name} and {Quotes:l}",
|
|
"extension logger",
|
|
"without quotes"
|
|
);
|
|
this.serilogLogger.Information("Serilog Contextual nested object {@Nested}", hash);
|
|
|
|
this.extensionLogger.LogInformation(
|
|
"System.Extension.Logging parameters {Name} and {Quotes:l}",
|
|
"extension logger",
|
|
"without quotes"
|
|
);
|
|
this.extensionLogger.LogInformation(
|
|
"System.Extension.Logging nested object {@Nested}",
|
|
hash
|
|
);
|
|
|
|
// We're good.
|
|
return Task.FromResult(0);
|
|
}
|
|
}
|