The eKasa library connects to logging through the
Microsoft.Extensions.Logging.Abstractions
abstraction. The Client class accepts an ILoggerFactory instance, so the library does not dictate a logging tool:
you use the one your application already has.
Supported tools
You can use any logging tool that provides an ILoggerFactory, for example:
| Tool | Connection to ILoggerFactory |
|---|---|
| Serilog | serilog-extensions-logging |
| log4net | Microsoft.Extensions.Logging.Log4Net.AspNetCore |
| Seq | Seq.Extensions.Logging |
Example with Serilog
- Install the NuGet packages
Serilog.Extensions.LoggingandSerilog.Sinks.Console. - Create a Serilog logger Set the level of detail of logged messages and the console output.
- Wrap it in an
ILoggerFactoryWith theSerilogLoggerFactoryclass. - Pass it to the client
To the
Client(configuration, loggerFactory)constructor, see Library instantiation.
using Microsoft.Extensions.Logging;
using NineDigit.eKasa;
using Serilog;
using Serilog.Extensions.Logging;
namespace SerilogExampleApp
{
class SerilogLoggerExample
{
public void Demo(ClientConfiguration configuration)
{
// The Client class accepts an ILoggerFactory instance.
// Thanks to this abstraction, you can use your favourite logging tool.
// This example uses Serilog with the NuGet packages:
// - Serilog.Extensions.Logging
// - Serilog.Sinks.Console
Serilog.ILogger serilogLogger = new LoggerConfiguration()
.MinimumLevel.Debug() // level of detail of logged messages
.WriteTo.Console()
.CreateLogger();
// create a universal Microsoft.Extensions.Logging.ILoggerFactory instance
ILoggerFactory loggerFactory = new SerilogLoggerFactory(serilogLogger);
// test the logger
loggerFactory.CreateLogger(nameof(SerilogLoggerExample)).LogDebug("Hello from SerilogExampleApp!");
// create the entry point of the whole solution, the Client instance
Client client = new NineDigit.eKasa.Client(configuration, loggerFactory);
}
}
}
More Serilog configuration options are in the Serilog documentation.