Reputation: 4451
The Nlog internal log file says:
Warn FileTarget(Name=allfile): Failed to create file appender: C:\nlog-all-2023-05-26.log Exception: System.UnauthorizedAccessException: Access to the path 'C:\nlog-all-2023-05-26.log' is denied.
Why should I get this warning?
Here is the configuration in program.cs:
NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
builder.Host.UseNLog();
GlobalDiagnosticsContext.Set("logfilesPath", "C:\Govmeeting\LOGS");
And here are the relevant lines from nlog.config:
<target xsi:type="File" name="allfile" fileName="${gdc:item=logfilesPath}/nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}"
archiveEvery="Day" archiveFileName="${gdc:item=logfilesPath}/nlog-${shortdate}.log" />
<logger name="*" minlevel="Trace" writeTo="allfile" />
This happens with Nlog 5.1.5 and 4.7.*
Upvotes: 0
Views: 351
Reputation: 19877
I'm guessing that Logger-output is generated before having assigned GDC-property logfilesPath
. Causing the issue with writing to root-folder:
UnauthorizedAccessException: Access to the path 'C:\nlog-all-2023-05-26.log' is denied.
Maybe assign GDC-property very early:
GlobalDiagnosticsContext.Set("logfilesPath", "C:\Govmeeting\LOGS"); // First thing
NLog.LogManager.Setup().LoadConfigurationFromAppSettings();
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
builder.Host.UseNLog();
Upvotes: 0