Skip to content

Commit 443e7c7

Browse files
authored
Merge pull request #185
fix: The default logger only writes output for the Error log level unless other is specified
2 parents b63881c + d7ea2f1 commit 443e7c7

File tree

12 files changed

+65
-151
lines changed

12 files changed

+65
-151
lines changed

Yubico.Core/src/Yubico/Core/Logging/Log.Legacy.cs

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -18,109 +18,10 @@
1818

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

47-
/// <summary>
48-
/// The logger factory implementation that should be used by the SDK. Use this to set the active logger.
49-
/// </summary>
50-
/// <remarks>
51-
/// <para>
52-
/// The LoggerFactory controls how the concrete log(s) that the SDK will use get created. This is something that
53-
/// should be controlled by the application using the SDK, and not the SDK itself. The application can decide
54-
/// whether they would like to send events to the Windows Event Log, or to a cross platform logger such as NLog,
55-
/// Serilog, or others. An application can decide to send log messages to multiple sinks as well (see examples).
56-
/// </para>
57-
/// <para>
58-
/// The <see cref="ILoggerFactory"/> interface is the same one that is used by `Microsoft.Extensions.Logging.` You
59-
/// can read more about how to integrate with this interface in the
60-
/// [Logging in .NET](https://docs.microsoft.com/en-us/dotnet/core/extensions/logging) webpage provided by Microsoft.
61-
/// </para>
62-
/// </remarks>
63-
/// <example>
64-
/// <para>
65-
/// Send SDK log messages to the console:
66-
/// </para>
67-
/// <code language="csharp">
68-
/// using Microsoft.Extensions.Logging;
69-
/// using Yubico.Core.Logging;
70-
///
71-
/// static class Program
72-
/// {
73-
/// static void EnableLogging()
74-
/// {
75-
/// Log.LoggerFactory = LoggerFactory.Create(
76-
/// builder => builder.AddSimpleConsole(
77-
/// options =>
78-
/// {
79-
/// options.IncludeScopes = true;
80-
/// options.SingleLine = true;
81-
/// options.TimestampFormat = "hh:mm:ss";
82-
/// })
83-
/// .AddFilter(level => level >= LogLevel.Information));
84-
/// }
85-
/// }
86-
/// </code>
87-
/// </example>
88-
/// <example>
89-
/// <para>
90-
/// Send SDK log messages to Serilog.
91-
/// </para>
92-
/// <para>
93-
/// First, begin by adding a package reference to `Serilog.Extensions.Logging` and `Serilog.Sinks.Console` (or
94-
/// to the appropriate sink you plan to use).
95-
/// </para>
96-
/// <para>
97-
/// Now, you can add the following code to your application:
98-
/// </para>
99-
/// <code language="csharp">
100-
/// using Microsoft.Extensions.Logging;
101-
/// using Serilog;
102-
/// using Yubico.Core.Logging;
103-
///
104-
/// static class Program
105-
/// {
106-
/// static void EnableLogging()
107-
/// {
108-
/// // Serilog does setup through its own LoggerConfiguration builder. The factory will
109-
/// // pick up the log from Serilog.Log.Logger.
110-
/// Serilog.Log.Logger = new LoggerConfiguration()
111-
/// .Enrich().FromLogContext()
112-
/// .WriteTo.Console()
113-
/// .CreateLogger();
114-
///
115-
/// // Fully qualified name to avoid conflicts with Serilog types
116-
/// Yubico.Core.Logging.Log.LoggerFactory = LoggerFactory.Create(
117-
/// builder => builder
118-
/// .AddSerilog(dispose: true)
119-
/// .AddFilter(level => level >= LogLevel.Information));
120-
/// }
121-
/// }
122-
/// </code>
123-
/// </example>
12425
[Obsolete("Obsolete, use Log.Instance instead. Setting this will override the default dotnet console logger.")]
12526
public static ILoggerFactory LoggerFactory
12627
{
@@ -134,36 +35,6 @@ public static ILoggerFactory LoggerFactory
13435
}
13536
}
13637

137-
/// <summary>
138-
/// Gets an instance of the active logger.
139-
/// </summary>
140-
/// <returns>
141-
/// An instance of the active concrete logger.
142-
/// </returns>
143-
/// <example>
144-
/// <para>
145-
/// Write some information to the log.
146-
/// </para>
147-
/// <code language="csharp">
148-
/// using Yubico.Core.Logging;
149-
///
150-
/// public class Example
151-
/// {
152-
/// private Logger _log = Log.GetLogger();
153-
///
154-
/// public void SampleMethod()
155-
/// {
156-
/// _log.LogDebug("The SampleMethod method has been called!");
157-
/// }
158-
///
159-
/// public void StaticMethod()
160-
/// {
161-
/// Logger log = Log.GetLogger(); // Can't use the instance logger because we're static.
162-
/// log.LogDebug("Called from a static method!");
163-
/// }
164-
/// }
165-
/// </code>
166-
/// </example>
16738
[Obsolete("Obsolete, use equivalent ILogger method, or view the changelog for further instruction.")]
16839
public static Logger GetLogger() => new Logger(Yubico.Core.Logging.Log.LoggerFactory.CreateLogger("Yubico.Core logger"));
16940
}

Yubico.Core/src/Yubico/Core/Logging/Log.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -158,34 +158,31 @@ public static void ConfigureLoggerFactory(Action<ILoggingBuilder> configure) =>
158158
//Creates a logging factory based on a JsonConfiguration in appsettings.json
159159
private static ILoggerFactory GetDefaultLoggerFactory()
160160
{
161-
ILoggerFactory? configuredLoggingFactory = null;
162-
try
161+
string settingsPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
162+
if (!File.Exists(settingsPath))
163163
{
164-
IConfigurationRoot configuration = new ConfigurationBuilder()
164+
return ErrorLoggerFactory;
165+
}
166+
167+
IConfigurationRoot configuration = new ConfigurationBuilder()
165168
.SetBasePath(Directory.GetCurrentDirectory())
166-
.AddJsonFile("appsettings.json", optional: true)
169+
.AddJsonFile("appsettings.json", optional: false)
167170
.AddJsonFile("appsettings.Development.json", optional: true)
168171
.Build();
169172

170-
configuredLoggingFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(
171-
builder =>
172-
{
173-
IConfigurationSection loggingSection = configuration.GetSection("Logging");
174-
_ = builder.AddConfiguration(loggingSection);
175-
_ = builder.AddConsole();
176-
});
177-
}
178-
#pragma warning disable CA1031
179-
catch (Exception e)
180-
#pragma warning restore CA1031
181-
{
182-
Console.Error.WriteLine(e);
183-
}
173+
return Microsoft.Extensions.Logging.LoggerFactory.Create(
174+
builder =>
175+
{
176+
IConfigurationSection loggingSection = configuration.GetSection("Logging");
177+
_ = builder
178+
.AddConfiguration(loggingSection)
179+
.AddConsole();
180+
});
181+
}
184182

185-
return configuredLoggingFactory ?? Microsoft.Extensions.Logging.LoggerFactory.Create(
183+
private static ILoggerFactory ErrorLoggerFactory => Microsoft.Extensions.Logging.LoggerFactory.Create(
186184
builder => builder
187185
.AddConsole()
188186
.SetMinimumLevel(LogLevel.Error));
189-
}
190187
}
191188
}

Yubico.YubiKey/examples/Fido2SampleCode/Fido2SampleCode.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ limitations under the License. -->
2424
<UseWindowsForms Condition=" '$(OS)' == 'Windows_NT'">true</UseWindowsForms>
2525
</PropertyGroup>
2626
<ItemGroup>
27+
<None Update="appsettings.json">
28+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29+
</None>
2730
<ProjectReference Include="..\SharedSampleCode\SharedSampleCode.csproj" />
31+
2832
</ItemGroup>
2933

34+
3035
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"AppName": "FidoSampleCode",
3+
"Logging": {
4+
"LogLevel": {
5+
"Yubico": "Error"
6+
}
7+
}
8+
}

Yubico.YubiKey/examples/OathSampleCode/OathSampleCode.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ limitations under the License. -->
2222
</PropertyGroup>
2323

2424
<ItemGroup>
25+
<None Update="appsettings.json">
26+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
27+
</None>
2528
<ProjectReference Include="..\SharedSampleCode\SharedSampleCode.csproj" />
2629
</ItemGroup>
2730

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"AppName": "OathSampleCode",
3+
"Logging": {
4+
"LogLevel": {
5+
"Yubico": "Error"
6+
}
7+
}
8+
}

Yubico.YubiKey/examples/PivSampleCode/PivSampleCode.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ limitations under the License. -->
2222
</PropertyGroup>
2323

2424
<ItemGroup>
25+
<None Update="appsettings.json">
26+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
27+
</None>
2528
<ProjectReference Include="..\SharedSampleCode\SharedSampleCode.csproj" />
2629
</ItemGroup>
2730

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"AppName": "PivSampleCode",
3+
"Logging": {
4+
"LogLevel": {
5+
"Yubico": "Error"
6+
}
7+
}
8+
}

Yubico.YubiKey/examples/U2fSampleCode/U2fSampleCode.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ limitations under the License. -->
2222
</PropertyGroup>
2323

2424
<ItemGroup>
25+
<None Update="appsettings.json">
26+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
27+
</None>
2528
<ProjectReference Include="..\SharedSampleCode\SharedSampleCode.csproj" />
2629
</ItemGroup>
2730

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"AppName": "U2FSampleCode",
3+
"Logging": {
4+
"LogLevel": {
5+
"Yubico": "Error"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)