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
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4ac91ef
Rename
DennisDyallo Aug 8, 2024
ed777d0
Add Microsoft Logger
DennisDyallo Aug 8, 2024
576661a
Add Msft logger package, using appsettings.json to configure logger i…
DennisDyallo Aug 8, 2024
0264b72
Use new logger in YubiKeyDeviceListener
DennisDyallo Aug 8, 2024
181d7a9
Rename
DennisDyallo Aug 8, 2024
c8f26a2
Remove type param, add doc
DennisDyallo Aug 8, 2024
fafc882
includeScopes
DennisDyallo Aug 8, 2024
9f05578
Replace old logger in Core
DennisDyallo Aug 8, 2024
8c43eb4
Replace old logger in Yubikey
DennisDyallo Aug 8, 2024
3bbd3aa
Replace old logger in Yubikey Core devices
DennisDyallo Aug 8, 2024
d3aaa0e
Replace in test projects
DennisDyallo Aug 8, 2024
65125f0
Boyscout: set correct test category
DennisDyallo Aug 8, 2024
e806738
Add todo
DennisDyallo Aug 8, 2024
a75c68e
Added dynamic configuration of loggerfactory
DennisDyallo Aug 8, 2024
f12b2b1
Rename
DennisDyallo Aug 12, 2024
1aef6bb
Added legacy methods for logging, added documentation
DennisDyallo Aug 12, 2024
5d69fec
Add appsettings for tests
DennisDyallo Aug 12, 2024
e26bb67
Added docs to clear providers
DennisDyallo Aug 12, 2024
7a4a3e9
Clear todos
DennisDyallo Aug 12, 2024
7c6e033
Add clarification
DennisDyallo Aug 12, 2024
e66e238
Remove unused Serilog
DennisDyallo Aug 12, 2024
53e1df6
Fix typo
DennisDyallo Aug 12, 2024
72f4eaf
Merge branch 'develop' into dennisdyallo/logging
DennisDyallo Aug 12, 2024
8f056a6
Add tests
DennisDyallo Aug 12, 2024
c0dbd99
Fix tests that would fail if run together
DennisDyallo Aug 13, 2024
4284c4a
Add thread safe initialization
DennisDyallo Aug 13, 2024
debd0b4
Trim whitespace
DennisDyallo Aug 13, 2024
6187109
Dotnet format
DennisDyallo Aug 13, 2024
e116b2e
Revert change
DennisDyallo Aug 13, 2024
8bb3072
Revert name change of logger instance
DennisDyallo Aug 13, 2024
2dbc4a3
Revert name change of logger instance
DennisDyallo Aug 13, 2024
4d6580a
Revert tostring change
DennisDyallo Aug 13, 2024
d40d837
Revert using removal
DennisDyallo Aug 13, 2024
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
5 changes: 4 additions & 1 deletion Yubico.Core/src/Yubico.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ limitations under the License. -->


<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Security.Principal.Windows" Version="5.0.0" />
Expand Down
2 changes: 0 additions & 2 deletions Yubico.Core/src/Yubico/Core/Buffers/Hex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ namespace Yubico.Core.Buffers
/// <inheritdoc cref="Base16" path="/summary"/>
/// <remarks>
/// This class is an alias for <see cref="Base16"/>. New code should use that class
/// and existing code should be changed over time. Putting the <see cref="ObsoleteAttribute"/>
/// on this class is impractical.
/// </remarks>
public class Hex : Base16
{
Expand Down
8 changes: 4 additions & 4 deletions Yubico.Core/src/Yubico/Core/Devices/Hid/HidDeviceListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

using System;
using Yubico.Core.Logging;
using Microsoft.Extensions.Logging;
using Yubico.PlatformInterop;

namespace Yubico.Core.Devices.Hid
Expand All @@ -35,7 +35,7 @@ namespace Yubico.Core.Devices.Hid
/// </remarks>
public abstract class HidDeviceListener
{
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _logger = Logging.Log.GetLogger<HidDeviceListener>();

/// <summary>
/// Subscribe to receive an event whenever a Human Interface Device (HID) is added to the computer.
Expand Down Expand Up @@ -75,7 +75,7 @@ public static HidDeviceListener Create() =>
/// </param>
protected void OnArrived(IHidDevice device)
{
_log.LogInformation("HID {Device} arrived.", device);
_logger.LogInformation("HID {Device} arrived.", device);
Arrived?.Invoke(this, new HidDeviceEventArgs(device));
}

Expand All @@ -88,7 +88,7 @@ protected void OnArrived(IHidDevice device)
/// </param>
protected void OnRemoved(IHidDevice? device)
{
_log.LogInformation("HID {Device} removed.", device);
_logger.LogInformation("HID {Device} removed.", device);
Removed?.Invoke(this, new HidDeviceEventArgs(device));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Yubico.Core.Logging;
using Microsoft.Extensions.Logging;
using Yubico.PlatformInterop;

namespace Yubico.Core.Devices.Hid
{
internal static class HidLoggerExtensions
{
public static void IOKitApiCall(this Logger logger, string apiName, kern_return_t result)
public static void IOKitApiCall(this ILogger logger, string apiName, kern_return_t result)
{
if (result == kern_return_t.KERN_SUCCESS)
{
Expand Down
12 changes: 6 additions & 6 deletions Yubico.Core/src/Yubico/Core/Devices/Hid/LinuxHidDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Yubico.Core.Logging;
using Microsoft.Extensions.Logging;
using Yubico.PlatformInterop;

namespace Yubico.Core.Devices.Hid
Expand All @@ -34,7 +34,7 @@ internal class LinuxHidDevice : HidDevice

private readonly string _devnode;

private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<LinuxHidDevice>();

/// <summary>
/// Gets a list of all the HIDs on the system (not just YubiKeys).
Expand Down Expand Up @@ -78,7 +78,7 @@ internal LinuxHidDevice(string path, string devnode, string? parentPath) :
if (handle.IsInvalid)
{
_log.LogWarning(
"Could not open [{Path}]. Errno = {errno} {errorstring}",
"Could not open [{Path}]. Errno = {ErrNo} {ErrorString}",
path, Marshal.GetLastWin32Error(), LibcHelpers.GetErrnoString());
}

Expand Down Expand Up @@ -136,7 +136,7 @@ private void GetVendorProductIds(LinuxFileSafeHandle handle)
}
else
{
_log.LogWarning("IOCTL failed. {error}", LibcHelpers.GetErrnoString());
_log.LogWarning("IOCTL failed. {Error}", LibcHelpers.GetErrnoString());
}

Marshal.FreeHGlobal(infoStructData);
Expand All @@ -161,7 +161,7 @@ private void GetUsageProperties(LinuxFileSafeHandle handle)
}
else
{
_log.LogWarning("IOCTL failed. {error}", LibcHelpers.GetErrnoString());
_log.LogWarning("IOCTL failed. {Error}", LibcHelpers.GetErrnoString());
}
Marshal.FreeHGlobal(descriptorStructData);
}
Expand All @@ -178,7 +178,7 @@ private static int GetDescriptorLength(LinuxFileSafeHandle handle)
}
else
{
Log.GetLogger().LogWarning("IOCTL failed. {error}", LibcHelpers.GetErrnoString());
Logging.Log.GetLogger(typeof(LinuxHidDevice).FullName!).LogWarning("IOCTL failed. {Error}", LibcHelpers.GetErrnoString());
}

Marshal.FreeHGlobal(descSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System;
using System.Globalization;
using System.Security.Cryptography;
using Microsoft.Extensions.Logging;
using Yubico.Core.Buffers;
using Yubico.Core.Logging;
using Yubico.PlatformInterop;
Expand All @@ -28,7 +29,7 @@ internal class LinuxHidIOReportConnection : IHidConnection
private readonly LinuxFileSafeHandle _handle;
private bool _isDisposed;

private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<LinuxHidIOReportConnection>();
private readonly LinuxHidDevice _device;

public int InputReportSize { get; private set; }
Expand All @@ -46,7 +47,7 @@ public LinuxHidIOReportConnection(LinuxHidDevice device, string devnode)

if (_handle.IsInvalid)
{
_log.LogError("Could not open device for IO reports: {error}", LibcHelpers.GetErrnoString());
_log.LogError("Could not open device for IO reports: {Error}", LibcHelpers.GetErrnoString());

throw new PlatformApiException(
string.Format(
Expand Down Expand Up @@ -84,7 +85,7 @@ public void SetReport(byte[] report)
return;
}

_log.LogError("Write failed with: {error}", LibcHelpers.GetErrnoString());
_log.LogError("Write failed with: {Error}", LibcHelpers.GetErrnoString());

throw new PlatformApiException(
string.Format(
Expand All @@ -108,7 +109,7 @@ public byte[] GetReport()
return outputBuffer;
}

_log.LogError("Read failed with: {error}", LibcHelpers.GetErrnoString());
_log.LogError("Read failed with: {Error}", LibcHelpers.GetErrnoString());
throw new PlatformApiException(
string.Format(
CultureInfo.CurrentCulture,
Expand Down
22 changes: 11 additions & 11 deletions Yubico.Core/src/Yubico/Core/Devices/Hid/MacOSHidDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Yubico.Core.Logging;
using Microsoft.Extensions.Logging;
using Yubico.PlatformInterop;

using static Yubico.PlatformInterop.NativeMethods;
Expand All @@ -29,12 +29,12 @@ namespace Yubico.Core.Devices.Hid
internal class MacOSHidDevice : HidDevice
{
private readonly long _entryId;
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _logger = Logging.Log.GetLogger<MacOSHidDevice>();

public MacOSHidDevice(long entryId) :
base(entryId.ToString(CultureInfo.InvariantCulture))
{
_log.LogInformation(
_logger.LogInformation(
$"Creating new instance of MacOSHidDevice based on device with EntryID [{entryId}]",
entryId);

Expand All @@ -49,8 +49,8 @@ public MacOSHidDevice(long entryId) :
/// </returns>
public static IEnumerable<HidDevice> GetList()
{
Logger log = Log.GetLogger();
using IDisposable? logScope = log.BeginScope("MacOSHidDevice.GetList()");
ILogger logger = Logging.Log.GetLogger(typeof(MacOSHidDevice).FullName!);
using IDisposable? logScope = logger.BeginScope("MacOSHidDevice.GetList()");

IntPtr manager = IntPtr.Zero;
IntPtr deviceSet = IntPtr.Zero;
Expand All @@ -63,7 +63,7 @@ public static IEnumerable<HidDevice> GetList()
deviceSet = IOHIDManagerCopyDevices(manager);

long deviceSetCount = CFSetGetCount(deviceSet);
log.LogInformation("Found {DeviceCount} HID devices in this device set.", deviceSetCount);
logger.LogInformation("Found {DeviceCount} HID devices in this device set.", deviceSetCount);

var devices = new IntPtr[deviceSetCount];

Expand All @@ -84,13 +84,13 @@ public static IEnumerable<HidDevice> GetList()
{
if (manager != IntPtr.Zero)
{
log.LogInformation("IOHIDManager released.");
logger.LogInformation("IOHIDManager released.");
CFRelease(manager);
}

if (deviceSet != IntPtr.Zero)
{
log.LogInformation("HID device set released.");
logger.LogInformation("HID device set released.");
CFRelease(deviceSet);
}
}
Expand All @@ -116,11 +116,11 @@ public override IHidConnection ConnectToIOReports() =>

internal static long GetEntryId(IntPtr device)
{
Logger log = Log.GetLogger();
ILogger Logger = Logging.Log.GetLogger(typeof(MacOSHidDevice).FullName!);

int service = IOHIDDeviceGetService(device);
kern_return_t result = IORegistryEntryGetRegistryEntryID(service, out long entryId);
log.IOKitApiCall(nameof(IORegistryEntryGetRegistryEntryID), result);
Logger.IOKitApiCall(nameof(IORegistryEntryGetRegistryEntryID), result);

if (result != kern_return_t.KERN_SUCCESS)
{
Expand All @@ -136,7 +136,7 @@ internal static long GetEntryId(IntPtr device)
public void LogDeviceAccessTime()
{
LastAccessed = DateTime.Now;
_log.LogInformation("Updating last used for {Device} to {LastAccessed:hh:mm:ss.fffffff}", this, LastAccessed);
_logger.LogInformation("Updating last used for {Device} to {LastAccessed:hh:mm:ss.fffffff}", this, LastAccessed);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using System;
using System.Text;
using System.Threading;
using Yubico.Core.Logging;
using Microsoft.Extensions.Logging;
using static Yubico.PlatformInterop.NativeMethods;

namespace Yubico.Core.Devices.Hid
Expand All @@ -28,7 +28,7 @@ internal class MacOSHidDeviceListener : HidDeviceListener
private Thread? _listenerThread;
private IntPtr? _runLoop;

private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<MacOSHidDeviceListener>();

// Start listening as soon as this object is constructed.
public MacOSHidDeviceListener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using Microsoft.Extensions.Logging;
using Yubico.Core.Buffers;
using Yubico.Core.Logging;
using Yubico.PlatformInterop;
Expand All @@ -28,7 +29,7 @@ internal sealed class MacOSHidFeatureReportConnection : IHidConnection
private readonly MacOSHidDevice _device;
private readonly long _entryId;
private IntPtr _deviceHandle;
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<MacOSHidFeatureReportConnection>();

private bool _isDisposed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Extensions.Logging;
using Yubico.Core.Buffers;
using Yubico.Core.Logging;
using Yubico.PlatformInterop;
Expand All @@ -34,7 +35,7 @@ internal sealed class MacOSHidIOReportConnection : IHidConnection
private bool _isDisposed;
private readonly MacOSHidDevice _device;
private readonly IntPtr _loopId;
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<MacOSHidIOReportConnection>();

private readonly byte[] _readBuffer;
private GCHandle _readHandle;
Expand Down Expand Up @@ -246,14 +247,14 @@ private static void ReportCallback(
byte[] report,
long reportLength)
{
Logger log = Log.GetLogger();
ILogger logger = Logging.Log.GetLogger(typeof(MacOSHidIOReportConnection).FullName!);

log.LogInformation("MacOSHidIOReportConnection.ReportCallback has been called.");
logger.LogInformation("MacOSHidIOReportConnection.ReportCallback has been called.");

if (result != 0 || type != IOKitHidConstants.kIOHidReportTypeInput || reportId != 0 || reportLength < 0)
{
// Something went wrong. We don't currently signal, just continue.
log.LogWarning(
logger.LogWarning(
"ReportCallback did not receive some or all of the expected output.\n" +
"result = [{Result}], type = [{Type}], reportId = [{ReportId}], reportLength = [{ReportLength}]",
result,
Expand Down
4 changes: 2 additions & 2 deletions Yubico.Core/src/Yubico/Core/Devices/Hid/WindowsHidDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Yubico.Core.Logging;
using Microsoft.Extensions.Logging;
using Yubico.PlatformInterop;

namespace Yubico.Core.Devices.Hid
Expand All @@ -27,7 +27,7 @@ namespace Yubico.Core.Devices.Hid
/// </summary>
internal class WindowsHidDevice : HidDevice
{
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<WindowsHidDevice>();

/// <summary>
/// Gets the list of Windows HID devices available to the system.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using Yubico.Core.Logging;
using Microsoft.Extensions.Logging;
using Yubico.PlatformInterop;

using static Yubico.PlatformInterop.NativeMethods;
Expand All @@ -29,7 +29,7 @@ internal class WindowsHidDeviceListener : HidDeviceListener
private GCHandle? _marshalableThisPtr;
private CM_NOTIFY_CALLBACK? _callbackDelegate;

private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<WindowsHidDeviceListener>();

public WindowsHidDeviceListener()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

using System;
using Microsoft.Extensions.Logging;
using Yubico.Core.Iso7816;
using Yubico.Core.Logging;
using Yubico.PlatformInterop;

using static Yubico.PlatformInterop.NativeMethods;
Expand All @@ -23,15 +23,15 @@ namespace Yubico.Core.Devices.SmartCard
{
public class DesktopSmartCardConnection : ISmartCardConnection
{
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<DesktopSmartCardConnection>();
private readonly DesktopSmartCardDevice _device;
private readonly SCardContext _context;
private readonly SCardCardHandle _cardHandle;
private SCARD_PROTOCOL _activeProtocol;

private class TransactionScope : IDisposable
{
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<TransactionScope>();
private readonly DesktopSmartCardConnection _thisConnection;
private readonly IDisposable? _logScope;
private bool _disposedValue;
Expand Down
Loading
Loading