Skip to content
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
83 changes: 4 additions & 79 deletions src/c#/DrivelutionTest/Core/DriverUpdaterFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using GeneralUpdate.Drivelution.Core;
using GeneralUpdate.Drivelution.Abstractions;
using GeneralUpdate.Drivelution.Abstractions.Configuration;
using Serilog;
using Serilog.Core;

namespace DrivelutionTest.Core;

Expand All @@ -26,26 +24,6 @@ public void Create_WithoutParameters_ReturnsNonNullInstance()
Assert.IsAssignableFrom<IGeneralDrivelution>(updater);
}

/// <summary>
/// Tests that Create method accepts custom logger.
/// </summary>
[Fact]
public void Create_WithCustomLogger_ReturnsInstance()
{
// Arrange
var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();

// Act
var updater = DrivelutionFactory.Create(logger);

// Assert
Assert.NotNull(updater);
Assert.IsAssignableFrom<IGeneralDrivelution>(updater);
}

/// <summary>
/// Tests that Create method accepts custom options.
/// </summary>
Expand All @@ -55,12 +33,11 @@ public void Create_WithCustomOptions_ReturnsInstance()
// Arrange
var options = new DrivelutionOptions
{
LogLevel = "Debug",
LogFilePath = "./logs/test.log"
DefaultBackupPath = "./backups"
};

// Act
var updater = DrivelutionFactory.Create(null, options);
var updater = DrivelutionFactory.Create(options);

// Assert
Assert.NotNull(updater);
Expand Down Expand Up @@ -98,7 +75,7 @@ public void IsPlatformSupported_ReturnsBooleanValue()
/// Tests that CreateValidator returns a non-null instance.
/// </summary>
[Fact]
public void CreateValidator_WithoutLogger_ReturnsNonNullInstance()
public void CreateValidator_WithoutParameters_ReturnsNonNullInstance()
{
// Skip on MacOS and Unknown platforms
var platform = DrivelutionFactory.GetCurrentPlatform();
Expand All @@ -119,7 +96,7 @@ public void CreateValidator_WithoutLogger_ReturnsNonNullInstance()
/// Tests that CreateBackup returns a non-null instance.
/// </summary>
[Fact]
public void CreateBackup_WithoutLogger_ReturnsNonNullInstance()
public void CreateBackup_WithoutParameters_ReturnsNonNullInstance()
{
// Skip on MacOS and Unknown platforms
var platform = DrivelutionFactory.GetCurrentPlatform();
Expand All @@ -136,58 +113,6 @@ public void CreateBackup_WithoutLogger_ReturnsNonNullInstance()
Assert.IsAssignableFrom<IDriverBackup>(backup);
}

/// <summary>
/// Tests that CreateValidator with custom logger works correctly.
/// </summary>
[Fact]
public void CreateValidator_WithCustomLogger_ReturnsInstance()
{
// Skip on MacOS and Unknown platforms
var platform = DrivelutionFactory.GetCurrentPlatform();
if (platform == "MacOS" || platform == "Unknown")
{
return;
}

// Arrange
var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();

// Act
var validator = DrivelutionFactory.CreateValidator(logger);

// Assert
Assert.NotNull(validator);
}

/// <summary>
/// Tests that CreateBackup with custom logger works correctly.
/// </summary>
[Fact]
public void CreateBackup_WithCustomLogger_ReturnsInstance()
{
// Skip on MacOS and Unknown platforms
var platform = DrivelutionFactory.GetCurrentPlatform();
if (platform == "MacOS" || platform == "Unknown")
{
return;
}

// Arrange
var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();

// Act
var backup = DrivelutionFactory.CreateBackup(logger);

// Assert
Assert.NotNull(backup);
}

/// <summary>
/// Tests that Create throws PlatformNotSupportedException on unsupported platforms.
/// This test documents expected behavior but cannot be easily tested on supported platforms.
Expand Down
2 changes: 1 addition & 1 deletion src/c#/DrivelutionTest/DrivelutionTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
46 changes: 1 addition & 45 deletions src/c#/DrivelutionTest/GeneralDrivelutionTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using GeneralUpdate.Drivelution;
using GeneralUpdate.Drivelution.Abstractions.Models;
using GeneralUpdate.Drivelution.Abstractions.Configuration;
using Serilog;

namespace DrivelutionTest;

Expand Down Expand Up @@ -33,8 +32,7 @@ public void Create_WithOptions_ReturnsNonNullInstance()
// Arrange
var options = new DrivelutionOptions
{
LogLevel = "Information",
LogFilePath = "./logs/test.log"
DefaultBackupPath = "./backups"
};

// Act
Expand All @@ -44,48 +42,6 @@ public void Create_WithOptions_ReturnsNonNullInstance()
Assert.NotNull(updater);
}

/// <summary>
/// Tests that Create method with custom logger returns a non-null instance.
/// </summary>
[Fact]
public void Create_WithCustomLogger_ReturnsNonNullInstance()
{
// Arrange
var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();

// Act
var updater = GeneralDrivelution.Create(logger);

// Assert
Assert.NotNull(updater);
}

/// <summary>
/// Tests that Create method with custom logger and options returns instance.
/// </summary>
[Fact]
public void Create_WithCustomLoggerAndOptions_ReturnsInstance()
{
// Arrange
var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();
var options = new DrivelutionOptions
{
LogLevel = "Debug"
};

// Act
var updater = GeneralDrivelution.Create(logger, options);

// Assert
Assert.NotNull(updater);
}

/// <summary>
/// Tests that GetPlatformInfo returns valid platform information.
/// </summary>
Expand Down
40 changes: 37 additions & 3 deletions src/c#/GeneralUpdate.Core/GeneralUpdate.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
<RepositoryType>public</RepositoryType>
<PackageTags>upgrade,update</PackageTags>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net10.0</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0'">
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\GeneralUpdate.Common\Compress\CompressProvider.cs" Link="Common\CompressProvider.cs" />
Expand Down Expand Up @@ -90,8 +93,39 @@
<Compile Include="..\GeneralUpdate.Differential\Binary\StrangeCRC.cs" Link="Common\StrangeCRC.cs" />
<Compile Include="..\GeneralUpdate.Differential\DifferentialCore.cs" Link="Common\DifferentialCore.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<ProjectReference Include="..\GeneralUpdate.Drivelution\GeneralUpdate.Drivelution.csproj" />
<!-- Link Drivelution files for net10.0 target only -->
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\Configuration\DriverUpdateOptions.cs" Link="Common\DriverUpdateOptions.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\Events\DrivelutionLogger.cs" Link="Common\DrivelutionLogger.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\Events\IDrivelutionLogger.cs" Link="Common\IDrivelutionLogger.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\Events\LogEventArgs.cs" Link="Common\LogEventArgs.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\Exceptions\DriverUpdateExceptions.cs" Link="Common\DriverUpdateExceptions.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\IDriverBackup.cs" Link="Common\IDriverBackup.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\IDriverValidator.cs" Link="Common\IDriverValidator.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\IGeneralDrivelution.cs" Link="Common\IGeneralDrivelution.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\INetworkDownloader.cs" Link="Common\INetworkDownloader.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\Models\DriverInfo.cs" Link="Common\DriverInfo.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\Models\ErrorInfo.cs" Link="Common\ErrorInfo.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\Models\UpdateResult.cs" Link="Common\UpdateResult.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Abstractions\Models\UpdateStrategy.cs" Link="Common\UpdateStrategy.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Core\DriverUpdaterFactory.cs" Link="Common\DriverUpdaterFactory.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Core\Logging\LoggerConfigurator.cs" Link="Common\LoggerConfigurator.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Core\Utilities\CompatibilityChecker.cs" Link="Common\CompatibilityChecker.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Core\Utilities\HashValidator.cs" Link="Common\HashValidator.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Core\Utilities\RestartHelper.cs" Link="Common\RestartHelper.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Core\Utilities\VersionComparer.cs" Link="Common\VersionComparer.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\GeneralDrivelution.cs" Link="Common\GeneralDrivelution.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Linux\Helpers\LinuxPermissionHelper.cs" Link="Common\LinuxPermissionHelper.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Linux\Helpers\LinuxSignatureHelper.cs" Link="Common\LinuxSignatureHelper.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Linux\Implementation\LinuxDriverBackup.cs" Link="Common\LinuxDriverBackup.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Linux\Implementation\LinuxDriverValidator.cs" Link="Common\LinuxDriverValidator.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Linux\Implementation\LinuxGeneralDrivelution.cs" Link="Common\LinuxGeneralDrivelution.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\MacOS\Implementation\MacOsGeneralDrivelution.cs" Link="Common\MacOsGeneralDrivelution.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Windows\Helpers\WindowsPermissionHelper.cs" Link="Common\WindowsPermissionHelper.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Windows\Helpers\WindowsSignatureHelper.cs" Link="Common\WindowsSignatureHelper.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Windows\Implementation\WindowsDriverBackup.cs" Link="Common\WindowsDriverBackup.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Windows\Implementation\WindowsDriverValidator.cs" Link="Common\WindowsDriverValidator.cs" />
<Compile Include="..\GeneralUpdate.Drivelution\Windows\Implementation\WindowsGeneralDrivelution.cs" Link="Common\WindowsGeneralDrivelution.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Common\" />
Expand Down
5 changes: 1 addition & 4 deletions src/c#/GeneralUpdate.Core/Pipeline/DrivelutionMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#if NET8_0_OR_GREATER
#if NET10_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -17,8 +16,6 @@ namespace GeneralUpdate.Core.Pipeline;
/// </summary>
public class DrivelutionMiddleware : IMiddleware
{
[RequiresUnreferencedCode("Driver update process includes signature validation that may require runtime reflection on some platforms")]
[RequiresDynamicCode("Driver update process includes signature validation that may require runtime code generation on some platforms")]
public async Task InvokeAsync(PipelineContext context)
{
try
Expand Down
2 changes: 1 addition & 1 deletion src/c#/GeneralUpdate.Core/Strategys/LinuxStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override PipelineBuilder BuildPipeline(PipelineContext context)
.UseMiddlewareIf<PatchMiddleware>(_configinfo.PatchEnabled)
.UseMiddleware<CompressMiddleware>()
.UseMiddleware<HashMiddleware>();
#if NET8_0_OR_GREATER
#if NET10_0_OR_GREATER
builder.UseMiddlewareIf<DrivelutionMiddleware>(_configinfo.DriveEnabled == true);
#endif
return builder;
Expand Down
2 changes: 1 addition & 1 deletion src/c#/GeneralUpdate.Core/Strategys/WindowsStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override PipelineBuilder BuildPipeline(PipelineContext context)
.UseMiddlewareIf<PatchMiddleware>(_configinfo.PatchEnabled)
.UseMiddleware<CompressMiddleware>()
.UseMiddleware<HashMiddleware>();
#if NET8_0_OR_GREATER
#if NET10_0_OR_GREATER
builder.UseMiddlewareIf<DrivelutionMiddleware>(_configinfo.DriveEnabled == true);
#endif
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,7 @@ public class DrivelutionOptions
/// </summary>
public string DefaultBackupPath { get; set; } = "./DriverBackups";

/// <summary>
/// 日志级别(Debug/Info/Warn/Error/Fatal)
/// Log level (Debug/Info/Warn/Error/Fatal)
/// </summary>
public string LogLevel { get; set; } = "Info";

/// <summary>
/// 日志文件路径
/// Log file path
/// </summary>
public string LogFilePath { get; set; } = "./Logs/drivelution-.log";

/// <summary>
/// 是否启用控制台日志
/// Enable console logging
/// </summary>
public bool EnableConsoleLogging { get; set; } = true;

/// <summary>
/// 是否启用文件日志
/// Enable file logging
/// </summary>
public bool EnableFileLogging { get; set; } = true;

/// <summary>
/// 默认重试次数
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace GeneralUpdate.Drivelution.Abstractions.Events;

/// <summary>
/// Default implementation of IDrivelutionLogger that raises events
/// </summary>
public class DrivelutionLogger : IDrivelutionLogger
{
/// <inheritdoc />
public event EventHandler<LogEventArgs>? LogMessage;

/// <inheritdoc />
public void Debug(string message, Exception? exception = null, params object[] args)
{
RaiseLogEvent(LogLevel.Debug, message, exception, args);
}

/// <inheritdoc />
public void Information(string message, Exception? exception = null, params object[] args)
{
RaiseLogEvent(LogLevel.Information, message, exception, args);
}

/// <inheritdoc />
public void Warning(string message, Exception? exception = null, params object[] args)
{
RaiseLogEvent(LogLevel.Warning, message, exception, args);
}

/// <inheritdoc />
public void Error(string message, Exception? exception = null, params object[] args)
{
RaiseLogEvent(LogLevel.Error, message, exception, args);
}

/// <inheritdoc />
public void Fatal(string message, Exception? exception = null, params object[] args)
{
RaiseLogEvent(LogLevel.Fatal, message, exception, args);
}

private void RaiseLogEvent(LogLevel level, string message, Exception? exception, params object[] args)
{
try
{
// Format message if args provided
var formattedMessage = args.Length > 0 ? string.Format(message, args) : message;

var eventArgs = new LogEventArgs
{
Level = level,
Message = formattedMessage,
Exception = exception,
Timestamp = DateTime.UtcNow
};

LogMessage?.Invoke(this, eventArgs);
}
catch
{
// Silently ignore exceptions in logging to prevent cascading failures
}
}
Comment on lines +41 to +62
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

IDrivelutionLogger methods accept params object[] args, but the implementation uses string.Format(message, args), which is incompatible with Serilog-style message templates (e.g., {DriverName}) and will throw FormatException. Because the catch block swallows all exceptions, logging can silently disappear. Either remove args and require pre-formatted strings, or implement a formatter compatible with the expected template syntax and avoid swallowing formatting errors silently.

Suggested change
private void RaiseLogEvent(LogLevel level, string message, Exception? exception, params object[] args)
{
try
{
// Format message if args provided
var formattedMessage = args.Length > 0 ? string.Format(message, args) : message;
var eventArgs = new LogEventArgs
{
Level = level,
Message = formattedMessage,
Exception = exception,
Timestamp = DateTime.UtcNow
};
LogMessage?.Invoke(this, eventArgs);
}
catch
{
// Silently ignore exceptions in logging to prevent cascading failures
}
}
private static string FormatMessage(string message, object[] args)
{
if (args == null || args.Length == 0)
{
return message;
}
try
{
return string.Format(message, args);
}
catch (FormatException)
{
// Fallback: preserve the original template and append the argument values
return message + " | Args: " + string.Join(", ", args);
}
}
private void RaiseLogEvent(LogLevel level, string message, Exception? exception, params object[] args)
{
var formattedMessage = FormatMessage(message, args);
var eventArgs = new LogEventArgs
{
Level = level,
Message = formattedMessage,
Exception = exception,
Timestamp = DateTime.UtcNow
};
LogMessage?.Invoke(this, eventArgs);
}

Copilot uses AI. Check for mistakes.
}
Loading
Loading