# Logging

> Connecting your own logging tool (Serilog, log4net, Seq) to the eKasa .NET library through Microsoft.Extensions.Logging.Abstractions.

Source: https://ekasa.ninedigit.sk/en/docs/dotnet/logging

The eKasa library connects to logging through the
[*Microsoft.Extensions.Logging.Abstractions*](https://www.nuget.org/packages/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](https://github.com/serilog/serilog-extensions-logging) |
| log4net | [Microsoft.Extensions.Logging.Log4Net.AspNetCore](https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore) |
| Seq | [Seq.Extensions.Logging](https://www.nuget.org/packages/Seq.Extensions.Logging/) |

## Example with Serilog

1. **Install the NuGet packages**
   `Serilog.Extensions.Logging` and `Serilog.Sinks.Console`.
2. **Create a Serilog logger**
   Set the level of detail of logged messages and the console output.
3. **Wrap it in an `ILoggerFactory`**
   With the `SerilogLoggerFactory` class.
4. **Pass it to the client**
   To the `Client(configuration, loggerFactory)` constructor, see [Library instantiation](https://ekasa.ninedigit.sk/en/docs/dotnet/instantiation).

```csharp
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](https://github.com/serilog/serilog/wiki/Configuration-Basics).

## Next steps

- [Library instantiation](https://ekasa.ninedigit.sk/en/docs/dotnet/instantiation): Constructors of the Client class and a single instance for the application lifetime.
- [Configuration](https://ekasa.ninedigit.sk/en/docs/dotnet/configuration): Loading the settings of the Portos eKasa Servis application.
