How to mock Eventlog with WebApplicationFactory

88 Views Asked by At

I have a application with a Log4net RollingFileAppender for info-logs and a EventLog for error-logs

        builder.ClearProviders()
            .AddEventLog(new EventLogSettings { SourceName = "ABC"})
            .AddLog4Net();

Configured in appsettings here

"Logging": {
        "EventLog": {
            "LogLevel": {
                "Default": "Error"
            }
        },
        "LogLevel": {
            "Default": "None",
            "MyNamespace.*": "Information"
        }
    }

When create a WebApplicationFactory in my test suite, I substitute the logger to an in-memory implantation with this code

  builder.ConfigureLogging(logging =>
  {
    logging.ClearProviders();
    logging.AddProvider(_inMemLoggerProvider);
  });

The InMemLoggerProvider is inspired by this code https://github.com/horsdal/InMemoryLogger

When running the test-suite all errors logged from MyNamespace is caught by the InMemoryLogger.

But errors logged from other namespaces is not caught by the InMemoryLogger.

When running the application without substituting the logger, errors are logged fine to the Event Log.

0

There are 0 best solutions below