I'm using NLog in a .NET Core 3.1 worker service application. Following the tutorial of NLog I inserted an nlog.config file to manage the configuration.
Now I'm confused because I have three points where I configure the logging:
In the code where I need to create a logger in a dependency injection context
// Other code... services.AddScoped<IApplyJcdsCommandsJob, ApplyJcdsCommandsJob>(provider => { var loggerFactory = LoggerFactory.Create(builder => { builder .ClearProviders() .AddFilter("Microsoft", Microsoft.Extensions.Logging.LogLevel.Trace) .AddFilter("System", Microsoft.Extensions.Logging.LogLevel.Trace) .AddFilter("ApplyJcdsCommandsJob", Microsoft.Extensions.Logging.LogLevel.Trace) //.AddConsole() //.AddEventLog(); .AddNLog(configuration); }); Microsoft.Extensions.Logging.ILogger logger = loggerFactory.CreateLogger<CommandsJob>(); return new CommandsJob(logger); }) // Other code...
In appSettings.json
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Trace", "System": "Trace", "Microsoft": "Trace" } } }
In NLog.config
The default config file produced by the nuget package installation:
<!-- a section of the config --> <targets> <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="f" /> </rules> <!-- ... -->
What I see is that if I remove the Nlog.config file, the log file will not be created. Other changes seam to have no effect.
How are this configurations related? What is the best way to switch on/off the logging and set the level?