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 .dll
s in ILSpy.
Expected results
System.Text.Json.dll
shouldn't be in the output, as the app doesn't use JSON.- In ILSpy, the
Microsoft.Extensions.Logging.Console.JsonConsoleFormatter
andMicrosoft.Extensions.Logging.Console.SystemdConsoleFormatter
classes should be trimmed.
Actual results
Notes
- I think these are being rooted by:
-
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).