GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — make log4net usable from a PublishAOT build (#306) by FreeAndNil · Pull Request #306 · apache/logging-log4net Conversation
|
| /// <seealso cref="Log4NetConfigurationSectionHandler"/> | ||
| public static ICollection Configure() | ||
| => Configure(LogManager.GetRepository(Assembly.GetCallingAssembly())); | ||
| => Configure(LogManager.GetRepository(CallerAssembly.IsSupported ? Assembly.GetCallingAssembly() : CallerAssembly.Fallback)); |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
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:
Assembly.GetCallingAssembly() usage via a cached runtime probe with an entry-assembly fallback.SystemInfo.GetAppSetting() behavior under trimmed/missing System.Configuration by falling back to environment variables and reducing noise.DynamicallyAccessedMembers) and refactored pattern-converter registries to preserve constructors; documented Native AOT constraints in the manual.Copilot reviewed 23 out of 23 changed files in this pull request and generated 8 comments.
| 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.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
… them, make _configurationSystemUnavailable volatile, and fix the IsAndoid test name typo.
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.
Set EnforceCodeStyleInBuild and GenerateDocumentationFile in Directory.Build.props, suppress CS1591 for test and integration projects, and clear the 15 usings this surfaced.
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.
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.
1920bb9 to
3e23ae9
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Exception calling GetLogger when compiled using PublishAOT
There was an error while loading. Please reload this page.