NLog with NLog.Targets.Syslog - Object reference error - nlog

I'am trying to log to the papertrail systems using NLog and NLog.Targets.Syslog and somehow NLog fails to load the target from the NLog.config when I call
Logger logger = LogManager.GetCurrentClassLogger();
and throws the exception
Object reference not set to an instance of an object.
at NLog.Targets.Syslog.Settings.Rfc3164Config..ctor()
at NLog.Targets.Syslog.Settings.MessageBuilderConfig..ctor()
at NLog.Targets.Syslog.SyslogTarget..ctor()
Stacktrace
at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
at NLog.Config.XmlLoggingConfiguration..ctor(String fileName, Boolean ignoreErrors, LogFactory logFactory)
at NLog.LogFactory.LoadLoggingConfiguration(String configFile)
at NLog.LogFactory.get_Configuration()
at NLog.LogFactory.GetLogger(LoggerCacheKey cacheKey)
at NLog.LogFactory.GetLogger(String name)
at NLog.LogManager.GetCurrentClassLogger()
at NLogPaperTrail.Controllers.HomeController.Index() in I:\Temp\Proj\tempsolution\NLogPaperTrail\NLogPaperTrail\Controllers
This is the NLog.config
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" ThrowExceptions = "true" >
<extensions>
<add assembly="NLog.Targets.Syslog" />
</extensions>
<targets>
<target name="syslog" xsi:type="Syslog" syslogserver="host.papertrailapp.com" port="5677" facility="Local7"/>
</targets>
<rules>
<logger name="*" minLevel="Trace" appendTo="syslog"/>
</rules>
</nlog>
Can anyone help or provide your suggestions?

Also reported here:
https://github.com/NLog/NLog/issues/1894
And resolved here (SysLog Target Ver. 3.0.1)
https://github.com/graffen/NLog.Targets.Syslog/pull/53

Related

Configuration NLog.config file for using on Azure portal

I've applied ASP.NET core 3.1 in my web application and I've used NLog for logging.
In local host I set the internalLogFile and fileName like this
internalLogFile="E:\Archives\Projects\Alpha\Alpha.Web.App\internal_logs\internallog.txt"
fileName="E:/Archives/Projects/Alpha/Alpha.Web.App/logs/${shortdate}_logfile.txt"
in nlog.config file. How can I set the paths for using in Azure?
<?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="Trace"
internalLogFile="E:\Archives\Projects\Alpha\Alpha.Web.App\internal_logs\internallog.txt">
<targets>
<target name="logfile" xsi:type="File"
fileName="E:/Archives/Projects/Alpha/Alpha.Web.App/logs/${shortdate}_logfile.txt"
layout="${longdate} ${level:uppercase=true} ${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
nlog.config file is in this path:
https://{appname}.scm.azurewebsites.net/dev/wwwroot/nlog.config
I have used ${basedir} and changed values of internalLogFile and fileName to
internalLogFile="${basedir}/internal_logs/internallog.txt">
and
fileName="${basedir}/logs/${shortdate}_logfile.txt"
Now I access to these files in bin folder in my project.

Can layout render(s) be used for FileName(s)?

Can layout render(s) (processname to be exact) be used for FileName(s)?
Note,
the internalLogFile (and INTERNAL.log value)
and
fileName="${processname}.NLog.${shortdate}.PeanutButter.log"
values below.
<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="${processname}.NLog.INTERNAL.log"
internalLogLevel="Trace" >
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="target1" fileName="${processname}.NLog.${shortdate}.PeanutButter.log"
layout="${date}|${level:uppercase=true}|${logger}|${environment-user:userName=true:domain=true}|****|${message} ${exception:format=toString,Data}|${all-event-properties}" />
<target xsi:type="Console" name="target2"
layout="${date}|${level:uppercase=true}|${logger}|${message} ${exception:format=toString,Data}|${all-event-properties}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="target1,target2" />
</rules>
</nlog>
With the above, I'm getting a file created:
${processname}.NLog.INTERNAL.log
(literally, that is the filename)
and no files at all named:
*PeanutButter.log
where * is a wild card search.
Imported packages below.
<ItemGroup>
<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" />
</ItemGroup>
Can layout render(s) (processname to be exact) be used for FileName(s)?
Filetarget
Yes, it's fully supported for the file target.
See this simple example from the Filetarget docs:
Per-level log files
Single File target can be used to write to multiple files at once.
The following configuration will cause log entries for each log level
to be written to a separate file, so you will get:
Trace.log
Debug.log
Info.log
Warn.log
Error.log
Fatal.log
<target name="file" xsi:type="File"
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
fileName="${basedir}/${level}.log" />
Internal logger
${processname}.NLog.INTERNAL.log
(literally, that is the filename)
It's not supported for the internal logger filename!
From the Internalloger docs
internalLogFile
Note: only a few layouts are supported, as the internal log needs to be as stable as possible.
NLog 4.6+: Supported renderers (without options): ${currentdir}, ${basedir}, ${tempdir}
NLog 4.6+: Environment Variables are also supported: e.g. %appdata%
If the internal logger would fail because of (complicated) layout renderers, where should it log that then? ;)

NLog with AzureTableStorage

Trying to insert logs into Azure table storage, while using the extension of NLog.Extensions.AzureTableStorage in Nlog.config file , i am getting error under target type for azure table storage.
Error : - This is invalid xsi:type http://www.nlog-project.org/schemas/NLog.xsd:AzureTableStorage
FYI - I am using latest version Nlog latest version 4.4.11 & i added extension of Nlog.extensions.azuretablestorage(Nuget version 1.1.4)
Update for config file:
<extensions>
<add assembly="NLog.Extensions.AzureTableStorage"/>
</extensions>
<!-- set up a an azure storage table target -->
<targets>
<target name="AzureTableStorage" xsi:type="AzureTableStorage" PartitionKey="${date}.${logger}" RowKey="${ticks}.${guid}" ConnectionString="UseDevelopmentStorage=true" tableName="TempAzureTableStorageTargetTestsLogs" />
</targets>
This is invalid xsi:type http://www.nlog-project.org/schemas/NLog.xsd:AzureTableStorage
I assumed that you have installed NLog.Config that would automatically create the default NLog.config. Since this package references NLog.Schema which would enable Intellisense(TM) when editing NLog configuration files.
I have checked with this issue, the above message is just for warning, the logging feature would be fine. And you could remove the NLog.Config and NLog.Config packages, then the warning message would go away.
FYI - I am using latest version Nlog latest version 4.4.11 & i added extension of Nlog.extensions.azuretablestorage(Nuget version 1.1.4)
AFAIK, the latest version for NLog Azure Table Storage Target is 1.0.11. Here is my code snippet, you could refer to it:
NLog.config
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
<extensions>
<add assembly="NLog.Extensions.AzureTableStorage"/>
</extensions>
<targets>
<target xsi:type="AzureTableStorage"
name="NLogAzureTable"
ConnectionStringKey="NLog.Azure.TableStorage.ConnectionString"
TableName="NLogTable"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="NLogAzureTable" />
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
</nlog>
App.config
<appSettings>
<add key="NLog.Azure.TableStorage.ConnectionString" value="{your-storage-account-connectionString}" />
</appSettings>
Usage:
var logger = LogManager.GetLogger(nameof(Program));
logger.Info("hello world!!!");
Result:
For more details about how to configure the NLog Azure Table Storage Target, you could refer to NLog.Extensions.AzureTableStorage. Also, you could refer to NLog for more tutorials.

NLog: LayoutRenderer cannot be found

NLog throws an exception while trying to create logger:
var configuration = new XmlLoggingConfiguration(configurationPath);
Exception message: LayoutRenderer cannot be found: 'TargetDirectory'
Logger configuration:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Extended" />
</extensions>
<variables>
<variable name="TargetDirectory"
value="D:\Internal\Logs\" />
...
<targets>
<target name="TraceLog"
xsi:type="File"
fileName="${TargetDirectory}${TraceLogname}"
...
</nlog>
NLog.Extended.dll is presented in bin folder
D:\Internal\Logs\ folder exists on HDD but I don't think that it is the problem's root
But what?
Change value="D:\Internal\Logs\"
to:
value="D:/Internal/Logs/"
and
fileName="${TargetDirectory}${TraceLogname}
to:
fileName="${TargetDirectory}/${TraceLogname}

NLog 4.0 ignores final attribute on a rule

My application is using NLog configured with NLog.config shown below. It also uses RavenDB database which by default uses active NLog settings for logging.
It produces a lot of DEBUG and INFO messages that pollute the log. I do want to log ERROR and WARN messages. All of the records created by RavenDB come from namespaces that start with Raven.
I created the rules as shown below. Basically there is a final rule that prevents INFO/DEBUG messages that come from Raven.* namespace to get written into the log file. Everything was working well until I upgraded the NuGet packages to NLog 4.0. Now all the RavenDB messages are written into the log file.
Is this a bug or there is some configuration change happened across the NLog versions?
<?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">
<targets>
<target name="file" xsi:type="AsyncWrapper" queueLimit="1000" overflowAction="Discard">
<target
name="file"
xsi:type="File"
layout="${longdate} - ${level:upperCase=true} - ${identity} - ${logger} - ${message}${onexception:inner=${newline}${exception:format=tostring}${newline}}"
fileName="D:/Logs/AppName/${shortdate}.log" />
</target>
</targets>
<rules>
<logger name="Raven.*" writeTo="file" minlevel="Warn" final="true" />
<logger name="*" writeTo="file" minlevel="Debug" />
</rules>
</nlog>
Apparently there was a logic change in NLog 4. It does not mark messages from Raven namespace with level below the Warn final any longer.
http://nlog-project.org/2015/06/09/nlog-4-has-been-released.html
So the rule would have to change to send messages in the Raven.* namespace with maxlevel="INFO" into a null target.

Resources