Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #78518 ...
eerhardt opened this issue Apr 20, 2023 · 4 comments
Open
Tracked by #78518 ...
Labels
area-Extensions-Logging breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet size-reduction Issues impacting final app size primary for size sensitive workloads
Milestone

Comments

@eerhardt
Copy link
Member

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

@eerhardt eerhardt added breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. area-Extensions-Logging labels Apr 20, 2023
@ghost
Copy link

ghost commented Apr 20, 2023

Tagging subscribers to this area: @dotnet/area-extensions-logging
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: eerhardt
Assignees: -
Labels:

breaking-change, area-Extensions-Logging

Milestone: -

@ghost ghost added the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Apr 20, 2023
@ghost
Copy link

ghost commented Apr 20, 2023

Added needs-breaking-change-doc-created label because this issue has the breaking-change label.

  1. Create and link to this issue a matching issue in the dotnet/docs repo using the breaking change documentation template, then remove this needs-breaking-change-doc-created label.

Tagging @dotnet/compat for awareness of the breaking change.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Apr 20, 2023
@eerhardt eerhardt added the size-reduction Issues impacting final app size primary for size sensitive workloads label Apr 20, 2023
@ghost
Copy link

ghost commented Apr 20, 2023

Tagging subscribers to 'size-reduction': @eerhardt, @SamMonoRT, @marek-safar
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: eerhardt
Assignees: -
Labels:

breaking-change, untriaged, area-Extensions-Logging, needs-breaking-change-doc-created, size-reduction

Milestone: -

@tarekgh tarekgh added this to the 8.0.0 milestone Apr 20, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Apr 20, 2023
@layomia
Copy link
Contributor

layomia commented Aug 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-Logging breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet size-reduction Issues impacting final app size primary for size sensitive workloads
Projects
None yet
Development

No branches or pull requests

3 participants