Skip to content

feat: log4net appender no longer initializes the SDK - #5592

Draft
jamescrosswell wants to merge 1 commit into
feat/no-init-from-logging-nlog-5245from
feat/no-init-from-logging-log4net-5245
Draft

jamescrosswell wants to merge 1 commit into
feat/no-init-from-logging-nlog-5245from
feat/no-init-from-logging-log4net-5245

Conversation

@jamescrosswell

Copy link
Copy Markdown
Collaborator

The log4net portion of #5245, stacked on #5585 (NLog) and following the same design. The Sentry appender for log4net now only sends log events to Sentry; Sentry has to be initialized separately via SentrySdk.Init, UseSentry, etc.

Part of #5245

Breaking changes

  • SentryAppender.Dsn is removed, along with the appender initializing the SDK on the first append.
  • SentryAppender.Environment is removed. Events and structured logs take the environment from the options used to initialize Sentry.
  • SentryAppender no longer overrides OnClose. It only existed to dispose the SDK the appender had initialized, so closing the log4net repository no longer flushes Sentry; disposing the handle from SentrySdk.Init does.
  • XML configs that still set <Dsn> or <Environment> on the appender keep loading. log4net reports each one through its internal logging (log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [Dsn] to set object on [Sentry.Log4Net.SentryAppender]) and applies the remaining properties, but Sentry is no longer initialized.

Before:

<appender name="SentryAppender" type="Sentry.Log4Net.SentryAppender, Sentry.Log4Net">
  <Dsn value="https://key@sentry.io/1" />
  <Environment value="dev" />
</appender>

After:

using var _ = SentrySdk.Init(o =>
{
    o.Dsn = "https://key@sentry.io/1";
    o.Environment = "dev";
});
<appender name="SentryAppender" type="Sentry.Log4Net.SentryAppender, Sentry.Log4Net" />

Notes for review

  • On a disabled hub the appender now returns immediately. That's the same path the old code took when no Dsn was set.
  • The unit test fixture now uses an enabled hub. Previously the substitute hub reported IsEnabled == false, and events were only captured because the mocked init had run.
  • Like NLog, log4net needs no UseLog4Net(): identity, properties and the SDK name are all applied by the appender to the events and logs it creates.
  • SendIdentity and MinimumEventLevel stay; they configure what the appender sends, not the SDK.
  • ApiApprovalTests.Run.Net4_8 and DotNet10_0 are copies of the regenerated DotNet11_0 snapshot, which all three matched byte-for-byte beforehand. Net4_8 can't regenerate on macOS, and DotNet10_0 isn't run at all since the test project only targets the latest TFM.

🤖 Generated with Claude Code

The Sentry appender for log4net now only sends log events to Sentry.
Sentry must be initialized separately (SentrySdk.Init, UseSentry, etc).

- Remove SentryAppender.Dsn and the lazy SDK initialization on first append
- Remove SentryAppender.Environment; events take the environment from the
  options used to initialize Sentry
- Remove the OnClose override, which only disposed the SDK the appender
  had initialized

Part of #5245

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jamescrosswell jamescrosswell added Breaking Change Binary/Source/Behavioral Breaking Changes. log4net labels Sep 17, 2026
@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.56%. Comparing base (489d7ce) to head (6438db5).

Additional details and impacted files
@@                           Coverage Diff                           @@
##           feat/no-init-from-logging-nlog-5245    #5592      +/-   ##
=======================================================================
- Coverage                                74.58%   74.56%   -0.03%     
=======================================================================
  Files                                      514      514              
  Lines                                    18745    18728      -17     
  Branches                                  3646     3641       -5     
=======================================================================
- Hits                                     13981    13964      -17     
  Misses                                    3891     3891              
  Partials                                   873      873              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

<!--via code (SentrySdk.Init) or via another integration like ASP.NET-->
<!--The SDK only needs to be initialized once, you can choose where to do that-->
<!-- <Dsn value="TODO: Configure your DSN here and uncomment this line" /> -->
<!--This only configures the Sentry appender. Sentry itself is initialised in Program.cs-->

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
<!--This only configures the Sentry appender. Sentry itself is initialised in Program.cs-->
<!--Configures the Sentry appender. Sentry itself is initialised in Program.cs-->


private static void Main()
{
// Initialise the Sentry SDK. The SentryAppender configured in app.config only sends log events to Sentry.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
// Initialise the Sentry SDK. The SentryAppender configured in app.config only sends log events to Sentry.
// Initialise the Sentry SDK.

// Honor the appender-level settings, overriding the scope/options defaults, to match the SentryEvent path.
if (!string.IsNullOrWhiteSpace(environment))
{
log.SetAttribute("sentry.environment", environment!);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This gets handled here now (just a couple of lines up):

log.SetDefaultAttributes(options, scope, Sdk);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking Change Binary/Source/Behavioral Breaking Changes. log4net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant