-
-
Notifications
You must be signed in to change notification settings - Fork 332
make log4net usable from a PublishAOT build (#306) #306
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
Open
FreeAndNil
wants to merge
1
commit into
master
Choose a base branch
from
Feature/306-aot-calling-assembly
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/changelog/3.4.0/306-usable-from-a-publishaot-build.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns="https://logging.apache.org/xml/ns" | ||
| xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" | ||
| type="fixed"> | ||
| <issue id="233" link="https://github.com/apache/logging-log4net/issues/233"/> | ||
| <issue id="306" link="https://github.com/apache/logging-log4net/pull/306"/> | ||
| <description format="asciidoc">Make log4net usable from a `PublishAot` build, where `LogManager.GetLogger()` | ||
| used to throw `PlatformNotSupportedException` from `Assembly.GetCallingAssembly()`, and where | ||
| repositories and pattern converters were left without a constructor by the trimmer. Configuration | ||
| has to be done in code - see the new | ||
| https://logging.apache.org/log4net/latest/manual/native-aot.html[Native AOT and trimming] page | ||
| (reported by @vpenades, implemented by @FreeAndNil in https://github.com/apache/logging-log4net/pull/306[#306])</description> | ||
| </entry> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #region Apache License | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one or more | ||
| // contributor license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright ownership. | ||
| // The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| // (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| #endregion | ||
|
|
||
| using System.Reflection; | ||
| using System.Runtime.CompilerServices; | ||
| using log4net.Util; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace log4net.Tests.Util; | ||
|
|
||
| /// <summary> | ||
| /// Tests for <see cref="CallerAssembly"/>, the guard that keeps the | ||
| /// <see cref="Assembly.GetCallingAssembly()"/> based overloads usable under Native AOT. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// The AOT half of the behaviour cannot be covered here - these tests always run on a JIT | ||
| /// runtime, where <see cref="CallerAssembly.IsSupported"/> is <see langword="true"/> and | ||
| /// <see cref="CallerAssembly.Fallback"/> is never consulted. What they do cover is that the | ||
| /// guard stays inert on a JIT runtime, so that no call site silently starts attributing | ||
| /// loggers to the entry assembly instead of the caller. | ||
| /// </para> | ||
| /// </remarks> | ||
| [TestFixture] | ||
| public class CallerAssemblyTest | ||
| { | ||
| /// <summary> | ||
| /// The probe recognises a runtime that does implement | ||
| /// <see cref="Assembly.GetCallingAssembly()"/>, so the guard stays out of the way everywhere | ||
| /// except Native AOT. A false negative here would silently move every logger to the entry | ||
| /// assembly's repository. | ||
| /// </summary> | ||
| [Test] | ||
| public void IsSupportedOnAJitRuntime() => Assert.That(CallerAssembly.IsSupported, Is.True); | ||
|
|
||
| /// <summary> | ||
| /// There is always a replacement assembly to attribute a call to, even though the entry | ||
| /// assembly is <see langword="null"/> in a host without a managed entry point. | ||
| /// </summary> | ||
| [Test] | ||
| public void FallbackIsAvailable() => Assert.That(CallerAssembly.Fallback, Is.Not.Null); | ||
|
|
||
| /// <summary> | ||
| /// The guard has to leave <see cref="Assembly.GetCallingAssembly()"/> in the method whose | ||
| /// caller is wanted, so a call from this assembly still resolves to this assembly. | ||
| /// </summary> | ||
| [Test] | ||
| public void GuardedCallStillReportsTheCallersAssembly() | ||
| => Assert.That(GuardedCallingAssembly(), Is.SameAs(typeof(CallerAssemblyTest).Assembly)); | ||
|
|
||
| /// <summary> | ||
| /// Stands in for a public log4net entry point. Inlining is suppressed because it would | ||
| /// hand <see cref="Assembly.GetCallingAssembly()"/> a different frame - the same effect | ||
| /// that makes the release build of <see cref="SystemInfoTest"/> unable to assert on an | ||
| /// exact assembly. | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static Assembly GuardedCallingAssembly() | ||
| => CallerAssembly.IsSupported ? Assembly.GetCallingAssembly() : CallerAssembly.Fallback; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
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.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
CallerAssemblythe calleris log4net itself - every logger would land in log4net's own repository. Two-assembly harness,
called from
UserApp: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 byrefsoAssembly.Loadcan delegate to aprivate helper. It's
NotPublicand no public API accepts it. It also wouldn't help us: it'sstack-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.
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.