mirror of
https://github.com/AlexMacocian/SystemExtensions.git
synced 2026-07-15 14:19:29 +00:00
Deprecate logging and config abstractions, in favor of standard MS provided ones
This commit is contained in:
@@ -33,7 +33,7 @@ jobs:
|
|||||||
- name: Install .NET Core
|
- name: Install .NET Core
|
||||||
uses: actions/setup-dotnet@v3
|
uses: actions/setup-dotnet@v3
|
||||||
with:
|
with:
|
||||||
dotnet-version: '6.0.x'
|
dotnet-version: '10.0.x'
|
||||||
|
|
||||||
- name: Setup MSBuild.exe
|
- name: Setup MSBuild.exe
|
||||||
uses: microsoft/setup-msbuild@v1.0.1
|
uses: microsoft/setup-msbuild@v1.0.1
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ jobs:
|
|||||||
- name: Install .NET Core
|
- name: Install .NET Core
|
||||||
uses: actions/setup-dotnet@v3
|
uses: actions/setup-dotnet@v3
|
||||||
with:
|
with:
|
||||||
dotnet-version: '6.0.x'
|
dotnet-version: '10.0.x'
|
||||||
|
|
||||||
- name: Setup MSBuild.exe
|
- name: Setup MSBuild.exe
|
||||||
uses: microsoft/setup-msbuild@v1.0.1
|
uses: microsoft/setup-msbuild@v1.0.1
|
||||||
|
|||||||
-25
@@ -1,25 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Configuration;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class DefaultOptionsManagerTests
|
|
||||||
{
|
|
||||||
private readonly DefaultOptionsManager optionsManager = new();
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void GetOptions_ReturnsDefault()
|
|
||||||
{
|
|
||||||
var options = this.optionsManager.GetOptions<string>();
|
|
||||||
|
|
||||||
options.Should().BeNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void UpdateOptions_Succeeds()
|
|
||||||
{
|
|
||||||
this.optionsManager.UpdateOptions(string.Empty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
namespace SystemExtensions.DependencyInjection.Tests.Configuration;
|
|
||||||
|
|
||||||
public sealed class DummyOptions
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using NSubstitute;
|
|
||||||
using System;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Configuration;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class LiveOptionsResolverTests
|
|
||||||
{
|
|
||||||
private readonly LiveOptionsResolver liveOptionsResolver = new();
|
|
||||||
private readonly Slim.IServiceProvider serviceProviderMock = Substitute.For<Slim.IServiceProvider>();
|
|
||||||
private readonly IOptionsManager optionsManagerMock = Substitute.For<IOptionsManager>();
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_ILiveOptions_ReturnsTrue()
|
|
||||||
{
|
|
||||||
var type = typeof(ILiveOptions<string>);
|
|
||||||
|
|
||||||
var canResolve = this.liveOptionsResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
|
||||||
{
|
|
||||||
var types = new Type[] { typeof(object), typeof(string), typeof(LiveOptionsResolverTests), typeof(int) };
|
|
||||||
|
|
||||||
foreach (var type in types)
|
|
||||||
{
|
|
||||||
var canResolve = this.liveOptionsResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeFalse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_ILiveOptions_ReturnsILiveOptions()
|
|
||||||
{
|
|
||||||
this.SetupServiceProvider();
|
|
||||||
|
|
||||||
var liveOptions = this.liveOptionsResolver.Resolve(this.serviceProviderMock, typeof(ILiveOptions<string>));
|
|
||||||
|
|
||||||
liveOptions.Should().BeAssignableTo<ILiveOptions<string>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_AnythingElse_Throws()
|
|
||||||
{
|
|
||||||
this.SetupServiceProvider();
|
|
||||||
|
|
||||||
Action action = new(() =>
|
|
||||||
{
|
|
||||||
this.liveOptionsResolver.Resolve(this.serviceProviderMock, typeof(string));
|
|
||||||
});
|
|
||||||
|
|
||||||
action.Should().Throw<Exception>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetupServiceProvider()
|
|
||||||
{
|
|
||||||
this.serviceProviderMock.GetService<IOptionsManager>().Returns(this.optionsManagerMock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-66
@@ -1,66 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using NSubstitute;
|
|
||||||
using System;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Configuration;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class LiveUpdateableOptionsResolverTests
|
|
||||||
{
|
|
||||||
private readonly LiveUpdateableOptionsResolver liveUpdateableOptionsResolver = new();
|
|
||||||
private readonly Slim.IServiceProvider serviceProviderMock = Substitute.For<Slim.IServiceProvider>();
|
|
||||||
private readonly IOptionsManager optionsManagerMock = Substitute.For<IOptionsManager>();
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_ILiveUpdateableOptions_ReturnsTrue()
|
|
||||||
{
|
|
||||||
var type = typeof(ILiveUpdateableOptions<string>);
|
|
||||||
|
|
||||||
var canResolve = this.liveUpdateableOptionsResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
|
||||||
{
|
|
||||||
var types = new Type[] { typeof(object), typeof(string), typeof(LiveOptionsResolverTests), typeof(int) };
|
|
||||||
|
|
||||||
foreach (var type in types)
|
|
||||||
{
|
|
||||||
var canResolve = this.liveUpdateableOptionsResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeFalse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_ILiveUpdateableOptions_ReturnsILiveUpdateableOptions()
|
|
||||||
{
|
|
||||||
this.SetupServiceProvider();
|
|
||||||
|
|
||||||
var liveOptions = this.liveUpdateableOptionsResolver.Resolve(this.serviceProviderMock, typeof(ILiveUpdateableOptions<string>));
|
|
||||||
|
|
||||||
liveOptions.Should().BeAssignableTo<ILiveUpdateableOptions<string>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_AnythingElse_Throws()
|
|
||||||
{
|
|
||||||
this.SetupServiceProvider();
|
|
||||||
|
|
||||||
Action action = new(() =>
|
|
||||||
{
|
|
||||||
this.liveUpdateableOptionsResolver.Resolve(this.serviceProviderMock, typeof(string));
|
|
||||||
});
|
|
||||||
|
|
||||||
action.Should().Throw<Exception>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetupServiceProvider()
|
|
||||||
{
|
|
||||||
this.serviceProviderMock.GetService<IOptionsManager>().Returns(this.optionsManagerMock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-37
@@ -1,37 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using NSubstitute;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Configuration;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class LiveUpdateableOptionsWrapperTests
|
|
||||||
{
|
|
||||||
private LiveUpdateableOptionsWrapper<string> optionsWrapper;
|
|
||||||
private readonly IOptionsManager optionsManagerMock = Substitute.For<IOptionsManager>();
|
|
||||||
|
|
||||||
[TestInitialize]
|
|
||||||
public void TestInitialize()
|
|
||||||
{
|
|
||||||
this.optionsWrapper = new LiveUpdateableOptionsWrapper<string>(this.optionsManagerMock);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void GetValue_ReturnsValue()
|
|
||||||
{
|
|
||||||
this.optionsManagerMock.GetOptions<string>().Returns("hello");
|
|
||||||
|
|
||||||
var value = this.optionsWrapper.Value;
|
|
||||||
|
|
||||||
value.Should().Be("hello");
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void UpdateOption_CallsOptionsManager()
|
|
||||||
{
|
|
||||||
this.optionsWrapper.UpdateOption();
|
|
||||||
|
|
||||||
this.optionsManagerMock.ReceivedWithAnyArgs().UpdateOptions(Arg.Any<string>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using NSubstitute;
|
|
||||||
using System;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Configuration;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class OptionsResolverTests
|
|
||||||
{
|
|
||||||
private readonly OptionsResolver optionsResolver = new();
|
|
||||||
private readonly Slim.IServiceProvider serviceProviderMock = Substitute.For<Slim.IServiceProvider>();
|
|
||||||
|
|
||||||
public IOptionsManager OptionsManagerMock { get; } = Substitute.For<IOptionsManager>();
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_ILiveOptions_ReturnsTrue()
|
|
||||||
{
|
|
||||||
var type = typeof(IOptions<string>);
|
|
||||||
|
|
||||||
var canResolve = this.optionsResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
|
||||||
{
|
|
||||||
var types = new Type[] { typeof(object), typeof(string), typeof(OptionsResolverTests), typeof(int) };
|
|
||||||
|
|
||||||
foreach (var type in types)
|
|
||||||
{
|
|
||||||
var canResolve = this.optionsResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeFalse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_IOptions_ReturnsIOptions()
|
|
||||||
{
|
|
||||||
this.SetupServiceProvider();
|
|
||||||
|
|
||||||
var liveOptions = this.optionsResolver.Resolve(this.serviceProviderMock, typeof(IOptions<string>));
|
|
||||||
|
|
||||||
liveOptions.Should().BeAssignableTo<IOptions<string>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_AnythingElse_Throws()
|
|
||||||
{
|
|
||||||
this.SetupServiceProvider();
|
|
||||||
|
|
||||||
Action action = new(() =>
|
|
||||||
{
|
|
||||||
this.optionsResolver.Resolve(this.serviceProviderMock, typeof(string));
|
|
||||||
});
|
|
||||||
|
|
||||||
action.Should().Throw<Exception>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetupServiceProvider()
|
|
||||||
{
|
|
||||||
this.serviceProviderMock.GetService<IOptionsManager>().Returns(this.OptionsManagerMock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-66
@@ -1,66 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using NSubstitute;
|
|
||||||
using System;
|
|
||||||
using System.Configuration;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Configuration;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class UpdateableOptionsResolverTests
|
|
||||||
{
|
|
||||||
private readonly UpdateableOptionsResolver updateableOptionsResolver = new();
|
|
||||||
private readonly Slim.IServiceProvider serviceProviderMock = Substitute.For<Slim.IServiceProvider>();
|
|
||||||
private readonly IOptionsManager optionsManagerMock = Substitute.For<IOptionsManager>();
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_ILiveOptions_ReturnsTrue()
|
|
||||||
{
|
|
||||||
var type = typeof(IUpdateableOptions<string>);
|
|
||||||
|
|
||||||
var canResolve = this.updateableOptionsResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
|
||||||
{
|
|
||||||
var types = new Type[] { typeof(object), typeof(string), typeof(UpdateableOptionsResolverTests), typeof(int) };
|
|
||||||
|
|
||||||
foreach (var type in types)
|
|
||||||
{
|
|
||||||
var canResolve = this.updateableOptionsResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeFalse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_IUpdateableOptions_ReturnsIUpdateableOptions()
|
|
||||||
{
|
|
||||||
this.SetupServiceProvider();
|
|
||||||
|
|
||||||
var liveOptions = this.updateableOptionsResolver.Resolve(this.serviceProviderMock, typeof(IUpdateableOptions<string>));
|
|
||||||
|
|
||||||
liveOptions.Should().BeAssignableTo<IUpdateableOptions<string>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_AnythingElse_Throws()
|
|
||||||
{
|
|
||||||
this.SetupServiceProvider();
|
|
||||||
|
|
||||||
Action action = new(() =>
|
|
||||||
{
|
|
||||||
this.updateableOptionsResolver.Resolve(this.serviceProviderMock, typeof(string));
|
|
||||||
});
|
|
||||||
|
|
||||||
action.Should().Throw<Exception>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetupServiceProvider()
|
|
||||||
{
|
|
||||||
this.serviceProviderMock.GetService<IOptionsManager>().Returns(this.optionsManagerMock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-42
@@ -1,42 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using NSubstitute;
|
|
||||||
using NSubstitute.ExceptionExtensions;
|
|
||||||
using System;
|
|
||||||
using System.Configuration;
|
|
||||||
using System.Extensions.Configuration;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Configuration;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class UpdateableOptionsWrapperTests
|
|
||||||
{
|
|
||||||
private const string Value = "hello";
|
|
||||||
|
|
||||||
private UpdateableOptionsWrapper<string> optionsWrapper;
|
|
||||||
private readonly IOptionsManager optionsManagerMock = Substitute.For<IOptionsManager>();
|
|
||||||
|
|
||||||
[TestInitialize]
|
|
||||||
public void TestInitialize()
|
|
||||||
{
|
|
||||||
this.optionsWrapper = new UpdateableOptionsWrapper<string>(this.optionsManagerMock, Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void GetValue_ReturnsValue()
|
|
||||||
{
|
|
||||||
this.optionsManagerMock.GetOptions<string>().Throws<Exception>();
|
|
||||||
|
|
||||||
var value = this.optionsWrapper.Value;
|
|
||||||
|
|
||||||
value.Should().Be(Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void UpdateOption_CallsOptionsManager()
|
|
||||||
{
|
|
||||||
this.optionsWrapper.UpdateOption();
|
|
||||||
|
|
||||||
this.optionsManagerMock.ReceivedWithAnyArgs().UpdateOptions(Arg.Any<string>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using NSubstitute;
|
|
||||||
using System.Logging;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Logging;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class CVLoggerProviderTests
|
|
||||||
{
|
|
||||||
private readonly ILogsWriter logsWriterMock = Substitute.For<ILogsWriter>();
|
|
||||||
private CVLoggerProvider cVLoggerProvider;
|
|
||||||
|
|
||||||
[TestInitialize]
|
|
||||||
public void TestInitialize()
|
|
||||||
{
|
|
||||||
this.cVLoggerProvider = new CVLoggerProvider(this.logsWriterMock);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CreateLogger_CreatesNewLogger()
|
|
||||||
{
|
|
||||||
var logger = this.cVLoggerProvider.CreateLogger(string.Empty);
|
|
||||||
|
|
||||||
logger.Should().NotBeNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void LogEntry_CallsLogWriter()
|
|
||||||
{
|
|
||||||
this.cVLoggerProvider.LogEntry(new Log());
|
|
||||||
|
|
||||||
this.logsWriterMock.ReceivedWithAnyArgs().WriteLog(Arg.Any<Log>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using NSubstitute;
|
|
||||||
using System;
|
|
||||||
using System.Logging;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Logging;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class CVLoggerTests
|
|
||||||
{
|
|
||||||
private readonly ICVLoggerProvider cvLoggerProviderMock = Substitute.For<ICVLoggerProvider>();
|
|
||||||
private CVLogger cVLogger;
|
|
||||||
|
|
||||||
[TestInitialize]
|
|
||||||
public void TestInitialize()
|
|
||||||
{
|
|
||||||
this.cVLogger = new CVLogger(string.Empty, this.cvLoggerProviderMock);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void BeginScope_ReturnsNull()
|
|
||||||
{
|
|
||||||
var scope = this.cVLogger.BeginScope(string.Empty);
|
|
||||||
|
|
||||||
scope.Should().BeNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
[DataRow(LogLevel.Debug)]
|
|
||||||
[DataRow(LogLevel.Trace)]
|
|
||||||
[DataRow(LogLevel.Information)]
|
|
||||||
[DataRow(LogLevel.Warning)]
|
|
||||||
[DataRow(LogLevel.Error)]
|
|
||||||
[DataRow(LogLevel.Critical)]
|
|
||||||
public void IsEnabled_OnAllLogLevels_ReturnsTrue(LogLevel logLevel)
|
|
||||||
{
|
|
||||||
var enabled = this.cVLogger.IsEnabled(logLevel);
|
|
||||||
|
|
||||||
enabled.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Log_CallsFormatter()
|
|
||||||
{
|
|
||||||
var called = false;
|
|
||||||
Func<string, Exception, string> messageFormatter = new((state, exception) =>
|
|
||||||
{
|
|
||||||
called = true;
|
|
||||||
return string.Empty;
|
|
||||||
});
|
|
||||||
|
|
||||||
this.cVLogger.Log(LogLevel.Debug, new EventId(), "Some message", new Exception(), messageFormatter);
|
|
||||||
|
|
||||||
called.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Log_CallsLogsProvider()
|
|
||||||
{
|
|
||||||
this.cVLogger.Log(LogLevel.Debug, new EventId(), "Some message", new Exception(), new Func<string, Exception, string>((s, e) => string.Empty));
|
|
||||||
|
|
||||||
this.cvLoggerProviderMock.ReceivedWithAnyArgs().LogEntry(Arg.Any<Log>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using System;
|
|
||||||
using System.Logging;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Logging;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class DebugLogsWriterTests
|
|
||||||
{
|
|
||||||
private readonly DebugLogsWriter logsWriter = new();
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void WriteLog_Succeeds()
|
|
||||||
{
|
|
||||||
this.logsWriter.WriteLog(new Log());
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void WriteNullLog_Throws()
|
|
||||||
{
|
|
||||||
Action action = new(() =>
|
|
||||||
{
|
|
||||||
this.logsWriter.WriteLog(null);
|
|
||||||
});
|
|
||||||
|
|
||||||
action.Should().Throw<Exception>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using NSubstitute;
|
|
||||||
using System;
|
|
||||||
using System.Logging;
|
|
||||||
|
|
||||||
namespace SystemExtensions.DependencyInjection.Tests.Logging;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class LoggerResolverTests
|
|
||||||
{
|
|
||||||
private readonly Slim.IServiceProvider serviceProviderMock = Substitute.For<Slim.IServiceProvider>();
|
|
||||||
private readonly ILoggerFactory loggerFactoryMock = Substitute.For<ILoggerFactory>();
|
|
||||||
private readonly ILogger loggerMock = Substitute.For<ILogger>();
|
|
||||||
private readonly LoggerResolver loggerResolver = new();
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_ILogger_ReturnsTrue()
|
|
||||||
{
|
|
||||||
var type = typeof(ILogger);
|
|
||||||
|
|
||||||
var canResolve = this.loggerResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_GenericILogger_ReturnsTrue()
|
|
||||||
{
|
|
||||||
var type = typeof(ILogger<string>);
|
|
||||||
|
|
||||||
var canResolve = this.loggerResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void CanResolve_AnythingElse_ReturnsFalse()
|
|
||||||
{
|
|
||||||
var types = new Type[] { typeof(CVLogger), typeof(object), typeof(string), typeof(LoggerResolverTests), typeof(int) };
|
|
||||||
|
|
||||||
foreach(var type in types)
|
|
||||||
{
|
|
||||||
var canResolve = this.loggerResolver.CanResolve(type);
|
|
||||||
|
|
||||||
canResolve.Should().BeFalse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_ILogger_ReturnsILogger()
|
|
||||||
{
|
|
||||||
this.SetupIServiceProvider();
|
|
||||||
|
|
||||||
var logger = this.loggerResolver.Resolve(this.serviceProviderMock, typeof(ILogger));
|
|
||||||
|
|
||||||
logger.Should().BeAssignableTo<ILogger>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_TypedILogger_ReturnsTypedILogger()
|
|
||||||
{
|
|
||||||
this.SetupIServiceProvider();
|
|
||||||
|
|
||||||
var logger = this.loggerResolver.Resolve(this.serviceProviderMock, typeof(ILogger<string>));
|
|
||||||
|
|
||||||
logger.Should().BeAssignableTo<ILogger<string>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_RandomType_Throws()
|
|
||||||
{
|
|
||||||
this.SetupIServiceProvider();
|
|
||||||
|
|
||||||
Action action = new(() =>
|
|
||||||
{
|
|
||||||
this.loggerResolver.Resolve(this.serviceProviderMock, typeof(string));
|
|
||||||
});
|
|
||||||
|
|
||||||
action.Should().Throw<Exception>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void Resolve_NonTypedGeneric_Throws()
|
|
||||||
{
|
|
||||||
this.SetupIServiceProvider();
|
|
||||||
|
|
||||||
Action action = new(() =>
|
|
||||||
{
|
|
||||||
this.loggerResolver.Resolve(this.serviceProviderMock, typeof(ILogger<>));
|
|
||||||
});
|
|
||||||
|
|
||||||
action.Should().Throw<Exception>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SetupIServiceProvider()
|
|
||||||
{
|
|
||||||
this.serviceProviderMock.GetService<ILoggerFactory>().Returns(this.loggerFactoryMock);
|
|
||||||
|
|
||||||
this.loggerFactoryMock.CreateLogger(Arg.Any<string>()).Returns(this.loggerMock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>System</RootNamespace>
|
<RootNamespace>System</RootNamespace>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||||
<Version>1.6.12</Version>
|
<Version>1.7</Version>
|
||||||
<Authors>Alexandru Macocian</Authors>
|
<Authors>Alexandru Macocian</Authors>
|
||||||
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
||||||
<Description>Extensions for the System namespace</Description>
|
<Description>Extensions for the System namespace</Description>
|
||||||
|
|||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public sealed class DefaultOptionsManager : IOptionsManager
|
|
||||||
{
|
|
||||||
public T GetOptions<T>() where T : class
|
|
||||||
{
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateOptions<T>(T value) where T : class
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
using Microsoft.Extensions.Options;
|
|
||||||
|
|
||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public interface ILiveOptions<T> : IOptions<T>
|
|
||||||
where T : class
|
|
||||||
{
|
|
||||||
}
|
|
||||||
-6
@@ -1,6 +0,0 @@
|
|||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public interface ILiveUpdateableOptions<T> : ILiveOptions<T>, IUpdateableOptions<T>
|
|
||||||
where T : class
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public interface IOptionsManager
|
|
||||||
{
|
|
||||||
T GetOptions<T>()
|
|
||||||
where T : class;
|
|
||||||
void UpdateOptions<T>(T value)
|
|
||||||
where T : class;
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
using Microsoft.Extensions.Options;
|
|
||||||
|
|
||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public interface IUpdateableOptions<out T> : IOptions<T>
|
|
||||||
where T : class
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Updates the configuration with the current value stored in <see cref="IOptions{TOptions}.Value"/>.
|
|
||||||
/// </summary>
|
|
||||||
void UpdateOption();
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
using Slim.Resolvers;
|
|
||||||
|
|
||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public sealed class LiveOptionsResolver : IDependencyResolver
|
|
||||||
{
|
|
||||||
private static readonly Type optionsType = typeof(LiveUpdateableOptionsWrapper<>);
|
|
||||||
|
|
||||||
public bool CanResolve(Type type)
|
|
||||||
{
|
|
||||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ILiveOptions<>))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Resolve(Slim.IServiceProvider serviceProvider, Type type)
|
|
||||||
{
|
|
||||||
var typedOptionsType = optionsType.MakeGenericType(type.GetGenericArguments());
|
|
||||||
var configurationManager = serviceProvider.GetService<IOptionsManager>();
|
|
||||||
|
|
||||||
return Activator.CreateInstance(typedOptionsType, configurationManager);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-25
@@ -1,25 +0,0 @@
|
|||||||
using Slim.Resolvers;
|
|
||||||
|
|
||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public sealed class LiveUpdateableOptionsResolver : IDependencyResolver
|
|
||||||
{
|
|
||||||
private static readonly Type optionsType = typeof(LiveUpdateableOptionsWrapper<>);
|
|
||||||
|
|
||||||
public bool CanResolve(Type type)
|
|
||||||
{
|
|
||||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ILiveUpdateableOptions<>))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Resolve(Slim.IServiceProvider serviceProvider, Type type)
|
|
||||||
{
|
|
||||||
var typedOptionsType = optionsType.MakeGenericType(type.GetGenericArguments());
|
|
||||||
var configurationManager = serviceProvider.GetService<IOptionsManager>();
|
|
||||||
return Activator.CreateInstance(typedOptionsType, configurationManager);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-30
@@ -1,30 +0,0 @@
|
|||||||
using System.Extensions;
|
|
||||||
|
|
||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public sealed class LiveUpdateableOptionsWrapper<T> : ILiveUpdateableOptions<T>
|
|
||||||
where T : class
|
|
||||||
{
|
|
||||||
private readonly IOptionsManager configurationManager;
|
|
||||||
|
|
||||||
private T value;
|
|
||||||
|
|
||||||
public T Value
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
this.value = this.configurationManager.GetOptions<T>();
|
|
||||||
return this.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public LiveUpdateableOptionsWrapper(IOptionsManager configurationManager)
|
|
||||||
{
|
|
||||||
this.configurationManager = configurationManager.ThrowIfNull(nameof(configurationManager));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateOption()
|
|
||||||
{
|
|
||||||
this.configurationManager.UpdateOptions<T>(this.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Slim.Resolvers;
|
|
||||||
|
|
||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public sealed class OptionsResolver : IDependencyResolver
|
|
||||||
{
|
|
||||||
private static readonly Type optionsType = typeof(OptionsWrapper<>);
|
|
||||||
private static readonly Type configurationType = typeof(IOptionsManager);
|
|
||||||
|
|
||||||
public bool CanResolve(Type type)
|
|
||||||
{
|
|
||||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IOptions<>))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Resolve(Slim.IServiceProvider serviceProvider, Type type)
|
|
||||||
{
|
|
||||||
var typedOptionsType = optionsType.MakeGenericType(type.GetGenericArguments());
|
|
||||||
var configurationManager = serviceProvider.GetService<IOptionsManager>();
|
|
||||||
var optionsValue = configurationType
|
|
||||||
.GetMethod(nameof(IOptionsManager.GetOptions))
|
|
||||||
.MakeGenericMethod(type.GetGenericArguments())
|
|
||||||
.Invoke(configurationManager, Array.Empty<object>());
|
|
||||||
|
|
||||||
return Activator.CreateInstance(typedOptionsType, optionsValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-32
@@ -1,32 +0,0 @@
|
|||||||
using Slim.Resolvers;
|
|
||||||
using System.Extensions.Configuration;
|
|
||||||
|
|
||||||
namespace System.Configuration;
|
|
||||||
|
|
||||||
public sealed class UpdateableOptionsResolver : IDependencyResolver
|
|
||||||
{
|
|
||||||
private static readonly Type optionsType = typeof(UpdateableOptionsWrapper<>);
|
|
||||||
private static readonly Type configurationType = typeof(IOptionsManager);
|
|
||||||
|
|
||||||
public bool CanResolve(Type type)
|
|
||||||
{
|
|
||||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IUpdateableOptions<>))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Resolve(Slim.IServiceProvider serviceProvider, Type type)
|
|
||||||
{
|
|
||||||
var typedOptionsType = optionsType.MakeGenericType(type.GetGenericArguments());
|
|
||||||
var configurationManager = serviceProvider.GetService<IOptionsManager>();
|
|
||||||
var optionsValue = configurationType
|
|
||||||
.GetMethod(nameof(IOptionsManager.GetOptions))
|
|
||||||
.MakeGenericMethod(type.GetGenericArguments())
|
|
||||||
.Invoke(configurationManager, Array.Empty<object>());
|
|
||||||
|
|
||||||
return Activator.CreateInstance(typedOptionsType, configurationManager, optionsValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-22
@@ -1,22 +0,0 @@
|
|||||||
using System.Configuration;
|
|
||||||
|
|
||||||
namespace System.Extensions.Configuration;
|
|
||||||
|
|
||||||
public sealed class UpdateableOptionsWrapper<T> : IUpdateableOptions<T>
|
|
||||||
where T : class
|
|
||||||
{
|
|
||||||
private readonly IOptionsManager configurationManager;
|
|
||||||
|
|
||||||
public T Value { get; }
|
|
||||||
|
|
||||||
public UpdateableOptionsWrapper(IOptionsManager configurationManager, T options)
|
|
||||||
{
|
|
||||||
this.configurationManager = configurationManager.ThrowIfNull(nameof(configurationManager));
|
|
||||||
this.Value = options;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateOption()
|
|
||||||
{
|
|
||||||
this.configurationManager.UpdateOptions(this.Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-133
@@ -1,132 +1,12 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Slim;
|
using Slim;
|
||||||
using System.Configuration;
|
|
||||||
using System.Logging;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
|
||||||
namespace System.Extensions;
|
namespace System.Extensions;
|
||||||
|
|
||||||
public static class ServiceManagerExtensions
|
public static class ServiceManagerExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Registers a <see cref="IOptionsManager"/> with the default <see cref="DefaultOptionsManager"/>.
|
|
||||||
/// This call also registers the resolver that resolves <see cref="IUpdateableOptions{T}"/> and <see cref="IOptions{T}"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="serviceManager"><see cref="IServiceManager"/>.</param>
|
|
||||||
/// <returns>Provided <see cref="IServiceManager"/>.</returns>
|
|
||||||
public static IServiceProducer RegisterOptionsManager(this IServiceProducer serviceProducer)
|
|
||||||
{
|
|
||||||
serviceProducer.ThrowIfNull(nameof(serviceProducer));
|
|
||||||
|
|
||||||
serviceProducer.RegisterSingleton<IOptionsManager, DefaultOptionsManager>();
|
|
||||||
serviceProducer.RegisterOptionsResolver();
|
|
||||||
|
|
||||||
return serviceProducer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Registers a <see cref="IOptionsManager"/>.
|
|
||||||
/// This call also registers the resolver that resolves <see cref="IUpdateableOptions{T}"/> and <see cref="IOptions{T}"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">Implementation of <see cref="IOptionsManager"/>.</typeparam>
|
|
||||||
/// <param name="serviceManager"><see cref="IServiceManager"/>.</param>
|
|
||||||
/// <returns>Provided <see cref="IServiceManager"/>.</returns>
|
|
||||||
public static IServiceProducer RegisterOptionsManager<T>(this IServiceProducer serviceProducer)
|
|
||||||
where T : IOptionsManager
|
|
||||||
{
|
|
||||||
serviceProducer.ThrowIfNull(nameof(serviceProducer));
|
|
||||||
|
|
||||||
serviceProducer.RegisterSingleton<IOptionsManager, T>();
|
|
||||||
serviceProducer.RegisterOptionsResolver();
|
|
||||||
|
|
||||||
return serviceProducer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Registers resolvers for <see cref="IOptions{TOptions}"/> and <see cref="IUpdateableOptions{T}"/>.
|
|
||||||
/// Depends on a <see cref="IOptionsManager"/> in order to properly resolve options.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="serviceManager"><see cref="IServiceManager"/>.</param>
|
|
||||||
/// <returns><see cref="IServiceManager"/>.</returns>
|
|
||||||
public static IServiceProducer RegisterOptionsResolver(this IServiceProducer serviceProducer)
|
|
||||||
{
|
|
||||||
serviceProducer.ThrowIfNull(nameof(serviceProducer));
|
|
||||||
|
|
||||||
serviceProducer.RegisterResolver(new OptionsResolver());
|
|
||||||
serviceProducer.RegisterResolver(new UpdateableOptionsResolver());
|
|
||||||
serviceProducer.RegisterResolver(new LiveOptionsResolver());
|
|
||||||
serviceProducer.RegisterResolver(new LiveUpdateableOptionsResolver());
|
|
||||||
|
|
||||||
return serviceProducer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Registers a <see cref="ILogsWriter"/> with the default <see cref="CVLoggerProvider"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TLogsWriter">Implementation of <see cref="ILogsWriter"/>.</typeparam>
|
|
||||||
/// <param name="serviceManager"><see cref="IServiceProducer"/>.</param>
|
|
||||||
/// <returns>Provided <see cref="IServiceProducer"/>.</returns>
|
|
||||||
public static IServiceProducer RegisterLogWriter<TLogsWriter>(this IServiceProducer serviceManager)
|
|
||||||
where TLogsWriter : ILogsWriter
|
|
||||||
{
|
|
||||||
serviceManager.ThrowIfNull(nameof(serviceManager));
|
|
||||||
|
|
||||||
serviceManager.RegisterSingleton<ILogsWriter, TLogsWriter>();
|
|
||||||
serviceManager.RegisterScoped<ILoggerFactory, LoggerFactory>(sp =>
|
|
||||||
{
|
|
||||||
var factory = new LoggerFactory();
|
|
||||||
factory.AddProvider(new CVLoggerProvider(sp.GetService<ILogsWriter>()));
|
|
||||||
return factory;
|
|
||||||
});
|
|
||||||
|
|
||||||
return serviceManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Registers a <see cref="ILogsWriter"/> with the default <see cref="CVLoggerProvider"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TILogsWriter">Interface of <see cref="ILogsWriter"/>.</typeparam>
|
|
||||||
/// <typeparam name="TLogsWriter">Implementation of <see cref="ILogsWriter"/>.</typeparam>
|
|
||||||
/// <param name="serviceManager"><see cref="IServiceProducer"/>.</param>
|
|
||||||
/// <returns>Provided <see cref="IServiceProducer"/>.</returns>
|
|
||||||
public static IServiceProducer RegisterLogWriter<TILogsWriter, TLogsWriter>(this IServiceProducer serviceManager)
|
|
||||||
where TLogsWriter : TILogsWriter
|
|
||||||
where TILogsWriter : class, ILogsWriter
|
|
||||||
{
|
|
||||||
serviceManager.ThrowIfNull(nameof(serviceManager));
|
|
||||||
|
|
||||||
serviceManager.RegisterSingleton<TILogsWriter, TLogsWriter>();
|
|
||||||
serviceManager.RegisterScoped<ILoggerFactory, LoggerFactory>(sp =>
|
|
||||||
{
|
|
||||||
var factory = new LoggerFactory();
|
|
||||||
factory.AddProvider(new CVLoggerProvider(sp.GetService<TILogsWriter>()));
|
|
||||||
return factory;
|
|
||||||
});
|
|
||||||
|
|
||||||
return serviceManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Register a <see cref="ILoggerFactory"/> with a <see cref="CVLoggerProvider"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="serviceProducer"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IServiceProducer RegisterCVLoggerFactory(this IServiceProducer serviceProducer)
|
|
||||||
{
|
|
||||||
serviceProducer.ThrowIfNull(nameof(serviceProducer));
|
|
||||||
|
|
||||||
serviceProducer.RegisterScoped<ILoggerFactory, LoggerFactory>(sp =>
|
|
||||||
{
|
|
||||||
LoggerFactory loggerFactory = new();
|
|
||||||
loggerFactory.AddProvider(new CVLoggerProvider(sp.GetService<ILogsWriter>()));
|
|
||||||
return loggerFactory;
|
|
||||||
});
|
|
||||||
|
|
||||||
return serviceProducer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers a <see cref="ILoggerFactory"/>.
|
/// Registers a <see cref="ILoggerFactory"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -141,19 +21,6 @@ public static class ServiceManagerExtensions
|
|||||||
return serviceProducer;
|
return serviceProducer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Registers a <see cref="ILoggerFactory"/> with a <see cref="ILogsWriter"/> that writes to <see cref="Diagnostics.Debug"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="serviceProducer"><see cref="IServiceProducer"/>.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IServiceProducer RegisterDebugLoggerFactory(this IServiceProducer serviceProducer)
|
|
||||||
{
|
|
||||||
serviceProducer.ThrowIfNull(nameof(serviceProducer));
|
|
||||||
|
|
||||||
serviceProducer.RegisterLogWriter<ILogsWriter, DebugLogsWriter>();
|
|
||||||
return serviceProducer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Register a scoped <see cref="IHttpClient{TScope}"/> to be used by the DI engine.
|
/// Register a scoped <see cref="IHttpClient{TScope}"/> to be used by the DI engine.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using System.Extensions;
|
|
||||||
|
|
||||||
namespace System.Logging;
|
|
||||||
|
|
||||||
public sealed class CVLogger : ILogger
|
|
||||||
{
|
|
||||||
private readonly string category;
|
|
||||||
private readonly ICVLoggerProvider cvLoggerProvider;
|
|
||||||
|
|
||||||
public CVLogger(string category, ICVLoggerProvider cvLoggerProvider)
|
|
||||||
{
|
|
||||||
this.category = category;
|
|
||||||
this.cvLoggerProvider = cvLoggerProvider.ThrowIfNull(nameof(cvLoggerProvider));
|
|
||||||
}
|
|
||||||
|
|
||||||
public IDisposable BeginScope<TState>(TState state)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsEnabled(LogLevel logLevel)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
|
||||||
{
|
|
||||||
var message = formatter(state, exception);
|
|
||||||
|
|
||||||
var log = new Log
|
|
||||||
{
|
|
||||||
Exception = exception,
|
|
||||||
LogLevel = logLevel,
|
|
||||||
EventId = eventId.Name,
|
|
||||||
Message = message,
|
|
||||||
Category = category,
|
|
||||||
LogTime = DateTime.Now
|
|
||||||
};
|
|
||||||
|
|
||||||
this.cvLoggerProvider.LogEntry(log);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
using Microsoft.CorrelationVector;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using System.Extensions;
|
|
||||||
|
|
||||||
namespace System.Logging;
|
|
||||||
|
|
||||||
public sealed class CVLoggerProvider : ICVLoggerProvider
|
|
||||||
{
|
|
||||||
private readonly ILogsWriter logsManager;
|
|
||||||
private readonly CorrelationVector correlationVector;
|
|
||||||
|
|
||||||
public CVLoggerProvider(ILogsWriter logsWriter)
|
|
||||||
{
|
|
||||||
this.logsManager = logsWriter.ThrowIfNull(nameof(logsWriter));
|
|
||||||
this.correlationVector = new CorrelationVector();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void LogEntry(Log log)
|
|
||||||
{
|
|
||||||
if (this.correlationVector is not null)
|
|
||||||
{
|
|
||||||
log.CorrelationVector = this.correlationVector.Value.ToString();
|
|
||||||
this.correlationVector.Increment();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logsManager.WriteLog(log);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ILogger CreateLogger(string categoryName)
|
|
||||||
{
|
|
||||||
return new CVLogger(categoryName, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
throw new System.NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
using System.Diagnostics;
|
|
||||||
|
|
||||||
namespace System.Logging;
|
|
||||||
|
|
||||||
public sealed class DebugLogsWriter : ILogsWriter
|
|
||||||
{
|
|
||||||
public void WriteLog(Log log)
|
|
||||||
{
|
|
||||||
Debug.WriteLine($"{log.LogTime} - {log.Category} - {log.EventId} - {log.CorrelationVector} - {log.LogLevel} - {log.Message}{Environment.NewLine}{log.Exception}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace System.Logging;
|
|
||||||
|
|
||||||
public interface ICVLoggerProvider : ILoggerProvider
|
|
||||||
{
|
|
||||||
void LogEntry(Log log);
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
namespace System.Logging;
|
|
||||||
|
|
||||||
public interface ILogsWriter
|
|
||||||
{
|
|
||||||
void WriteLog(Log log);
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Slim.Resolvers;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace System.Logging;
|
|
||||||
|
|
||||||
public sealed class LoggerResolver : IDependencyResolver
|
|
||||||
{
|
|
||||||
public bool CanResolve(Type type)
|
|
||||||
{
|
|
||||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ILogger<>) ||
|
|
||||||
type == typeof(ILogger))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Resolve(Slim.IServiceProvider serviceProvider, Type type)
|
|
||||||
{
|
|
||||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ILogger<>))
|
|
||||||
{
|
|
||||||
return ResolveScopedLogger(serviceProvider, type);
|
|
||||||
}
|
|
||||||
else if (type == typeof(ILogger))
|
|
||||||
{
|
|
||||||
return ResolveLogger(serviceProvider);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"{nameof(LoggerResolver)} cannot resolve an object of type {type.Name}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static object ResolveScopedLogger(Slim.IServiceProvider serviceProvider, Type type)
|
|
||||||
{
|
|
||||||
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
|
|
||||||
var categoryTypes = type.GetGenericArguments();
|
|
||||||
var createLoggerMethod = typeof(LoggerFactoryExtensions).GetMethods().Where(m => m.IsGenericMethodDefinition && m.Name == nameof(LoggerFactoryExtensions.CreateLogger)).First();
|
|
||||||
return createLoggerMethod.MakeGenericMethod(categoryTypes.First()).Invoke(null, new object[] { loggerFactory });
|
|
||||||
}
|
|
||||||
|
|
||||||
private static object ResolveLogger(Slim.IServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
|
|
||||||
return loggerFactory.CreateLogger(string.Empty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
<Version>1.6.9</Version>
|
<Version>1.7</Version>
|
||||||
<Authors>Alexandru Macocian</Authors>
|
<Authors>Alexandru Macocian</Authors>
|
||||||
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
||||||
<Description>Extensions for the Slim Dependency Injection engine</Description>
|
<Description>Extensions for the Slim Dependency Injection engine</Description>
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||||
<RootNamespace>System</RootNamespace>
|
<RootNamespace>System</RootNamespace>
|
||||||
<Version>1.6.12</Version>
|
<Version>1.7</Version>
|
||||||
<Authors>Alexandru Macocian</Authors>
|
<Authors>Alexandru Macocian</Authors>
|
||||||
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
|
||||||
<Description>Extensions for the System namespace</Description>
|
<Description>Extensions for the System namespace</Description>
|
||||||
|
|||||||
@@ -124,36 +124,4 @@ public class AVLTreeTests
|
|||||||
|
|
||||||
_ = this.avlTree.ToArray();
|
_ = this.avlTree.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod()]
|
|
||||||
public void Serialize()
|
|
||||||
{
|
|
||||||
var avlTree2 = new AVLTree<int>();
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
this.avlTree.Add(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
var serializer = new BinaryFormatter();
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
serializer.Serialize(stream, this.avlTree);
|
|
||||||
stream.Position = 0;
|
|
||||||
avlTree2 = (AVLTree<int>)serializer.Deserialize(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
var avlTreeEnum = this.avlTree.GetEnumerator();
|
|
||||||
var avlTree2Enum = avlTree2.GetEnumerator();
|
|
||||||
|
|
||||||
for(var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
if(avlTreeEnum.Current != avlTree2Enum.Current)
|
|
||||||
{
|
|
||||||
Assert.Fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
avlTreeEnum.MoveNext();
|
|
||||||
avlTree2Enum.MoveNext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -179,35 +179,4 @@ public class BinaryHeapTests
|
|||||||
|
|
||||||
Assert.Fail();
|
Assert.Fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod()]
|
|
||||||
public void Serialize()
|
|
||||||
{
|
|
||||||
var binaryHeap2 = new BinaryHeap<int>();
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
this.binaryHeap.Add(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
var serializer = new BinaryFormatter();
|
|
||||||
using (var stream = new MemoryStream()) {
|
|
||||||
serializer.Serialize(stream, this.binaryHeap);
|
|
||||||
stream.Position = 0;
|
|
||||||
binaryHeap2 = (BinaryHeap<int>)serializer.Deserialize(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
var binaryHeapEnum = this.binaryHeap.GetEnumerator();
|
|
||||||
var binaryHeapEnum2 = binaryHeap2.GetEnumerator();
|
|
||||||
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
if (binaryHeapEnum.Current != binaryHeapEnum2.Current)
|
|
||||||
{
|
|
||||||
Assert.Fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
binaryHeapEnum.MoveNext();
|
|
||||||
binaryHeapEnum2.MoveNext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -177,36 +177,4 @@ public class FibonacciHeapTests
|
|||||||
|
|
||||||
Assert.Fail();
|
Assert.Fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod()]
|
|
||||||
public void Serialize()
|
|
||||||
{
|
|
||||||
var fibonacciHeap2 = new FibonacciHeap<int>();
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
this.fibonacciHeap.Add(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
var serializer = new BinaryFormatter();
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
serializer.Serialize(stream, this.fibonacciHeap);
|
|
||||||
stream.Position = 0;
|
|
||||||
fibonacciHeap2 = (FibonacciHeap<int>)serializer.Deserialize(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
var enum1 = this.fibonacciHeap.GetEnumerator();
|
|
||||||
var enum2 = fibonacciHeap2.GetEnumerator();
|
|
||||||
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
if (enum1.Current != enum2.Current)
|
|
||||||
{
|
|
||||||
Assert.Fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
enum1.MoveNext();
|
|
||||||
enum2.MoveNext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -81,36 +81,4 @@ public class PriorityQueueTests
|
|||||||
Assert.Fail();
|
Assert.Fail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod()]
|
|
||||||
public void Serialize()
|
|
||||||
{
|
|
||||||
var priorityQueue2 = new PriorityQueue<int>();
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
this.priorityQueue.Enqueue(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
var serializer = new BinaryFormatter();
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
serializer.Serialize(stream, this.priorityQueue);
|
|
||||||
stream.Position = 0;
|
|
||||||
priorityQueue2 = (PriorityQueue<int>)serializer.Deserialize(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
var enum1 = this.priorityQueue.GetEnumerator();
|
|
||||||
var enum2 = priorityQueue2.GetEnumerator();
|
|
||||||
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
if (enum1.Current != enum2.Current)
|
|
||||||
{
|
|
||||||
Assert.Fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
enum1.MoveNext();
|
|
||||||
enum2.MoveNext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -125,37 +125,4 @@ public class SkipListTests
|
|||||||
Assert.Fail();
|
Assert.Fail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod()]
|
|
||||||
[Ignore("Binary serialization is obsolete and should not be used anymore")]
|
|
||||||
public void Serialize()
|
|
||||||
{
|
|
||||||
var skipList2 = new SkipList<int>();
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
this.skipList.Add(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
var serializer = new BinaryFormatter();
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
serializer.Serialize(stream, this.skipList);
|
|
||||||
stream.Position = 0;
|
|
||||||
skipList2 = (SkipList<int>)serializer.Deserialize(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
var enum1 = this.skipList.GetEnumerator();
|
|
||||||
var enum2 = skipList2.GetEnumerator();
|
|
||||||
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
if (enum1.Current != enum2.Current)
|
|
||||||
{
|
|
||||||
Assert.Fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
enum1.MoveNext();
|
|
||||||
enum2.MoveNext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -109,37 +109,4 @@ public class TreapTests
|
|||||||
|
|
||||||
Assert.Fail();
|
Assert.Fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod()]
|
|
||||||
[Ignore("Binary serialization is obsolete and should not be used anymore")]
|
|
||||||
public void Serialize()
|
|
||||||
{
|
|
||||||
var treap2 = new Treap<int>();
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
this.treap.Add(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
var serializer = new BinaryFormatter();
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
serializer.Serialize(stream, this.treap);
|
|
||||||
stream.Position = 0;
|
|
||||||
treap2 = (Treap<int>)serializer.Deserialize(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
var enum1 = this.treap.GetEnumerator();
|
|
||||||
var enum2 = treap2.GetEnumerator();
|
|
||||||
|
|
||||||
for (var i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
if (enum1.Current != enum2.Current)
|
|
||||||
{
|
|
||||||
Assert.Fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
enum1.MoveNext();
|
|
||||||
enum2.MoveNext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user