Skip to content

log4net appenders never flush their client, so events are lost at shutdown #390

Open

Description

@ejsmith

In Exceptionless.Log4net 6.2.0, when ApiKey or ServerUrl is set on ExceptionlessAppender or BufferingExceptionlessAppender, the appender creates its own ExceptionlessClient with in-memory storage. Nothing ever processes that client's queue on close or flush, so events logged shortly before the process exits are never sent.

Where

  • public override void ActivateOptions() {
    if (String.IsNullOrEmpty(ApiKey) && String.IsNullOrEmpty(ServerUrl))
    return;
    _client = new ExceptionlessClient(config => {
    if (!String.IsNullOrEmpty(ApiKey) && ApiKey != "API_KEY_HERE")
    config.ApiKey = ApiKey;
    if (!String.IsNullOrEmpty(ServerUrl))
    config.ServerUrl = ServerUrl;
    config.UseInMemoryStorage();
    // Rely on Logging Rules
    config.SetDefaultMinLogLevel(Logging.LogLevel.Trace);
    });
    }
  • public override void ActivateOptions() {
    base.ActivateOptions();
    if (String.IsNullOrEmpty(ApiKey) && String.IsNullOrEmpty(ServerUrl))
    return;
    _client = new ExceptionlessClient(config => {
    if (!String.IsNullOrEmpty(ApiKey) && ApiKey != "API_KEY_HERE")
    config.ApiKey = ApiKey;
    if (!String.IsNullOrEmpty(ServerUrl))
    config.ServerUrl = ServerUrl;
    config.UseInMemoryStorage();
    // Rely on Logging Rules
    config.SetDefaultMinLogLevel(Logging.LogLevel.Trace);
    });
    }

For comparison, the NLog target flushes its client:

protected override void FlushAsync(AsyncContinuation asyncContinuation) {
_client.ProcessQueueAsync().ContinueWith(t => asyncContinuation(t.Exception));
}

What happens

  • The private client is never started, so it does not register the process-exit handler that Startup() registers for other clients.
  • Neither appender overrides OnClose or Flush, so LogManager.Shutdown() / LogManager.Flush() do not send queued events.
  • The queue is only processed by the client's timer (first run after 2 seconds, then every 10 seconds). Events logged in the last few seconds before exit stay in memory and are lost. For BufferingExceptionlessAppender, the buffer is handed to the client on close, but the client's queue is still never sent.
  • The client is never disposed either.

Expected

Closing or flushing the appender (for example LogManager.Shutdown()) sends the queued events, and the appender disposes a client it created.

Steps to reproduce

  1. Configure ExceptionlessAppender in log4net with <apiKey value="..." />.
  2. In a console app, log an error and then call LogManager.Shutdown() and exit.
  3. The event never arrives in Exceptionless.

Suggested fix

In both appenders, override OnClose (calling the base first for the buffering appender so the buffer is submitted) and Flush(int millisecondsTimeout) to call _client.ProcessQueueAsync() and wait up to the timeout, and dispose the client in OnClose when the appender created it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions