Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If UseNLog and AddNLog are used, two logs will be written #324

Closed
czd890 opened this issue Aug 28, 2019 · 4 comments · Fixed by #325
Closed

If UseNLog and AddNLog are used, two logs will be written #324

czd890 opened this issue Aug 28, 2019 · 4 comments · Fixed by #325
Labels
Milestone

Comments

@czd890
Copy link

czd890 commented Aug 28, 2019

NLog version: 4.6.6
NLog.Extensions.Logging: 1.5.2
NLog.Web.AspNetCore: 4.8.4
Platform: .NET Core 2

If builder.AddNLog(this.Configuration); and .UseNLog(); are used,
two same logs will be written.

WebHost.CreateDefaultBuilder()
....
..UseStartup<Startup>()
.UseNLog();

class Startup {
    public void ConfigureServices(IServiceCollection services)
    {
        NLog.LogManager.Configuration = new NLogLoggingConfiguration(this.Configuration.GetSection("NLog"));
        services.AddLogging(builder =>
        {
            builder.AddConfiguration(this.Configuration.GetSection("Logging"));
            builder.AddNLog(this.Configuration);
            builder.AddConsole();
        });

    }
}
{
"Logging": {
        "IncludeScopes": true,
        "LogLevel": {
            "Default": "Warning"
        },
        "NLog": {
            "IncludeScopes": true,
            "CaptureMessageTemplates": true,
            "CaptureMessageProperties": true
        }
    },
    "NLog": {
            "NLog.Web.AspNetCore": {
                "Assembly": "NLog.Web.AspNetCore"
            }
        },
        "internalLogLevel": "Error",
        "internalLogFile": "logs/nlog-internal.log",
        "throwConfigExceptions": "True",
        "targets": {
            "file": {
                "type": "File",
                "fileName": "logs/logs.log",
                "keepFileOpen": true,
                "concurrentWrites": true,
                "archiveAboveSize": 10485760,
                "archiveEvery": "Day",
                "archiveNumbering": "DateAndSequence",
                "archiveDateFormat": "yyyyMMdd",
                "archiveFileName": "logs/logs.{#}.log",
                "layout": "${longdate:universalTime=true} ${logger} ${uppercase:${level}} ${threadid} ${threadname} ${message} ${exception:format=ToString:exceptionDataSeparator=\r\n}\r\n   on ${aspnet-TraceIdentifier} ${aspnet-request-url:IncludeHost=true:IncludePort=true:IncludeQueryString=true}"
            },
            "blackhole": { "type": "Null" }
        },
        "rules": {
            "01-allLogs": {
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "file",
                "final": true
            }
        }
    }
}
@304NotModified
Copy link
Member

Yes, I don't think we could change that easily.

@snakefoot
Copy link
Contributor

snakefoot commented Aug 28, 2019

Yes duplicate of #254

Can be solved by one of these ways:

  • Create new option NLogProviderOptions.ReplaceLoggerProviders that causes UseNLog / AddNLog to clear previous LoggerProviders (before adding itself)
  • Create new option NLogProviderOptions.ReplaceLoggerFactory that causes UseNLog / AddNLog to replace the default LoggerFactory with NLogLoggerFactory

PullRequests are always welcome

@304NotModified 304NotModified transferred this issue from NLog/NLog Aug 28, 2019
@repo-ranger
Copy link
Contributor

repo-ranger bot commented Aug 28, 2019

⚠️ This has been marked to be closed in 7 days.

@snakefoot
Copy link
Contributor

Discovered a different way for resolving this issue. Created PR #325

@repo-ranger repo-ranger bot closed this as completed Sep 4, 2019
@304NotModified 304NotModified reopened this Sep 4, 2019
@snakefoot snakefoot added bug and removed duplicate labels Jan 10, 2023
@snakefoot snakefoot added this to the 1.5.4 milestone Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants