简体   繁体   中英

NLog double logging - how to find the problem?

There are multiple articles on SO and the rest of the internet on this, but I think I've followed all of the all the advice and recommendations, and I'm still getting double logs, so clearly I'm doing something else wrong here.

I have a .NET Core web app with the following rules section in my nlog.config:

  <rules>
    <logger name="MyApp.*" minlevel="Info" writeTo="database" final="true" />
    <logger name="*" minlevel="Error" writeTo="fileSystem,database" final="true"/>
    <logger name="*" minlevel="Warn" writeTo="database" final="true"/>
  </rules>

In my Startup.ConfigureServices method, I have a line:

services.AddLogging();

and in the Startup.Configure method, I have a link to a helper method like so:

app.StartLogging(Configuration);

This helper method is written like so:

public static IApplicationBuilder StartLogging(this IApplicationBuilder app, IConfiguration config)
{
    var loggerFactory = app.ApplicationServices.GetService<ILoggerFactory>();
    loggerFactory.AddNLog();
    LogManager.LoadConfiguration("nlog.config");
    LogManager.Configuration.Variables["appDbConnectionString"] = config["ConnectionStrings:ApplicationContext"];
    return app;
}

...and that's it. There's no more mentions of NLog anywhere else in this project. Yet, I'm still getting double log entries for info. Why? And how can I detect that there are multiple NLog loggers running on my ILoggerFactory ?

NLog.Web.AspNetCore v4.8.6 changes registration of NLog Logging Provider , so one is now allowed to call both AddNLog and UseNLog without experiencing double logging.

NLog 5.0 implements validation of incorrect duplicate target configuration , so one will not experience double logging when using incorrect NLog configuration.

I suggest adjusting your rules. All three of your rules tell NLog to write to the database; I'm guessing two of the rules are getting picked up for certain calls to the logger. I was getting double entries in my database table with a similar rules configuration. I removed a 'logger name="*"' rule that wrote to the database and the double entries stopped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM