Skip to content

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

Open

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

Activity

  1. added
    breaking-change
    on Apr 20, 2023
  2. ghost added
    needs-breaking-change-doc-created
    on Apr 20, 2023
  3. ghost added
    untriaged
    on Apr 20, 2023
  4. added
    size-reduction
    on Apr 20, 2023
  5. added this to the 8.0.0 milestone on Apr 20, 2023
  6. ghost removed
    untriaged
    on Apr 20, 2023
  7. layomia commented on Aug 3, 2023

    @layomia
    Contributor

    Triage: we can address this in vNext cc @eerhardt @tarekgh.

  8. modified the milestones: 8.0.0, 9.0.0 on Aug 3, 2023
  9. self-assigned this
    on Aug 3, 2023
  10. modified the milestones: 9.0.0, Future on Jun 20, 2024
  11. removed
    needs-breaking-change-doc-created
    on Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Loggingbreaking-changesize-reduction

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions