diff --git a/src/Uno.Extensions.Hosting.UI.Tests/HostBuilderTests.cs b/src/Uno.Extensions.Hosting.UI.Tests/HostBuilderTests.cs new file mode 100644 index 0000000000..e3c31b0890 --- /dev/null +++ b/src/Uno.Extensions.Hosting.UI.Tests/HostBuilderTests.cs @@ -0,0 +1,34 @@ +using System; +using System.IO; +using System.Text.Json; +using FluentAssertions; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Uno.Extensions.Hosting.Tests; + +[TestClass] +public class HostBuilderTests +{ + [TestMethod] + public void After_Build_ILoggerFactory_Is_AmbientLogger() + { + var originalAmbientLoggerFactory = Uno.Extensions.LogExtensionPoint.AmbientLoggerFactory; + try + { + var myLoggerFactory = new LoggerFactory(); + Uno.Extensions.LogExtensionPoint.AmbientLoggerFactory = myLoggerFactory; + var hostBuilder = new HostBuilder(); + var host = hostBuilder.Build(); + var serviceLoggerFactory = host.Services.GetService(); + serviceLoggerFactory.Should().NotBeNull(); + serviceLoggerFactory.Should().Be(myLoggerFactory); + } + finally + { + Uno.Extensions.LogExtensionPoint.AmbientLoggerFactory = originalAmbientLoggerFactory; + } + } +} diff --git a/src/Uno.Extensions.Hosting.UI.Tests/Uno.Extensions.Hosting.WinUI.Tests.csproj b/src/Uno.Extensions.Hosting.UI.Tests/Uno.Extensions.Hosting.WinUI.Tests.csproj new file mode 100644 index 0000000000..a3b48faf85 --- /dev/null +++ b/src/Uno.Extensions.Hosting.UI.Tests/Uno.Extensions.Hosting.WinUI.Tests.csproj @@ -0,0 +1,21 @@ + + + + net9.0 + false + disable + + + + + + + + + + + + + + + diff --git a/src/Uno.Extensions.Hosting.UI/HostBuilder.cs b/src/Uno.Extensions.Hosting.UI/HostBuilder.cs index 52ec2d9973..2476a2bb47 100644 --- a/src/Uno.Extensions.Hosting.UI/HostBuilder.cs +++ b/src/Uno.Extensions.Hosting.UI/HostBuilder.cs @@ -1,6 +1,8 @@ // Marking as auto-generated to prevent StyleCop including this file - Taken from Microsoft.Extensions.Hosting // +using Microsoft.Extensions.DependencyInjection.Extensions; + namespace Uno.Extensions.Hosting; /// @@ -241,6 +243,12 @@ private void CreateServiceProvider() //services.AddOptions().Configure(options => { options.Initialize(_hostConfiguration); }); services.AddLogging(); + var ambientFactory = Uno.Extensions.LogExtensionPoint.AmbientLoggerFactory; + if (ambientFactory is not null) + { + services.Replace(ServiceDescriptor.Singleton(typeof(ILoggerFactory), ambientFactory)); + } + foreach (Action configureServicesAction in _configureServicesActions) { configureServicesAction(_hostBuilderContext!, services); diff --git a/src/Uno.Extensions.Hosting.UI/Uno.Extensions.Hosting.WinUI.csproj b/src/Uno.Extensions.Hosting.UI/Uno.Extensions.Hosting.WinUI.csproj index 9cf3529717..61f9119a2c 100644 --- a/src/Uno.Extensions.Hosting.UI/Uno.Extensions.Hosting.WinUI.csproj +++ b/src/Uno.Extensions.Hosting.UI/Uno.Extensions.Hosting.WinUI.csproj @@ -18,6 +18,7 @@ + diff --git a/src/Uno.Extensions.Logging/HostBuilderExtensions.cs b/src/Uno.Extensions.Logging/HostBuilderExtensions.cs index d27d331180..54f710b2f8 100644 --- a/src/Uno.Extensions.Logging/HostBuilderExtensions.cs +++ b/src/Uno.Extensions.Logging/HostBuilderExtensions.cs @@ -48,11 +48,17 @@ public static IHostBuilder UseLogging( this IHostBuilder hostBuilder, Action? configure = default, bool enableUnoLogging = false) { + bool ambientLoggerExists = Uno.Extensions.LogExtensionPoint.AmbientLoggerFactory != null; return hostBuilder .ConfigureLogging((context, builder) => { if (!context.IsRegistered(nameof(UseLogging))) { + if (ambientLoggerExists) + { + typeof(HostBuilderExtensions).Log().LogWarning($"Uno.Extensions.LogExtensionPoint.AmbientLoggerFactory already configured; ignoring this .UseLogging() invocation."); + return; + } #if __IOS__ #pragma warning disable CA1416 // Validate platform compatibility: The net8.0 version is not used on older versions of OS builder.AddProvider(new global::Uno.Extensions.Logging.OSLogLoggerProvider());