Ask Your Question
1

What is the method for clearing NLog Loggers while utilizing Microsoft.Extensions.Logging LoggerFactory?

asked 2022-04-28 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-09-06 01:00:00 +0000

pufferfish gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-28 11:00:00 +0000

Seen: 14 times

Last updated: Sep 06 '22