Repository navigation
Description
When only using AddSimpleConsole(), it shouldn't pull in the other inbox console logging formatters. This leads to unnecessary app size bloat when publishing trimmed or AOT.
Repro Steps
dotnet publish --sc the following app:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0-preview.4.23214.1" />
</ItemGroup>
</Project>using Microsoft.Extensions.Logging;
ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole());
ILogger logger = loggerFactory.CreateLogger<object>();
logger.LogInformation("Hi");
Console.ReadLine();Inspect the publish folder. Load all the .dlls in ILSpy.
Expected results
System.Text.Json.dllshouldn't be in the output, as the app doesn't use JSON.- In ILSpy, the
Microsoft.Extensions.Logging.Console.JsonConsoleFormatterandMicrosoft.Extensions.Logging.Console.SystemdConsoleFormatterclasses should be trimmed.
Actual results
Notes
- I think these are being rooted by:
runtime/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs
Lines 101 to 106 in bd37436
| if (!added) | |
| { | |
| cd.TryAdd(ConsoleFormatterNames.Simple, new SimpleConsoleFormatter(new FormatterOptionsMonitor<SimpleConsoleFormatterOptions>(new SimpleConsoleFormatterOptions()))); | |
| cd.TryAdd(ConsoleFormatterNames.Systemd, new SystemdConsoleFormatter(new FormatterOptionsMonitor<ConsoleFormatterOptions>(new ConsoleFormatterOptions()))); | |
| cd.TryAdd(ConsoleFormatterNames.Json, new JsonConsoleFormatter(new FormatterOptionsMonitor<JsonConsoleFormatterOptions>(new JsonConsoleFormatterOptions()))); | |
| } |
-
From a local prototype, I've measured around 120KB+ (of the ~8MB) in a BasicMinimalApi app coming from these unnecessary formatters being left in the app.
-
I believe this would be a breaking change, since previously if an app just does
AddSimpleConsole(), but the configures the Json formatter to be used through appsettings.json (or other), the Json formatter would be used. The simple fix for that would be to just callAddConsole()instead and let the configuration win.
See discussion in dotnet/aspnetcore#47797 (comment).

Activity
layomia commentedon Aug 3, 2023
Triage: we can address this in vNext cc @eerhardt @tarekgh.