Provides Serilog extensions for RightTurn
This section will demonstrate practical or interesing ways how to use RightTurn.Extension.Serilog along with other RightTurn extensions.
In this example time and log level is moved to the right side of the screen leaving the left side acting like console write line. Using serilog adds colors and structured data.
RightTurn.Extensions.CommandLine
RightTurn.Extensions.Logging
RightTurn.Extensions.Serilog
namespace QuickStart
{
interface IQuickOptions
{
string Name { get; }
}
}
using CommandLine;
namespace QuickStart
{
class QuickOptions : IQuickOptions
{
[Option(HelpText = "Your name.")]
public string Name { get; set; }
}
}
namespace QuickStart
{
internal interface IQuickService
{
void Run();
}
}
using Microsoft.Extensions.Logging;
using System;
namespace QuickStart
{
class QuickService : IQuickService
{
readonly ILogger<QuickService> _logger;
readonly IQuickOptions _options;
public QuickService(ILogger<QuickService> logger, IQuickOptions options)
{
_logger = logger;
_options = options;
}
public void Run()
{
_logger.LogInformation("Service {name} started with {@arguments}", nameof(QuickService), _options);
for (int i = 10; i > 0; i--)
_logger.LogWarning("Counting down {i}", i);
_logger.LogError("Where is {i}?", 0);
_logger.LogInformation("Visiting {uri}", new Uri("https://github.com/Jandini/RightTurn"));
_logger.LogInformation("Service {name} finished", nameof(QuickService));
}
}
}
using RightTurn;
using RightTurn.Extensions.CommandLine;
using RightTurn.Extensions.Logging;
using Serilog.Sinks.SystemConsole.Themes;
using Serilog;
using System;
namespace QuickStart
{
class Program
{
static void Main(string[] args) => new Turn()
.ParseOptions<QuickOptions>(args)
.WithOptionsAsSingleton<IQuickOptions, QuickOptions>()
.WithUnhandledExceptionLogging()
.WithLogging((logging, turn) =>
{
var loggerConfiguration = new LoggerConfiguration();
loggerConfiguration.WriteTo.Console(
theme: AnsiConsoleTheme.Literate,
outputTemplate: $"{{Message,-{Console.WindowWidth - 19}:lj}} {{Timestamp:HH:mm:ss}} [ {{Level:u4}} ]{{NewLine}}{{Exception}}");
logging.AddSerilog(
loggerConfiguration.CreateLogger(),
dispose: true);
})
.Take<IQuickService, QuickService>((quick) => quick.Run());
}
}