GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — make log4net usable from a PublishAOT build (#306) by FreeAndNil · Pull Request #306 · apache/logging-log4net

Conversation

@FreeAndNil

@FreeAndNil FreeAndNil commented Aug 5, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Native AOT broke log4net in three ways (#233).

Assembly.GetCallingAssembly() throws PlatformNotSupportedException there, so
every overload that resolves the repository from the caller failed -
LogManager.GetLogger(Type) among them. Guard the 18 call sites with
CallerAssembly.IsSupported, a flag probed once, and fall back to the entry
assembly when the runtime does not implement the call. The call itself has to
stay in the public method whose caller is wanted, so it cannot be moved into
the helper.

SystemInfo.GetAppSetting() then reported a caught failure on every lookup,
because a trimmed System.Configuration cannot initialize. Tell that apart from
a configuration file that does not parse - Native AOT surfaces both as a
ConfigurationErrorsException, so only the inner exception distinguishes them -
and treat a missing configuration system as a property of the runtime rather
than a fault: log it at debug level and let environment variables stand in for
the config file, as they already do on Android. A malformed config file is
still reported as an error and still yields no setting.

Finally the trimmer removed the constructors of everything log4net creates
reflectively, so no repository, pattern converter or locking model could be
instantiated. Annotate that flow with DynamicallyAccessedMembers - polyfilled
here, because the trimmer matches it by name and neither target framework
declares it - and hold the built-in converters in a Dictionary of ConverterInfo
rather than of Type, since a Type placed in a collection loses its annotation.
The registries are now built through a generic method whose new() constraint
states the same requirement structurally, so a converter without a public
parameterless constructor fails to compile instead of failing in a trimmed
build.

Configuration still has to be done in code: XmlConfigurator names its types in
strings and cannot work once they have been trimmed. Document that, and the
fact that loggers from non-entry assemblies land in the entry assembly's
repository, on a new Native AOT page in the manual.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@FreeAndNil FreeAndNil mentioned this pull request Aug 5, 2026
Exception calling GetLogger when compiled using PublishAOT #233
Closed
FreeAndNil added a commit that referenced this pull request Aug 5, 2026
@FreeAndNil
make log4net usable from a PublishAOT build (#306)
9ca8d88
Native AOT broke two things in the startup path (fixes #233 partially).

Assembly.GetCallingAssembly() throws PlatformNotSupportedException there, so
every overload that resolves the repository from the caller failed -
LogManager.GetLogger(Type) among them. Guard the 18 call sites with
CallerAssembly.IsSupported, a flag probed once, and fall back to the entry
assembly when the runtime does not implement the call. The call itself has to
stay in the public method whose caller is wanted, so it cannot be moved into
the helper.

SystemInfo.GetAppSetting() then failed as well, because System.Configuration
is trimmed away. Its catch never saw that: resolving the missing assembly
fails on entry to the method, before the try region, so the exception escaped
the static constructor as a TypeInitializationException and killed the
process. Read the setting through a separate, never inlined method so the
failure is raised inside the try block, latch the result so a permanent
failure is reported once rather than per lookup, and fall back to environment
variables the way the Android branch already does. That fallback also makes
log4net.NullText and log4net.NotAvailableText settable under AOT, where they
previously could not be configured at all.

Note that the fallback applies on .NET Framework too: a malformed app.config
now reads settings from the environment instead of returning null.

This does not make log4net AOT-clean - repositories, appenders and layouts are
still instantiated via Activator.CreateInstance, so an AOT app still fails
with MissingMethodException on Hierarchy's constructor.
@FreeAndNil
FreeAndNil force-pushed the Feature/306-aot-calling-assembly branch from 26841b2 to 9ca8d88 Compare August 5, 2026 21:15
@FreeAndNil
make log4net usable from a PublishAOT build (#306)
bf839f7
Native AOT broke log4net in three ways (#233).

Assembly.GetCallingAssembly() throws PlatformNotSupportedException there, so
every overload that resolves the repository from the caller failed -
LogManager.GetLogger(Type) among them. Guard the 18 call sites with
CallerAssembly.IsSupported, a flag probed once, and fall back to the entry
assembly when the runtime does not implement the call. The call itself has to
stay in the public method whose caller is wanted, so it cannot be moved into
the helper.

SystemInfo.GetAppSetting() then reported a caught failure on every lookup,
because a trimmed System.Configuration cannot initialize. Tell that apart from
a configuration file that does not parse - Native AOT surfaces both as a
ConfigurationErrorsException, so only the inner exception distinguishes them -
and treat a missing configuration system as a property of the runtime rather
than a fault: log it at debug level and let environment variables stand in for
the config file, as they already do on Android. A malformed config file is
still reported as an error and still yields no setting.

Finally the trimmer removed the constructors of everything log4net creates
reflectively, so no repository, pattern converter or locking model could be
instantiated. Annotate that flow with DynamicallyAccessedMembers - polyfilled
here, because the trimmer matches it by name and neither target framework
declares it - and hold the built-in converters in a Dictionary of ConverterInfo
rather than of Type, since a Type placed in a collection loses its annotation.
The registries are now built through a generic method whose new() constraint
states the same requirement structurally, so a converter without a public
parameterless constructor fails to compile instead of failing in a trimmed
build.

Configuration still has to be done in code: XmlConfigurator names its types in
strings and cannot work once they have been trimmed. Document that, and the
fact that loggers from non-entry assemblies land in the entry assembly's
repository, on a new Native AOT page in the manual.
@FreeAndNil
FreeAndNil force-pushed the Feature/306-aot-calling-assembly branch from 9ca8d88 to bf839f7 Compare August 6, 2026 20:35
@FreeAndNil
FreeAndNil marked this pull request as ready for review August 6, 2026 20:39
@FreeAndNil
FreeAndNil requested review from fluffynuts and gdziadkiewicz August 6, 2026 20:39
fluffynuts
fluffynuts reviewed Aug 7, 2026
View reviewed changes

@fluffynuts fluffynuts left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work - just a suggestion to consolidate the logic which determines the caller assembly or falls back on the entry assembly.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/log4net/Config/XmlConfigurator.cs
/// <seealso cref="Log4NetConfigurationSectionHandler"/>
public static ICollection Configure()
=> Configure(LogManager.GetRepository(Assembly.GetCallingAssembly()));
=> Configure(LogManager.GetRepository(CallerAssembly.IsSupported ? Assembly.GetCallingAssembly() : CallerAssembly.Fallback));

@fluffynuts fluffynuts Aug 7, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this logic quite a few times throughout - perhaps move to CallerAssembly with a lazy backing field and reference that static property elsewhere (eg CallerAssembly.ResolvedCallerAssembly? Main reason being that it's no longer just an obvious call to Assembly.GetCallingAssembly(), but now includes logic, which is repeated in quite a few places.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@FreeAndNil FreeAndNil Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fluffynuts
Good catch, but this one can't move - though I don't like it either. Assembly.GetCallingAssembly()
returns the caller of the method containing the call, so in a property on CallerAssembly the caller
is log4net itself - every logger would land in log4net's own repository. Two-assembly harness,
called from UserApp:

inline (current PR)      -> UserApp       <- correct
via property (suggested) -> log4net

The lazy backing field is worse: the first assembly to touch it wins forever, so the result depends on
load order.

The BCL hits this exact problem and needs an internal enum for it - System.Threading.StackCrawlMark
(LookForMyCaller, LookForMyCallersCaller), passed by ref so Assembly.Load can delegate to a
private helper. It's NotPublic and no public API accepts it. It also wouldn't help us: it's
stack-walking machinery, and AOT throws precisely because there is no stack to walk.

Only a Roslyn interceptor would actually remove the repetition - left out here, but I'm happy to open a separate issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fluffynuts reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@FreeAndNil FreeAndNil Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I think [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
is the bigger eyesore.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fluffynuts fluffynuts Aug 12, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I was also looking at that - but if I were to guess wildly, the attribute as found (when found) in the dotnet runtime, is likely sealed. Otherwise I'd suggest sub-classing with those settings - which still may not even work if the logic doesn't bother to look at attributes' base types (quite likely). I don't think there's much that can be done about it :/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/log4net/Diagnostics/CodeAnalysis/DynamicallyAccessedMembersAttribute.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gdziadkiewicz
gdziadkiewicz requested a balanced review from Copilot August 12, 2026 07:24
fluffynuts
fluffynuts approved these changes Aug 12, 2026
View reviewed changes
Copilot started reviewing on behalf of gdziadkiewicz August 12, 2026 07:49
View session
Copilot AI reviewed Aug 12, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

This PR makes log4net usable when consumers publish with Native AOT / trimming by guarding unsupported runtime APIs, avoiding repeated configuration-system failures, and annotating reflection-based activation paths so required constructors survive trimming.

Changes:

  • Guarded Assembly.GetCallingAssembly() usage via a cached runtime probe with an entry-assembly fallback.
  • Improved SystemInfo.GetAppSetting() behavior under trimmed/missing System.Configuration by falling back to environment variables and reducing noise.
  • Added trimming annotations (DynamicallyAccessedMembers) and refactored pattern-converter registries to preserve constructors; documented Native AOT constraints in the manual.

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/site/antora/modules/ROOT/pages/manual/native-aot.adoc Adds a Native AOT/trimming manual page with guidance and examples.
src/site/antora/modules/ROOT/pages/manual/configuration.adoc Notes XML config is unavailable under PublishAot and links to Native AOT guidance.
src/site/antora/modules/ROOT/nav.adoc Adds the new Native AOT page to the manual navigation.
src/log4net/Util/CallerAssembly.cs Introduces a runtime probe + fallback assembly for unsupported GetCallingAssembly().
src/log4net/LogManager.cs Routes calling-assembly-based overloads through the guard/fallback.
src/log4net/Config/BasicConfigurator.cs Uses guarded calling-assembly resolution for repository selection.
src/log4net/Config/XmlConfigurator.cs Uses guarded calling-assembly resolution to select repositories.
src/log4net/Util/SystemInfo.cs Adds config-system detection, env-var fallback, and a non-inlined settings reader.
src/log4net/Util/TypeConverters/ConverterRegistry.cs Annotates converter Type flows to keep parameterless ctors under trimming.
src/log4net/Layout/PatternLayout.cs Refactors built-in converter registry to retain trimming annotations.
src/log4net/Util/PatternString.cs Same registry refactor for PatternString converters.
src/log4net/Util/ConverterInfo.cs Annotates ConverterInfo.Type to preserve constructors.
src/log4net/Core/LoggerManager.cs Annotates repository type activation paths.
src/log4net/Core/IRepositorySelector.cs Annotates repository type parameters in the interface.
src/log4net/Core/DefaultRepositorySelector.cs Propagates repository-type annotations and stores annotated default type.
src/log4net/Config/RepositoryAttribute.cs Annotates repository type property to preserve constructors.
src/log4net/Appender/FileAppender.cs Adds trimming-safe constraints/annotations for default locking model activation.
src/log4net/Diagnostics/CodeAnalysis/DynamicallyAccessedMembersAttribute.cs Adds a polyfill attribute for TFMs lacking it (for trimmer recognition).
src/log4net/Diagnostics/CodeAnalysis/DynamicallyAccessedMemberTypes.cs Adds a polyfill enum for TFMs lacking it (for trimmer recognition).
src/log4net.Tests/log4net.Tests.csproj Links CallerAssembly.cs into tests.
src/log4net.Tests/Util/SystemInfoTest.cs Adds tests for config-system fallback and detection behavior.
src/log4net.Tests/Util/CallerAssemblyTest.cs Adds tests validating the guarded calling-assembly behavior on JIT.
src/changelog/3.4.0/306-usable-from-a-publishaot-build.xml Adds a changelog entry describing the Native AOT fixes and limitations.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/log4net/Diagnostics/CodeAnalysis/DynamicallyAccessedMemberTypes.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/log4net/Util/SystemInfo.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/log4net/Util/SystemInfo.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/log4net/Util/SystemInfo.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/log4net/Util/SystemInfo.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/log4net/Layout/PatternLayout.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/log4net.Tests/log4net.Tests.csproj Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/log4net.Tests/Util/SystemInfoTest.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

FreeAndNil added 2 commits August 16, 2026 21:06
@FreeAndNil
Copy the built-in ConverterInfo entries per parser instead of sharing…
f78341f
… them, make

_configurationSystemUnavailable volatile, and fix the IsAndoid test name typo.
@FreeAndNil
narrow the missing configuration system check (#306)
ae3ee38
Ask the runtime instead of guessing from the exception: Native AOT is identified by CallerAssembly.IsSupported, and elsewhere only a failure naming System.Configuration itself counts, so a broken app.config is still reported.
@FreeAndNil FreeAndNil added this to the 3.4.0 milestone Aug 16, 2026
@FreeAndNil FreeAndNil linked an issue Aug 16, 2026 that may be closed by this pull request
Exception calling GetLogger when compiled using PublishAOT #233
Closed
@FreeAndNil
enforce IDE0005 on build and remove unnecessary usings (#306)
364e0ee
Set EnforceCodeStyleInBuild and GenerateDocumentationFile in Directory.Build.props,
suppress CS1591 for test and integration projects, and clear the 15 usings this
  surfaced.
FreeAndNil added a commit that referenced this pull request Aug 16, 2026
@FreeAndNil
test what works under Native AOT (#306)
1920bb9
Add log4net.Tests.Aot, which runs the same probes JIT compiled and published with PublishAot and fails when either run differs from the expected list, and run it from CI. Guard the trimmer annotations with unit tests, and fix the two IL3000 warnings a consumer sees by handling the empty Assembly.Location a single file application returns.
@FreeAndNil
test what works under Native AOT (#306)
3e23ae9
Add log4net.Tests.Aot, which runs the same probes JIT compiled and published with
PublishAot and fails when either run differs from the expected list, and run it from
CI. Silence log4net's internal messages while probing, because the probes report their
own failures and a passing run that prints errors reads as a broken one.
@FreeAndNil
FreeAndNil force-pushed the Feature/306-aot-calling-assembly branch from 1920bb9 to 3e23ae9 Compare August 16, 2026 21:31
@FreeAndNil
Merge remote-tracking branch 'origin/master' into Feature/306-aot-cal…
cf8aa32
…ling-assembly
gdziadkiewicz
gdziadkiewicz approved these changes Aug 18, 2026
View reviewed changes

@gdziadkiewicz gdziadkiewicz left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@FreeAndNil
FreeAndNil merged commit e3246a5 into master Aug 18, 2026
3 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@FreeAndNil
FreeAndNil deleted the Feature/306-aot-calling-assembly branch August 18, 2026 08:30
This was referenced Aug 21, 2026
Bump log4net from 3.3.2 to 3.4.0 OPCFoundation/UA-EdgeTranslator#118
Merged
Bump log4net from 3.3.2 to 3.4.0 overdrive1708/FastFileReadonly#13
Merged
Bump log4net from 3.3.2 to 3.4.0 afonsoft/EAF#377
Merged
Bump log4net and Microsoft.Extensions.Logging.Console quartznet/quartznet#3354
Merged
Bump the all-updates group with 5 updates frasermolyneux/demo-manager#67
Merged
This was referenced Nov 10, 2026
Bump the minor-version-updates group with 5 updates PxTools/PCAxis.Serializers#219
Merged
Bump the nuget-dependencies group with 14 updates SvenGroot/JumboCore#76
Closed
Bump Crc32.NET and 12 others SvenGroot/JumboCore#77
Closed
Bump log4net from 3.3.2 to 3.5.0 filadelfija-cyber/Reposter#1
Merged
Bump log4net from 3.2.0 to 3.5.0 marcmy/ProtonVPN-win-app#153
Closed
Dependency (dependabot/nuget): Bump log4net from 3.2.0 to 3.5.0 sg-scale/NETworkManager--2025.10.18.0#10
Open
Bump the nuget-dependencies group with 14 updates SvenGroot/JumboCore#78
Closed
Bump Crc32.NET and 12 others SvenGroot/JumboCore#79
Closed
Bump log4net from 3.3.2 to 3.5.0 jasonlandreth/GarminR10-BLE-Connector#6
Closed
Bump log4net from 3.3.0 to 3.5.0 chutch3/kenku#87
Merged
Bump log4net from 2.0.15 to 3.6.0 sg-scale/NETworkManager--2022.12.20.0#5
Open
Bump the minor-version-updates group with 4 updates PxTools/PCAxis.Menu.OracleDatamodelMenu#115
Merged
Bump log4net and 3 others PxTools/PCAxis.Metadata#108
Merged
Bump Crc32.NET and 12 others SvenGroot/JumboCore#80
Closed
Bump the nuget-dependencies group with 14 updates SvenGroot/JumboCore#81
Closed
Bump log4net and 4 others PxTools/PCAxis.Menu.MsSqlDatamodelMenu#114
Merged
Bump log4net and 3 others PxTools/PXWeb.SavedQuery.Oracle#119
Merged
Bump log4net and 4 others PxTools/PCAxis.Core#142
Merged
Bump log4net from 3.3.1 to 3.6.0 bbtsoftware/AzureDevOpsPolicyConfigurator#255
Open
Bump the minor-version-updates group with 4 updates PxTools/PCAxis.Menu#100
Merged
Bump log4net and 3 others PxTools/PCAxis.Query#113
Merged
Bump log4net and PCAxis.Core PxTools/PCAxis.Query#114
Closed
Bump log4net from 2.0.8 to 3.6.0 devsecops-lab-luis/demo-ghas-multilenguaje#27
Open
Dependency (dependabot/nuget): Bump log4net from 3.3.0 to 3.6.0 Nevergoodstudy/NETworkManager-macOS#24
Open
Bump the nuget-dependencies group with 14 updates SvenGroot/JumboCore#82
Open
Bump Crc32.NET and 12 others SvenGroot/JumboCore#83
Open
deps: Bump log4net from 3.3.2 to 3.6.0 OliverRetro/WavePlus#62
Open
Bump log4net from 3.3.1 to 3.6.0 bNobo/needabreak#176
Open
This was referenced Nov 19, 2026
Bump log4net from 3.3.2 to 3.6.0 jasonlandreth/GarminR10-BLE-Connector#7
Open
Bump Crc32.NET and 12 others SvenGroot/JumboCore#84
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@fluffynuts fluffynuts fluffynuts approved these changes
@gdziadkiewicz gdziadkiewicz gdziadkiewicz approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

3.4.0

Development

Successfully merging this pull request may close these issues.

Exception calling GetLogger when compiled using PublishAOT

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@FreeAndNil @fluffynuts @gdziadkiewicz
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.