Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To clear all existing loggers in NLog while utilizing Microsoft.Extensions.Logging LoggerFactory, you can use the following code snippet:

ILoggerFactory loggerFactory = new LoggerFactory()
    .AddNLog(nLogConfiguration);

NLog.LogManager.Configuration = nLogConfiguration;

loggerFactory.Dispose();
NLog.LogManager.Shutdown();
NLog.LogManager.Configuration = null;

loggerFactory = new LoggerFactory()
    .AddNLog(nLogConfiguration);

In the above example, we first initialize our ILoggerFactory instance with the AddNLog() method, passing in our NLog configuration object. We also set the configuration object for the underlying NLog LogManager.

Next, we dispose of the ILoggerFactory instance and shut down the NLog LogManager. We also set the configuration object to null.

Finally, we create a new ILoggerFactory instance with the same NLog configuration, which will result in all existing loggers being cleared.

Note that this code snippet assumes that you have already configured NLog in your application and have an nLogConfiguration object to pass to the AddNLog() method.