Skip to content

Use standard ILogger calls everywhere - #4553

Merged
Mag-nus merged 1 commit into
ACEmulator:masterfrom
Mag-nus:ilogger-standard-calls
Nov 25, 2026
Merged

Mag-nus merged 1 commit into
ACEmulator:masterfrom
Mag-nus:ilogger-standard-calls

Conversation

@Mag-nus

@Mag-nus Mag-nus commented Nov 24, 2026

Copy link
Copy Markdown
Member

Use standard ILogger calls everywhere

The problem

Logging already runs on Microsoft.Extensions.Logging.ILogger, but every call site still used log4net-style extension methods from ACE.Common/Logging/LogExtensions.cs: Info, Warn, DebugFormat, IsDebugEnabled and so on. They were added so the move from log4net's ILog to ILogger could land without touching every call.

That leaves ACE on its own logging API, and every message reaches the logger as one pre-built string. As a result:

  • No structured logging. A sink can't filter or search on a player name, GUID or other value, because the values are already baked into the text.
  • Messages are built even when their level is off. An interpolated log.Debug($"...") builds its string before the call, unless the call is wrapped in an IsDebugEnabled check. The *Format calls also allocate their argument array before the level check runs.

The fix

Every call now uses the standard API, and LogExtensions.cs is deleted.

Before After
log.Debug / DebugFormat log.LogDebug
log.Info / InfoFormat log.LogInformation
log.Warn / WarnFormat log.LogWarning
log.Error / ErrorFormat log.LogError
log.Fatal / FatalFormat log.LogCritical
log.IsDebugEnabled (and the other levels) log.IsEnabled(LogLevel.Debug)

Messages become message templates with named values:

// before
log.Warn($"{Name}.HandleActionCastTargetedSpell({targetGuid:X8}, {spellId}) - CombatMode mismatch {CombatMode}");
log.InfoFormat("{0} logged in from {1}", name, endpoint);
log.Error(ex);

// after
log.LogWarning("{Name}.HandleActionCastTargetedSpell({TargetGuid:X8}, {SpellId}) - CombatMode mismatch {CombatMode}", Name, targetGuid, spellId, CombatMode);
log.LogInformation("{Name} logged in from {Endpoint}", name, endpoint);
log.LogError(ex, "{Message:l}", ex.Message);

Totals: 951 calls and 62 level checks, across 155 files.

How it was converted

A Roslyn syntax rewriter did the conversion, not regex:

  • Interpolated strings, string literals and "text" + value chains become templates. Each interpolation hole becomes a named value, and its format and alignment are kept, for example {TargetGuid:X8}.
  • *Format calls have their {0}-style placeholders replaced with names taken from the matching argument.
  • Names come from the expression: targetGuid → TargetGuid, player.Name → Name, item.Guid.Full → Guid, GetFullMessage() → FullMessage. When two different values in one message would get the same name, they are qualified with their owner: PlayerName and TargetName. About 40 values with no natural name were named by hand. These are mostly conditional text fragments such as (playerCount > 1 ? "s" : ""), which became {PlayerPlural}.
  • x.ToString("fmt") becomes {X:fmt}, which renders the same text but keeps the value structured.
  • Literal braces in message text are doubled, so they still print as braces.
  • Messages that are already a finished string (a variable, a StringBuilder, Environment.StackTrace) go in as "{Message:l}". That is the same form LogExtensions used internally, so their output is unchanged.
  • log.Error(ex), where the exception was the whole message, becomes log.LogError(ex, "{Message:l}", ex.Message). As before, the exception is attached and its message is the text.
  • #if DEBUG and #if WRAP_AND_MEASURE_ACT_WITH_STOPWATCH blocks were converted too, by re-parsing with those symbols defined.
  • using Microsoft.Extensions.Logging; is added in the third-party group where a file didn't already have it. using ACE.Common.Logging; is removed from files that no longer use LogManager.

Commented-out log calls (about 80) are left as they were.

One existing bug fixed

HouseManager.HandleRentPaid mixed $"..." with InfoFormat's {0}. The interpolation turned {0} into a literal 0 before InfoFormat ever saw it, so the log line has always read HandleRentPaid(0) instead of the player's name. It now logs the name.

Differences in log output

Most messages render exactly as before. The differences come from how ILogger formats values compared with C# interpolation:

  • Null values print as (null) instead of an empty string.
  • Values are formatted with the invariant culture. Interpolation used the server's culture. On an en-US or invariant server, the only visible difference is dates logged without an explicit format, which ACE rarely does because most use ToCommonString().
  • A collection passed as a value prints its items, comma-separated, instead of its type name. None were found in the converted calls.

Compatibility

LogExtensions was public in ACE.Common. A mod that calls log.Info(...) or the other log4net-style methods on an ILogger will no longer compile against this build. Such mods need the same one-line change to LogInformation and the rest.

Testing

  • The solution builds with 0 warnings and 0 errors.
  • ACE.Server.Tests/Logging/LogBridgeTests.cs is rewritten for the standard API, and all 7 tests pass. Through the log4net bridge they check:
    • template values and format specifiers render
    • escaped braces render as single braces
    • pre-built text containing braces renders literally
    • every level maps to its log4net level
    • exceptions are attached
    • values aren't formatted when their level is off
    • IsEnabled follows the configured level
    • loggers created before the provider is added still receive output
  • ACE.Server.Tests (4), ACE.Database.Tests (7) and ACE.DatLoader.Tests (6) fail the same tests as on master, all for environmental reasons: no config file, database or retail dat files.
  • Not tested: a full server run comparing log output line by line.

Files

  • 155 files under Source/ (call sites)
  • Source/ACE.Common/Logging/LogExtensions.cs (deleted)
  • Source/ACE.Server.Tests/Logging/LogBridgeTests.cs

🤖 Generated with Claude Code

Convert all log4net-style LogExtensions calls (Info, WarnFormat,
IsDebugEnabled, ...) to the standard Microsoft.Extensions.Logging API
with named message templates, and delete LogExtensions.cs.

Also fixes HouseManager.HandleRentPaid always logging "0" instead of
the player name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mag-nus
Mag-nus merged commit 1ba0120 into ACEmulator:master Nov 25, 2026
2 checks passed
@Mag-nus
Mag-nus deleted the ilogger-standard-calls branch November 25, 2026 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants