Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now using Microsoft.Extensions.Logging.Console as the default logger #139

Merged
merged 33 commits into from
Aug 13, 2024

Conversation

DennisDyallo
Copy link
Collaborator

@DennisDyallo DennisDyallo commented Aug 8, 2024

Summary

This change introduces ´Microsoft.Extensions.Logging.Console´ as the new default logger. The reason for this change is to allow for easier logging output when using the SDK. For example. the integration tests and all other users will now have logging on by default. Previously, you had to set up logging for each specific test or application, whereas now, in the case of printing to the console, this is handled by the Log.cs class, as well as some configuration in the new appsettings.json.

To enable logging from a dependent project (e.g. unit tests, integration tests, your cool app), you have two options.

  1. Add an appsettings.json to your project
  2. Use the ConfigureLoggerFactory(Action configure)[^configureLoggerFactory]

Add an appsettings.json to your project1

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

Use the ConfigureLoggerFactory

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

// Add console logger (added by default)
Log.ConfigureLoggingFactory(builder => builder.AddConsole())

// Add Serilog logger
Log.ConfigureLoggingFactory(builder => builder.AddSerilog())

// Why not both?
Log.ConfigureLoggingFactory(builder => builder.AddConsole().AddSerilog())

// Use logger 
namespace Yubico;  
public class ExampleClass
{
  public ExampleClass()
  {
    ILogger typeNamedLogger = Loggers.CreateLogger<ExampleClass>()
    typeNamedLogger.LogInformation("Hello World")
    // Yubico.ExampleClass: Hello World

    ILogger categoryLogger = Loggers.CreateLogger("SmartCard")
    categoryLogger.LogInformation("Hello World")
    // SmartCard: Hello World
  }
}

Fixes: https://yubico.atlassian.net/browse/YESDK-1361

Type of change

  • Refactor (non-breaking change which improves code quality or performance)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Test configuration:

  • Operating system:
  • Firmware version:
  • Yubikey model2:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have run dotnet format to format my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Footnotes

  1. Make sure your appsettings.json is copied to build output

  2. See Yubikey models (Multi-protocol, Security Key, FIPS, Bio, YubiHSM)

Copy link

github-actions bot commented Aug 8, 2024

Test Results: Windows

    2 files      2 suites   4s ⏱️
3 703 tests 3 703 ✅ 0 💤 0 ❌
3 705 runs  3 705 ✅ 0 💤 0 ❌

Results for commit d40d837.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Aug 8, 2024

Test Results: Ubuntu

    2 files      2 suites   9s ⏱️
3 695 tests 3 695 ✅ 0 💤 0 ❌
3 697 runs  3 697 ✅ 0 💤 0 ❌

Results for commit d40d837.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Aug 8, 2024

Test Results: MacOS

    2 files      2 suites   4s ⏱️
3 695 tests 3 695 ✅ 0 💤 0 ❌
3 697 runs  3 697 ✅ 0 💤 0 ❌

Results for commit d40d837.

♻️ This comment has been updated with latest results.

@DennisDyallo DennisDyallo changed the title Now using Microsoft.Extensions.Logging.Console Now using Microsoft.Extensions.Logging.Console as the default logger Aug 12, 2024
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Copy link
Collaborator Author

@DennisDyallo DennisDyallo Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is the meat of the PR

Copy link
Collaborator Author

@DennisDyallo DennisDyallo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

@DennisDyallo DennisDyallo marked this pull request as ready for review August 12, 2024 15:29
Copy link

Code Coverage

Package Line Rate Branch Rate Complexity Health
Yubico.Core 42% 32% 4179
Yubico.YubiKey 50% 47% 19110
Summary 49% (32081 / 65961) 44% (8197 / 18549) 23289

Minimum allowed line rate is 40%

@DennisDyallo DennisDyallo merged commit 299b6ae into develop Aug 13, 2024
11 checks passed
@DennisDyallo DennisDyallo deleted the dennisdyallo/logging branch August 13, 2024 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant