Configuring NLog E-Mail Message Body Output - nlog

I am using NLog v2 Beta to log a VB.NET .DLL which creates and sends messages to a third party service. I have it working perfectly when logging to a file, but now want it to also e-mail me the errors it catches automatically. The following is the relevant bit of my NLog.config file:
<?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="papercut" xsi:type="BufferingWrapper" bufferSize="100">
<target xsi:type="PostFilteringWrapper" defaultFilter="level >= LogLevel.Debug">
<target xsi:type="Mail"
name="papercut"
subject="Your app has errors"
to="ToAddress#Domain.com"
from="FromAddress#Domain.com"
smtpServer="127.0.0.1"
smtpPort="25"
body={longdate}|{message} />
</target>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="papercut" />
</rules>
</nlog>
By default it will just list the logged message in the e-mail body. I want to preceed this with the date / time logged, so have been playing around with the body= part to no avail (it either doesn't evaluate the variables properly or crashes NLog). Can somebody please give me a pointer as to how to configure NLog to do this?

seems like you are missing $ in the body configuration and that might be the reason that NLog is crashing..
body = "${longdate}| ${message}"
more info regarding Mail target
https://github.com/NLog/NLog/wiki/Mail-target
also you can enable logging for NLog errors itself as below..
<nlog
xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwExceptions="true"
internalLogFile="Nloglog.log"
internalLogLevel="Warn"
>
.....
</nlog>

Related

Sentry.NLog not logging to Sentry

Trying to get Sentry wired up via NLog, and not having much luck.
Packages:
<package id="Sentry" version="3.0.5" targetFramework="net462" />
<package id="Sentry.NLog" version="3.0.5" targetFramework="net462" />
Both latest.
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"
autoReload="true"
throwExceptions="true"
internalLogLevel="Debug"
internalLogFile="c:\temp\nlog-internal.txt"
internalLogToConsole="true"
throwConfigExceptions="true">
<extensions>
<add assembly="Sentry.NLog" />
</extensions>
<targets>
<target xsi:type="Sentry"
name="sentry"
dsn="https://secret"
environment="test"
includeEventProperties="True"
layout="${message}"
breadcrumbLayout="${message}"
minimumBreadcrumbLevel="Debug"
ignoreEventsWithNoException="False"
includeEventDataOnBreadcrumbs="False"
includeEventPropertiesAsTags="True"
minimumEventLevel="Error" />
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="sentry" />
</rules>
</nlog>
Things I've checked:
DSN. Copied and pasted from the Sentry portal, so i know it's correct
No filters on 'environments' in Sentry.
Nlog log file shows no errors. In fact in shows Sentry being wired up.
Added other targets to NLog (e.g console, file, etc), they work fine.
Any ideas?
Thanks
Did you opt out of InitializeSdk?
https://github.com/getsentry/sentry-dotnet/blob/6ce7f933f64bd517677b5e837d6c7e8cc37b4217/src/Sentry.NLog/SentryTarget.cs#L148-L155
Could that be the issue?
Unless you're using another integration which already initialized Sentry, of if you call SentrySdk.Init yourself though.
But by default the DSN alone should be enough to init the SDK.
You can set the SDK to debug mode to see what's happening.
There's a sample project in the repo:
https://github.com/getsentry/sentry-dotnet/tree/main/samples/Sentry.Samples.NLog
Sentry is sending data over the network, and propbably has an artificial delay to optimize for batching and to reduce network-traffic.
Did you remember to flush?, and wait for the data to arrive at destination.

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? ;)

I dont want nlog to create log file for every millisec

Im using the following config . This creates log file for every milli sec.
I want only one log file per execution and it should be time stamped
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="C:\log\log- ${date:format=dd/MM/yyyy HH\:mm\:ss}.txt"></target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
The only way to do this is to set the fileName programmatically.
E.g.
var logfileTarget = NLog.LogManager.Configuration.FindTargetByName<FileTarget>("logfile");
logfileTarget.FileName = "filename_with_date_and_ext"; //you can use layout renderers here.
See API docs
Have submitted a PR for the processinfo-layout-renderer, so it can output process-start-time in the wanted format. But it only supports local-time
fileName="C:\log\log-${processinfo:property=StartTime:format=yyyy-MM-dd_HHmmss}.log"

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.

Send logs in separate emails

When using the built-in nlog Mail target, I am receiving multiple error logs grouped into one email. There seems to be some kind of buffering occurring.
I want each log to be sent separately.
Example of my log.config file:
<?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 async="true">
<target
xsi:type="Mail"
name="email"
subject="MyProject [${level:uppercase=true}]: ${message}"
body="${message}"
useSystemNetMailSettings="True"
html="True"
addNewLines="True"
replaceNewlineWithBrTagInHtml="True"
to="xyz#mydomain.co.za"
from="no-reply#mydomain.co.za"/>
</targets>
<rules>
<logger name="*" level="Error" writeTo="email" />
</rules>
</nlog>
How can I do this?
Sorry, but this is impossible by default, and you can verify this by link: https://github.com/NLog/NLog/blob/master/src/NLog/Targets/MailTarget.cs#L282-L289.
But you always can create own NLog target https://github.com/nlog/nlog/wiki/How-to-write-a-Target, based on MailTarget.

Resources