Skip to content

Commit

Permalink
Merge pull request #139 from Yubico/dennisdyallo/logging
Browse files Browse the repository at this point in the history
Now using Microsoft.Extensions.Logging.Console as the default logger
  • Loading branch information
DennisDyallo authored Aug 13, 2024
2 parents 9a875a7 + d40d837 commit 299b6ae
Show file tree
Hide file tree
Showing 79 changed files with 1,835 additions and 807 deletions.
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
4 changes: 2 additions & 2 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 _log = Logging.Log.GetLogger<HidDeviceListener>();

/// <summary>
/// Subscribe to receive an event whenever a Human Interface Device (HID) is added to the computer.
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
11 changes: 6 additions & 5 deletions Yubico.Core/src/Yubico/Core/Devices/Hid/MacOSHidDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Yubico.Core.Logging;
using Microsoft.Extensions.Logging;
using Yubico.PlatformInterop;
using Yubico.Core.Logging;

using static Yubico.PlatformInterop.NativeMethods;

namespace Yubico.Core.Devices.Hid
{
/// <summary>
/// macOS implementation of a Human Interface Device (HID)
/// MacOS implementation of a Human Interface Device (HID)
/// </summary>
internal class MacOSHidDevice : HidDevice
{
private readonly long _entryId;
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Log.GetLogger<MacOSHidDevice>();

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

IntPtr manager = IntPtr.Zero;
Expand Down Expand Up @@ -116,7 +117,7 @@ public override IHidConnection ConnectToIOReports() =>

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

int service = IOHIDDeviceGetService(device);
kern_return_t result = IORegistryEntryGetRegistryEntryID(service, out long entryId);
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Extensions.Logging;
using Yubico.Core.Iso7816;
using Yubico.Core.Logging;
using Yubico.PlatformInterop;
Expand All @@ -27,11 +28,11 @@ namespace Yubico.Core.Devices.SmartCard
internal class DesktopSmartCardDevice : SmartCardDevice
{
private readonly string _readerName;
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Log.GetLogger<DesktopSmartCardDevice>();

public static IReadOnlyList<ISmartCardDevice> GetList()
{
Logger log = Log.GetLogger();
ILogger log = Log.GetLogger<DesktopSmartCardDevice>();
using IDisposable? logScope = log.BeginScope("SmartCardDevice.GetList()");

uint result = SCardEstablishContext(SCARD_SCOPE.USER, out SCardContext context);
Expand Down Expand Up @@ -112,7 +113,6 @@ public DesktopSmartCardDevice(string readerName, AnswerToReset? atr) :
base(readerName, atr)
{
_readerName = readerName;
_log = Log.GetLogger();
}

public override ISmartCardConnection Connect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Yubico.Core.Logging;
using Microsoft.Extensions.Logging;
using Yubico.PlatformInterop;

using static Yubico.PlatformInterop.NativeMethods;
Expand All @@ -29,7 +29,7 @@ namespace Yubico.Core.Devices.SmartCard
/// </summary>
internal class DesktopSmartCardDeviceListener : SmartCardDeviceListener, IDisposable
{
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<DesktopSmartCardDeviceListener>();

// The resource manager context.
private SCardContext _context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

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

namespace Yubico.Core.Devices.SmartCard
Expand All @@ -25,7 +25,7 @@ namespace Yubico.Core.Devices.SmartCard
/// </summary>
public abstract class SmartCardDevice : ISmartCardDevice
{
private readonly Logger _log = Log.GetLogger();
private readonly ILogger _log = Logging.Log.GetLogger<SmartCardDevice>();

/// <inheritdoc />
public DateTime LastAccessed { get; protected set; } = DateTime.MinValue;
Expand Down
Loading

0 comments on commit 299b6ae

Please sign in to comment.