I've added NLog using nuget to a project and added NLog.config. I'm running the debugger and getting a NullReferenceException
due to the fact LogManager.Configuration
is null:
LogManager.Configuration.AddTarget("sentinel", sentinalTarget);
This line of code runs in a static constructor.
LogManager.ThrowExceptions
was false so I suspect an issue with the configNLog.config contents:
<?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="false">
<variable name="appName" value="YourAppName" />
<targets async="true">
<target xsi:type="File"
name="default"
layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}"
fileName="${specialfolder:ApplicationData}\${appName}\Debug.log"
keepFileOpen="false"
archiveFileName="${specialfolder:ApplicationData}\${appName}\Debug_${shortdate}.{##}.log"
archiveNumbering="Sequence"
archiveEvery="Day"
maxArchiveFiles="30"
/>
<target xsi:type="EventLog"
name="eventlog"
source="${appName}"
layout="${message}${newline}${exception:format=ToString}"/>
<target xsi:type="NLogViewer"
name="viewer"
address="udp://127.0.0.1:9999"/>
<target xsi:type="OutputDebugString" name="DbWin" layout="Log4JXmlEventLayout">
<layout xsi:type="Log4JXmlEventLayout" />
</target>
</targets>
<rules>
<logger name="*" writeTo="default" minlevel="Info" />
<logger name="*" writeTo="eventlog" minlevel="Error" />
<logger name="*" minlevel="Debug" writeTo="viewer" />
<logger name="*" minlevel="Trace" writeTo="DbWin" />
</rules>
</nlog>
Update
I discovered the source. The issue occurs only when running unit tests. Running the full application (web app) the issue is not there. I copied the NLog.config file to the unit test home directory. The issue is still there when running unit tests.
DeploymentItemAttribute
to test classes (more info)Like this:
[TestClass]
[DeploymentItem("ProjectName\\NLog.config")]
public class GeneralTests
Alternatively, you can load the configuration programmatically:
LogManager.Configuration = new XmlLoggingConfiguration(@"c:\path\to\NLog.config")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With