Skip to content

Commit 755eb19

Browse files
committed
feat: ILoggingBuilder.AddSerilog() extension method
Context: #3008 Context: #3011 Context: unoplatform/uno.templates#1921 Issue #3008 details that there are two `ILogger` "environments" in a Uno app: * The `App.InitializeLogging()` environment ("NoDI"), and * The environment configured by Dependency Injection methods such as `.UseLogging()` and `.UseSerilog()` ("DI"). The problem is that "DI" logging *cannot* capture log messages produced before the DI environment is built, frequently via `builder.Build()` or `builder.NavigateAsync<TShell>()`. *A lot* of code can execute before those methods complete. A further problem is that no default template actually configures both environments: if you use `dotnet new unoapp -di False` you get the NoDI `App.InitializeLogging()` environment, which *will not* log messages from the Dependency Injection enviornment. If you use `dotnet new unoapp -preset "recommended"` you get *only* the DI-based logging, thus missing all messages from the NoDI environment. Thus, the idea for "`ILogger` configuration unification": What If instead of having two separate places for logging, we just had one: the NoDI `App.InitializeLogging()` location. This can be made to work via #3011, which updates `HostBuilder.Build()` so that *if* `LogExtensionPoint.AmbientLoggerFactory` is set, it *replaces* the `ILoggerFactory` that `HostBuilder.Build()` normally configures. This allows `App.InitializeLogging()` to also be used in the DI environment. So far, so useful. But what about [Serilog][0]? Serilog is a widely recommended logger library, and the current `.UseSerilog()` extension method *requires* a full Dependency Injection environment: it cannot be invoked within `App.InitializeLogging()`. Try to square this circle by adding a new `ILoggingBuilder.AddSerilog()` extension method. This would allow Serilog to be configured within `App.InitializeLogging()`, helping to support a unified logging setup experience. [0]: https://platform.uno/docs/articles/external/uno.extensions/doc/Overview/Logging/LoggingOverview.html#serilog
1 parent 5235975 commit 755eb19

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

src/Uno.Extensions.Core.UI/Uno.Extensions.Core.WinUI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<ItemGroup>
2626
<InternalsVisibleTo Include="Uno.Extensions.Hosting.UWP" />
2727
<InternalsVisibleTo Include="Uno.Extensions.Hosting.WinUI" />
28+
<InternalsVisibleTo Include="Uno.Extensions.Logging.Serilog" />
2829

2930
<PackageReference Include="Uno.WinUI" />
3031
</ItemGroup>

src/Uno.Extensions.Logging.Serilog/HostBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static IHostBuilder UseSerilog(this IHostBuilder hostBuilder,
9090
});
9191
}
9292

93-
private static LoggerConfiguration AddConsoleLogging(LoggerConfiguration configuration)
93+
internal static LoggerConfiguration AddConsoleLogging(LoggerConfiguration configuration)
9494
{
9595
#pragma warning disable CA1416 // Validate platform compatibility: The net8.0 version is not used on older versions of OS
9696
return configuration
@@ -107,7 +107,7 @@ private static LoggerConfiguration AddConsoleLogging(LoggerConfiguration configu
107107
#pragma warning restore CA1416 // Validate platform compatibility
108108
}
109109

110-
private static LoggerConfiguration AddFileLogging(LoggerConfiguration configuration, string logFilePath)
110+
internal static LoggerConfiguration AddFileLogging(LoggerConfiguration configuration, string logFilePath)
111111
{
112112
//-:cnd:noEmit
113113
#if __ANDROID__ || __IOS__ || NETSTANDARD
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.IO;
3+
4+
using Microsoft.Extensions.Logging;
5+
6+
using Serilog;
7+
8+
namespace Uno.Extensions;
9+
10+
/// <summary>
11+
/// Extension methods to adjust the scope of collected logs. (log level)
12+
/// </summary>
13+
public static class LoggingBuilderExtensions
14+
{
15+
public static ILoggingBuilder AddSerilog(
16+
this ILoggingBuilder builder,
17+
bool consoleLoggingEnabled = false,
18+
bool fileLoggingEnabled = false,
19+
Action<LoggerConfiguration>? configureLogger = null)
20+
{
21+
#pragma warning disable CS0436
22+
var loggerConfiguration = new LoggerConfiguration();
23+
// loggerConfiguration.ReadFrom.Configuration(context.Configuration);
24+
if (consoleLoggingEnabled)
25+
{
26+
HostBuilderExtensions.AddConsoleLogging(loggerConfiguration);
27+
}
28+
if (fileLoggingEnabled)
29+
{
30+
var logPath = GetLogFilePath();
31+
if (logPath is not null)
32+
{
33+
HostBuilderExtensions.AddFileLogging(loggerConfiguration, logPath);
34+
}
35+
}
36+
#pragma warning restore CS0436
37+
configureLogger?.Invoke(loggerConfiguration);
38+
var logger = loggerConfiguration.CreateLogger();
39+
40+
builder.AddSerilog(logger);
41+
return builder;
42+
}
43+
44+
private static string? GetLogFilePath()
45+
{
46+
var logDirectory = ApplicationDataExtensions.DataFolder();
47+
48+
if (string.IsNullOrWhiteSpace(logDirectory))
49+
{
50+
return null;
51+
}
52+
53+
var assemblyName = PlatformHelper.GetAppAssembly()?.FullName ?? "unologging";
54+
return Path.Combine(logDirectory, $"{assemblyName}.log");
55+
}
56+
}

src/Uno.Extensions.Logging.Serilog/Uno.Extensions.Logging.Serilog.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</ItemGroup>
2626

2727
<ItemGroup>
28+
<ProjectReference Include="..\Uno.Extensions.Core.UI\Uno.Extensions.Core.WinUI.csproj" />
2829
<ProjectReference Include="..\Uno.Extensions.Hosting\Uno.Extensions.Hosting.csproj" />
2930
</ItemGroup>
3031

0 commit comments

Comments
 (0)