Table of Contents

Namespace Yubico.Core.Logging

Classes

Log

The Log class provides centralized logging support for your application or library. It allows you to configure logging either through a JSON configuration file (appsettings.json) or by dynamically setting up a logger using the ConfigureLoggerFactory(Action<ILoggingBuilder>) method.

How to enable Logging:

There are two primary ways to enable logging:

Option 1: Using appsettings.json

Place an appsettings.json file in your project directory with the following structure:

{
  "Logging": {
    "LogLevel": {
      "Yubico.Core": "Warning",
      "Yubico.Yubikey": "Information"
    }
  }
}

Option 2: Using ConfigureLoggerFactory

Configure the logger dynamically in your code:

// Optionally, clear previous loggers
Log.ConfigureLoggerFactory(builder => builder.ClearProviders());

// Add a console logger (added by default) Log.ConfigureLoggerFactory(builder => builder.AddConsole());

// Add a Serilog logger Log.ConfigureLoggerFactory(builder => builder.AddSerilog());

// Add both Console and Serilog loggers Log.ConfigureLoggerFactory(builder => builder.AddConsole().AddSerilog());

Using the Logger

After configuring the logger, you can create log instances and log messages as follows:

namespace Yubico;
public class ExampleClass
{
    public ExampleClass()
    {
        // Logger with the class name as the category
        ILogger typeNamedLogger = Log.GetLogger<ExampleClass>();
        typeNamedLogger.LogInformation("Hello World");
        // Output: Yubico.ExampleClass: Hello World
    // Logger with a custom category name
    ILogger categoryLogger = Log.GetLogger("SmartCard");
    categoryLogger.LogInformation("Hello World");
    // Output: SmartCard: Hello World
}

}

Note:

You can also directly set a custom logger factory using the Instance property, though it is not the recommended approach. Using ConfigureLoggerFactory(Action<ILoggingBuilder>) or the appsettings.json approach is preferred.

LoggerExtensions

Logger extension methods for common scenarios.