C
C#•10mo ago
ewilliams

Removing Logging Abstractions from Libraries

I'm creating a library that uses a builder patter. I don't want the library to process exceptions, create logs, etc. I want the consumer to be able to decide how to handle this aspect. Normally I would just stick some Serilog inside but I don't to force the dependancy onto others. My first thought is to provide a Func<Exception> as an option in the builders pipeline. Currently this works fine and could certainly create extension motioned that plug serilog into this. The default output dumps the console if the WithLogging function is not provided. Where I don't love this, closures. I feel like generally consumers will create a closure with choose. I'm targeting dotnet6 for this project (for a few reasons).
csharp
using var orderlyScanner = ScannerBuilder
.Configure()
.WithAddresses(ips)
.WithNodeTimeout(TimeSpan.FromMilliseconds(250))
.WithLogging(
exception =>
{
Console.WriteLine($"ORDERED ERROR: {exception.Message}");
},
message =>
{
Console.WriteLine($"ORDERED MESSAGE: {message}");
})
.Build();
csharp
using var orderlyScanner = ScannerBuilder
.Configure()
.WithAddresses(ips)
.WithNodeTimeout(TimeSpan.FromMilliseconds(250))
.WithLogging(
exception =>
{
Console.WriteLine($"ORDERED ERROR: {exception.Message}");
},
message =>
{
Console.WriteLine($"ORDERED MESSAGE: {message}");
})
.Build();
6 Replies
Patrick
Patrick•10mo ago
this is why ILogger exists 🙂
ewilliams
ewilliamsOP•10mo ago
Damn now I feel dumb:) I thought that interface was part of asp, does it go back to standard 2.0?
Patrick
Patrick•10mo ago
Microsoft.Extensions.Logging.Abstractions 8.0.0
Logging abstractions for Microsoft.Extensions.Logging. Commonly Used Types: Microsoft.Extensions.Logging.ILogger Microsoft.Extensions.Logging.ILoggerFactory Microsoft.Extensions.Logging.ILogger Microsoft.Extensions.Logging.LogLevel Microsoft.Extensions.Logging.Logger Microsoft.Extensions.Logging.LoggerMessage Microsoft.Extensions.Logging.Abstra...
ewilliams
ewilliamsOP•10mo ago
nice, thanks!
ewilliams
ewilliamsOP•10mo ago
i think this cover the cases..
No description
No description
ewilliams
ewilliamsOP•10mo ago
No description
Want results from more Discord servers?
Add your server