I'm noticing this behavior in an Asp.NET Core 8.0 application. This is some new behavior that is not present in .NET Framework 4.8
These are the relevant NuGet packages that I'm using:
I'm using the nlog.development.config
configuration file below.
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="Seq.Client.NLog" />
<add assembly="Microsoft.ApplicationInsights.NLogTarget" />
</extensions>
<targets>
<target name="seq" xsi:type="Seq" serverUrl="http://localhost:5341" />
<target name="aiTarget" xsi:type="ApplicationInsightsTarget" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="seq" />
<logger name="*" minlevel="Info" writeTo="aiTarget" />
</rules>
</nlog>
I setup logging in the Main method using the code below:
var appInsightsConnectionString = builder.Configuration["AppInsightsConnectionString"];
var appInsightsOptions = new ApplicationInsightsServiceOptions
{
EnableAdaptiveSampling = false,
ConnectionString = appInsightsConnectionString
};
builder.Services.AddApplicationInsightsTelemetry(appInsightsOptions);
var nlogConfigFile = builder.Environment.IsDevelopment() ? "nlog.development.config" : "nlog.config";
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(nlogConfigFile);
builder.Services.AddLogging(loggingBuilder => { loggingBuilder.AddNLog(); });
The ILogger<ScheduleController> logger
instance that is injected into the ScheduleController class has both "seq" and aiTarget" targets - it writes to both locations. Yet a _logger = LogManager.GetLogger("TraceLogger");
call later on in the code returns an instance that no longer writes into the "aiTarget", only into the "seq". How should I go about fixing this behavior please, yet maintain backwards compatibility with .NET Framework 4.8?
I'm also wondering why is it that the builder.Services.AddLogging(loggingBuilder => { loggingBuilder.AddNLog(); });
call in a Dev environment loads the nlog.config
file instead of the nlog.development.config
.
Maybe you need to set EnableActiveTelemetryConfigurationSetup
:
var appInsightsOptions = new ApplicationInsightsServiceOptions
{
EnableAdaptiveSampling = false,
ConnectionString = appInsightsConnectionString,
EnableActiveTelemetryConfigurationSetup = true,
};
builder.Services.AddApplicationInsightsTelemetry(appInsightsOptions);
See also: https://github.com/microsoft/ApplicationInsights-dotnet/issues/2070
When troubleshooting logging in NLog, then it is also a good idea to look at output from NLog Internal-logging.