NLog: NLog.Web.AspNetCore layout renders are not working when configuring from code
Hi there, I think this is a bug 😃 context I’m trying to introduce a repeatable logging pattern in our organisation. I’ve managed to get things working with the NLog.Config file. now I’m trying to migrate to an appsettings.json file
first cab off the rank is .net Core 3.1 when I use the NLog file approach I’m getting headers through the renderer ${aspnet-request-headers} when I use the appsettings.json approach that value is missing 😦 here is the nlog.config that works
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="info"
internalLogFile="d:\home\LogFiles\startupLog.txt">
<extensions>
<add assembly="NLog.Targets.Splunk" />
</extensions>
<!-- the targets to write to -->
<targets async="true" >
<!-- write to file -->
<target xsi:type="File" name="traceData" fileName="d:\home\LogFiles\traceLogs\trace-${date:format=yyyy-MM-dd HH}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
archiveEvery="Hour"
maxArchiveFiles="24"
/>
<!-- another file log. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="debug" fileName="d:\home\LogFiles\Nlog\Startup-debug-${date:format=yyyy-MM-dd HH-mm}.log"
layout= "${longdate}|${level}|${logger}|${message}|${all-event-properties}|{exception:format=tostring}|${aspnet-request-headers}"
archiveEvery="Minute"
maxArchiveFiles="24"
/>
<target name="Splunk"
xsi:type="SplunkHttpEventCollector"
serverUrl="https://asplunkhec.endpoint"
token="we dont need to know this :) "
channel="we dont need to know this :)"
retriesOnError="0"
layout="${message} ${exception:format=tostring}"
IgnoreSslErrors="true">
<contextproperty name="host" layout="${machinename}" />
<contextproperty name="threadid" layout="${threadid}" />
<contextproperty name="logger" layout="${logger}" />
<contextproperty name="forwarded-client-ip" layout="${aspnet-request-ip:CheckForwardedForHeader=true}"></contextproperty>
<contextproperty name="client-ip" layout="${aspnet-request-ip:CheckForwardedForHeader=false}"></contextproperty>
<contextproperty name="headers" layout="${aspnet-request-headers}"></contextproperty>
<contextproperty name="site" layout="${iis-site-name}"></contextproperty>
<contextproperty name="Url" layout="${aspnet-request-url} "></contextproperty>
<contextproperty name="framework" layout="Environment.Version.ToString()"></contextproperty>
</target>
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="traceData" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxLevel="Info" final="true" />
<logger name="*" minlevel="Debug" maxLevel="Debug" writeTo="debug" />
<logger name="*" minlevel="Information" writeTo="Splunk" />
</rules>
</nlog>
and here is the
appsettings file
"NLog": {
"internalLogLevel": "Info",
"internalLogFile": "d:\\home\\LogFiles\\Nlog\\internal-nlog.txt",
"extensions": {
"NLog.Web.AspNetCore": {
"assembly": "NLog.Web.AspNetCore"
}
},
"targets": {
"allfile": {
"type": "File",
"fileName": "d:\\home\\LogFiles\\Nlog\\nlog-all-${shortdate}.log",
"layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
},
"ownFile-web": {
"type": "File",
"fileName": "d:\\home\\LogFiles\\Nlog\\nlog-own-${shortdate}.log",
"layout": "${longdate}|${event-properties:item=EventId_Id}|${aspnet-request-headers}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}"
}
},
"rules": [
{
"logger": "*",
"minLevel": "Trace",
"maxLevel" : "Trace",
"writeTo": "allfile"
},
{
"logger": "Microsoft.*",
"maxLevel": "Info",
"final": "true"
},
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "ownFile-web"
}
]
}
and here is how I’m wiring it up in code
public void ConfigureServices(IServiceCollection services)
{
var section = Configuration.GetSection("NLog");
var logconfiguration = new NLogLoggingConfiguration(section);
var splunkTarget = new SplunkHttpEventCollector
{
IgnoreSslErrors = true,
Token ="we dont need to know this :) ",
ServerUrl = new Uri("https://asplunkhec.endpoint"),
Layout = "${message} ${exception:format=tostring}",
Name = "Splunk",
ContextProperties =
{
new TargetPropertyWithContext("host","${machinename}"),
new TargetPropertyWithContext("threadid","${threadid}"),
new TargetPropertyWithContext("logger","Nlog"),
new TargetPropertyWithContext("loggedfrom","${logger}"),
new TargetPropertyWithContext("headers","${aspnet-request-headers}"),
new TargetPropertyWithContext("Url","${aspnet-request-url}"),
new TargetPropertyWithContext("Framework", Environment.Version.ToString()),
}
};
logconfiguration.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, splunkTarget, "*");
services.AddRazorPages();
services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddNLog(logconfiguration);
//loggingBuilder.AddNLog("NLog.config"); this works
});
}
and here are my Nuget packages
Thanks
Mark
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 19 (11 by maintainers)
NLog.Web.AspNetCore ver. 4.10 has been released that includes https://github.com/NLog/NLog.Web/pull/602
So you can now use
AddNLogWeb
like this: