I’m using NLog to log to network. From my understanding, the code and configuration outlined below should always work, no matter other parts of the project. And it indeed works fine for some configurations of my project, but for others it fails, and I can’t really figure out the exact reason.
I noticed that (in a non-working configuration) NLog throws when logging to 127.0.0.2 but fails silently (no error in log, but apparently no message is sent) for 127.0.0.1. Since both addresses ought to behave identically I’m left stumped. I’m using Sentinel as log viewer.
How is it possible that NLog fails despite having the exact same code and configuration, and how come it behaves differently for 127.0.0.1 and .2?
Code:
internal class Program
{
protected static Logger logger = LogManager.GetLogger ("Console.Main");
private static void Main (string[] args)
{
logger.Trace ("Initializing platform");
NLog.config:
<targets>
<target name="viewer"
xsi:type="NLogViewer"
address="udp://127.0.0.2:9999"/>
<target name="file"
xsi:type="File"
layout="${longdate} ${logger} ${message} ${exception}"
fileName="${basedir}/logs/logfile.txt"
keepFileOpen="false"
encoding="iso-8859-2" />
<target xsi:type="ColoredConsole"
layout="${logger} ${message} ${exception}"
name="console"/>
</targets>
<rules>
<logger name="*"
minlevel="Trace"
writeTo="viewer" />
<logger name="*"
minlevel="Trace"
writeTo="file" />
<logger name="*"
minlevel="Info"
writeTo="console" />
</rules>
Internal logfile for 127.0.0.2:
2011-10-11 12:05:08.4364 Error Target exception: System.Net.Sockets.SocketException (0x80004005): No such host is known
at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6)
at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
at NLog.Internal.NetworkSenders.UdpNetworkSender..ctor(String url)
at NLog.Internal.NetworkSenders.NetworkSender.Create(String url)
at NLog.Targets.NetworkTarget.NetworkSend(String address, Byte[] bytes)
at NLog.Targets.NetworkTarget.Write(LogEventInfo logEvent)
at NLog.LoggerImpl.Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
Found it. All configurations were referencing the same DLL, but some of them used one from GAC instead of the one hinted at. Manually copying the DLLs and rewriting project references fixed the issue.
Why doesn’t VS give a highest level warning on missing DLLs …