Skip to content

Logging AddSimpleConsole should allow for trimming Json and Systemd formatters #85116

Open
@eerhardt

Description

@eerhardt

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

  1. System.Text.Json.dll shouldn't be in the output, as the app doesn't use JSON.
  2. In ILSpy, the Microsoft.Extensions.Logging.Console.JsonConsoleFormatter and Microsoft.Extensions.Logging.Console.SystemdConsoleFormatter classes should be trimmed.

Actual results

  1. System.Text.Json.dll is in the publish output folder
  2. These classes are still left in the app:
    image

Notes

  1. I think these are being rooted by:

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())));
}

  1. 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.

  2. 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 call AddConsole() instead and let the configuration win.

See discussion in dotnet/aspnetcore#47797 (comment).

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Loggingbreaking-changeIssue or PR that represents a breaking API or functional change over a prerelease.size-reductionIssues impacting final app size primary for size sensitive workloads

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions