Skip to content

fix: The default logger only writes output for the Error log level unless other is specified #185

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

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 0 additions & 129 deletions Yubico.Core/src/Yubico/Core/Logging/Log.Legacy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,109 +18,10 @@

namespace Yubico.Core.Logging
{
/// <summary>
/// A static class for managing Yubico SDK logging for this process.
/// </summary>
/// <remarks>
/// <para>
/// This class is used for managing the active logger used globally by .NET-based Yubico SDKs in the current process.
/// Changing the settings in this class will not have any effect on applications or services that are not running
/// within the current application's process. It will affect all libraries contained within - for example, changing
/// the logger factory here will impact both the Yubico.YubiKey and Yubico.Core libraries.
/// </para>
/// <para>
/// The <see cref="LoggerFactory"/> property is used to set and control the concrete log to be used by the SDK. By
/// default, we send logs to the "null" logger - effectively disabling logging. If you set this property with your
/// own logger factory, the SDK will use this log from the point of the set until someone calls this set method again.
/// </para>
/// <para>
/// <see cref="GetLogger()"/> should be used to return an instance of the <see cref="Logger"/> class. This is the object
/// used to actually write the log messages. It is generally OK to cache an instance of a logger within another
/// class instance. Holding a Logger instance open longer than that is not recommended, as changes to the LoggerFactory
/// will not be reflected until you call the `GetLogger` method again.
/// </para>
/// </remarks>
public static partial class Log
{
private static ILoggerFactory? _factory;

/// <summary>
/// The logger factory implementation that should be used by the SDK. Use this to set the active logger.
/// </summary>
/// <remarks>
/// <para>
/// The LoggerFactory controls how the concrete log(s) that the SDK will use get created. This is something that
/// should be controlled by the application using the SDK, and not the SDK itself. The application can decide
/// whether they would like to send events to the Windows Event Log, or to a cross platform logger such as NLog,
/// Serilog, or others. An application can decide to send log messages to multiple sinks as well (see examples).
/// </para>
/// <para>
/// The <see cref="ILoggerFactory"/> interface is the same one that is used by `Microsoft.Extensions.Logging.` You
/// can read more about how to integrate with this interface in the
/// [Logging in .NET](https://docs.microsoft.com/en-us/dotnet/core/extensions/logging) webpage provided by Microsoft.
/// </para>
/// </remarks>
/// <example>
/// <para>
/// Send SDK log messages to the console:
/// </para>
/// <code language="csharp">
/// using Microsoft.Extensions.Logging;
/// using Yubico.Core.Logging;
///
/// static class Program
/// {
/// static void EnableLogging()
/// {
/// Log.LoggerFactory = LoggerFactory.Create(
/// builder => builder.AddSimpleConsole(
/// options =>
/// {
/// options.IncludeScopes = true;
/// options.SingleLine = true;
/// options.TimestampFormat = "hh:mm:ss";
/// })
/// .AddFilter(level => level >= LogLevel.Information));
/// }
/// }
/// </code>
/// </example>
/// <example>
/// <para>
/// Send SDK log messages to Serilog.
/// </para>
/// <para>
/// First, begin by adding a package reference to `Serilog.Extensions.Logging` and `Serilog.Sinks.Console` (or
/// to the appropriate sink you plan to use).
/// </para>
/// <para>
/// Now, you can add the following code to your application:
/// </para>
/// <code language="csharp">
/// using Microsoft.Extensions.Logging;
/// using Serilog;
/// using Yubico.Core.Logging;
///
/// static class Program
/// {
/// static void EnableLogging()
/// {
/// // Serilog does setup through its own LoggerConfiguration builder. The factory will
/// // pick up the log from Serilog.Log.Logger.
/// Serilog.Log.Logger = new LoggerConfiguration()
/// .Enrich().FromLogContext()
/// .WriteTo.Console()
/// .CreateLogger();
///
/// // Fully qualified name to avoid conflicts with Serilog types
/// Yubico.Core.Logging.Log.LoggerFactory = LoggerFactory.Create(
/// builder => builder
/// .AddSerilog(dispose: true)
/// .AddFilter(level => level >= LogLevel.Information));
/// }
/// }
/// </code>
/// </example>
[Obsolete("Obsolete, use Log.Instance instead. Setting this will override the default dotnet console logger.")]
public static ILoggerFactory LoggerFactory
{
Expand All @@ -134,36 +35,6 @@ public static ILoggerFactory LoggerFactory
}
}

/// <summary>
/// Gets an instance of the active logger.
/// </summary>
/// <returns>
/// An instance of the active concrete logger.
/// </returns>
/// <example>
/// <para>
/// Write some information to the log.
/// </para>
/// <code language="csharp">
/// using Yubico.Core.Logging;
///
/// public class Example
/// {
/// private Logger _log = Log.GetLogger();
///
/// public void SampleMethod()
/// {
/// _log.LogDebug("The SampleMethod method has been called!");
/// }
///
/// public void StaticMethod()
/// {
/// Logger log = Log.GetLogger(); // Can't use the instance logger because we're static.
/// log.LogDebug("Called from a static method!");
/// }
/// }
/// </code>
/// </example>
[Obsolete("Obsolete, use equivalent ILogger method, or view the changelog for further instruction.")]
public static Logger GetLogger() => new Logger(Yubico.Core.Logging.Log.LoggerFactory.CreateLogger("Yubico.Core logger"));
}
Expand Down
37 changes: 17 additions & 20 deletions Yubico.Core/src/Yubico/Core/Logging/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,34 +158,31 @@ public static void ConfigureLoggerFactory(Action<ILoggingBuilder> configure) =>
//Creates a logging factory based on a JsonConfiguration in appsettings.json
private static ILoggerFactory GetDefaultLoggerFactory()
{
ILoggerFactory? configuredLoggingFactory = null;
try
string settingsPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
if (!File.Exists(settingsPath))
{
IConfigurationRoot configuration = new ConfigurationBuilder()
return ErrorLoggerFactory;
}

IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile("appsettings.json", optional: false)
.AddJsonFile("appsettings.Development.json", optional: true)
.Build();

configuredLoggingFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(
builder =>
{
IConfigurationSection loggingSection = configuration.GetSection("Logging");
_ = builder.AddConfiguration(loggingSection);
_ = builder.AddConsole();
});
}
#pragma warning disable CA1031
catch (Exception e)
#pragma warning restore CA1031
{
Console.Error.WriteLine(e);
}
return Microsoft.Extensions.Logging.LoggerFactory.Create(
builder =>
{
IConfigurationSection loggingSection = configuration.GetSection("Logging");
_ = builder
.AddConfiguration(loggingSection)
.AddConsole();
});
}

return configuredLoggingFactory ?? Microsoft.Extensions.Logging.LoggerFactory.Create(
private static ILoggerFactory ErrorLoggerFactory => Microsoft.Extensions.Logging.LoggerFactory.Create(
builder => builder
.AddConsole()
.SetMinimumLevel(LogLevel.Error));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ limitations under the License. -->
<UseWindowsForms Condition=" '$(OS)' == 'Windows_NT'">true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<ProjectReference Include="..\SharedSampleCode\SharedSampleCode.csproj" />

</ItemGroup>


</Project>
8 changes: 8 additions & 0 deletions Yubico.YubiKey/examples/Fido2SampleCode/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"AppName": "FidoSampleCode",
"Logging": {
"LogLevel": {
"Yubico": "Error"
}
}
}
3 changes: 3 additions & 0 deletions Yubico.YubiKey/examples/OathSampleCode/OathSampleCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ limitations under the License. -->
</PropertyGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<ProjectReference Include="..\SharedSampleCode\SharedSampleCode.csproj" />
</ItemGroup>

Expand Down
8 changes: 8 additions & 0 deletions Yubico.YubiKey/examples/OathSampleCode/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"AppName": "OathSampleCode",
"Logging": {
"LogLevel": {
"Yubico": "Error"
}
}
}
3 changes: 3 additions & 0 deletions Yubico.YubiKey/examples/PivSampleCode/PivSampleCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ limitations under the License. -->
</PropertyGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<ProjectReference Include="..\SharedSampleCode\SharedSampleCode.csproj" />
</ItemGroup>

Expand Down
8 changes: 8 additions & 0 deletions Yubico.YubiKey/examples/PivSampleCode/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"AppName": "PivSampleCode",
"Logging": {
"LogLevel": {
"Yubico": "Error"
}
}
}
3 changes: 3 additions & 0 deletions Yubico.YubiKey/examples/U2fSampleCode/U2fSampleCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ limitations under the License. -->
</PropertyGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<ProjectReference Include="..\SharedSampleCode\SharedSampleCode.csproj" />
</ItemGroup>

Expand Down
8 changes: 8 additions & 0 deletions Yubico.YubiKey/examples/U2fSampleCode/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"AppName": "U2FSampleCode",
"Logging": {
"LogLevel": {
"Yubico": "Error"
}
}
}
2 changes: 1 addition & 1 deletion Yubico.YubiKey/tests/integration/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Yubico": "Debug"
},
"Console": {
"IncludeScopes": true,
"IncludeScopes": true
}
}
}
2 changes: 1 addition & 1 deletion Yubico.YubiKey/tests/unit/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"AppName": "UnitTests",
"Logging": {
"LogLevel": {
"Yubico": "Error"
"Yubico": "None"
},
"Console": {
"IncludeScopes": true
Expand Down
Loading