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 — Fix `LoggingEvent.UserName` resolving the Windows identity for every event by FreeAndNil · Pull Request #304 · apache/logging-log4net

Conversation

@FreeAndNil

@FreeAndNil FreeAndNil commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

The cache added in 2.0.15 (commit 9305ea9) was declared as an instance field
on LoggingEvent. Since every event is a fresh instance the guard was always
false, so the ??= never hit and each event ran a full
WindowsIdentity.GetCurrent().Name - measured at 79.6 us on the machine used
here. The same commit had changed TryGetCurrentUserName from static to
instance, so the caching it introduced has never taken effect.

The doc comment above the property already described the intended design and
the reason it was abandoned: the name should be cached "as long as the identity
stayed constant", but WindowsIdentity.GetCurrent() "seems to return different
objects every time". Object identity was the wrong comparison. The security
identifier is stable, and obtaining the identity is far cheaper than resolving
its name - the timing table in that same comment puts the two at roughly 20 ns
against 804 ns.

So the name is now cached without changing what the property reports:

  • A thread that is not impersonating runs as the process identity, so its name
    is resolved once per process. WindowsIdentity.GetCurrent(ifImpersonating: true) answers that question for 249 ns against 78 us for a name, making this
    the fast path for services, console applications and ASP.NET Core.
  • A thread that is impersonating - classic ASP.NET with
    <identity impersonate="true"/>, or RunImpersonated - has its name
    resolved once per distinct user, keyed by security identifier and capped at
    MaxCachedUserNames entries so that a site in front of a large directory
    cannot accumulate one entry per visitor.

The process-identity value is assigned in exactly one place, inside the branch
that has already established the thread is not impersonating. Seeding it from
an impersonating thread would report that user for the rest of the process,
which ImpersonationDoesNotSeedTheProcessUserName guards against.

A buffered FixFlags.All event goes from 192,937 to 17,578 ns on this machine,
both sides built as netstandard2.0. The remainder is FixFlags.LocationInfo,
which is untouched here.

Two related changes in the same method:

  • Environment.UserName, the fallback when WindowsIdentity is unusable, is
    also cached; it measured 33.8 us per call. Impersonation does not apply on
    that path.
  • The SecurityException handler now sets the unavailable flag, as the
    PlatformNotSupportedException handler already did. Under partial trust the
    old code threw, caught and logged once per event forever.

Documentation: the %username pattern entry now explains the caching and
points ASP.NET users at %identity, which is both cheaper and usually what is
wanted because it reports the authenticated application user. The
BufferingForwardingAppender manual page recommends Partial in its example
instead of All; the surrounding comment already warned that the All default
"may negatively impact performance enough to warrant changing it", and
LocationInfo costs 6.9 us and 14 kB per event.

LogicalThreadContextProperties no longer stores an empty dictionary just to
replace it on the next line, and no longer clones on removal of an absent key.
Both are cleanups; neither changes the allocation profile of the common
set/remove pattern.

Sorry, something went wrong.

Uh oh!

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

All reactions
@FreeAndNil
Fix LoggingEvent.UserName resolving the Windows identity for every …
0007db4
…event
@FreeAndNil
FreeAndNil requested review from fluffynuts and gdziadkiewicz August 3, 2026 20:21
@gdziadkiewicz

gdziadkiewicz commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

@FreeAndNil can we adjust the tests?

All reactions

Sorry, something went wrong.

Uh oh!

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

@FreeAndNil
Mono does not support WindowsIdentity
6bfb9be
@FreeAndNil

FreeAndNil commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

@FreeAndNil can we adjust the tests?

@gdziadkiewicz Yes, we can ;-)

All reactions

Sorry, something went wrong.

Uh oh!

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

@FreeAndNil FreeAndNil mentioned this pull request Aug 4, 2026
Add FileAppend feature to the Logging module DevTeam/dotnet-matrix#1
Merged
FreeAndNil added 2 commits August 4, 2026 10:54
@FreeAndNil
added missing comments
c3c6cea
@FreeAndNil
Merge branch 'master' into Feature/304-username-identity-lookup
16f21ab
fluffynuts
fluffynuts approved these changes Aug 4, 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.

lgtm

Sorry, something went wrong.

Uh oh!

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

All reactions
gdziadkiewicz
gdziadkiewicz approved these changes Aug 4, 2026
View reviewed changes
@FreeAndNil
FreeAndNil merged commit 29a2104 into master Aug 4, 2026
3 checks passed

Uh oh!

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

@FreeAndNil
FreeAndNil deleted the Feature/304-username-identity-lookup branch August 4, 2026 18:49
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
Bump Aspire.Hosting.AppHost and 14 others sheshankchandra/EggLedger#74
Closed
deps: Bump log4net from 3.3.2 to 3.4.0 wojciechowskiapp/Kaption#13
Closed
Bump the nuget-dependencies group with 10 updates SvenGroot/JumboCore#51
Closed
Bump log4net and 8 others SvenGroot/JumboCore#52
Closed
deps: Bump log4net from 3.3.2 to 3.4.0 OliverRetro/WavePlus#45
Closed
Bump log4net from 3.3.2 to 3.4.0 UnderminersTeam/UndertaleModTool#2468
Merged
Bump log4net and System.IO.Ports CreativeCodersTeam/Core#166
Closed
Bump log4net from 3.3.2 to 3.4.0 loresoft/KickStart#313
Merged
Bump log4net from 3.3.2 to 3.4.0 ParadoxGameConverters/PGCG.commonItems#914
Merged
Bump log4net from 3.3.2 to 3.4.0 AlexHedley/poker-recording#354
Merged
Bump log4net from 3.3.2 to 3.4.0 micutio/TermRTS#43
Closed
Bump log4net and 9 others SvenGroot/JumboCore#53
Closed
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

@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

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

3 participants

@FreeAndNil @gdziadkiewicz @fluffynuts
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.