NLog version: 4.7.8
Platform: .Net 4.7.2
Current NLog config (xml or C#, if relevant)
<?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="logfile" xsi:type="File" fileName="${basedir}\ICXForwarder.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
The following code:
``` c#
private void ConnectCallback(IAsyncResult ar)
{
try
{
_socket.EndConnect(ar);
Receive();
}
catch (Exception ex)
{
_logger.Error(ex);
_logger.Info("111");
_logger.Error(ex, $"Client {Hostname} ({Port}) connect callback error");
_logger.Info("222");
_logger.Error(ex, "connect callback error", Hostname, Port);
_logger.Info("333");
ExceptionEvent(this, new ExceptionEventArgs() { Exception = ex });
}
}
Produces this:
2021-03-04 17:02:54.6203|ERROR|ICXClient(10.40.30.1)|System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.40.30.1:18000
at System.Net.Sockets.Socket.InternalEndConnect(IAsyncResult asyncResult)
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at ICXForwarder.ICXClient.ConnectCallback(IAsyncResult ar)
2021-03-04 17:02:54.6343|INFO|ICXClient(10.40.30.1)|111
2021-03-04 17:02:54.6343|ERROR|ICXClient(10.40.30.1)|Client 10.40.30.1 (18000) connect callback error
2021-03-04 17:02:54.6343|INFO|ICXClient(10.40.30.1)|222
2021-03-04 17:02:54.6343|ERROR|ICXClient(10.40.30.1)|Client "10.40.30.1" (18000) connect callback error
2021-03-04 17:02:54.6343|INFO|ICXClient(10.40.30.1)|333
```
The exception should be logged in all cases, not just the first one.
Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!
The default Layout in NLog 4.7 is like this:
${longdate}|${level:uppercase=true}|${logger}|${message}
NLog has some small tricks, where ${message} does a fallback to LogEvent-Exception, when no actual LogEvent-message could be found.
But if you override the default Layout to include ${exception:format=tostring}, then you might get the wanted output:
<?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="logfile" xsi:type="File"
fileName="${basedir}\ICXForwarder.log"
layout="${longdate}|${level:uppercase=true}|${logger}|${message}${exception:format=tostring}"
/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
NLog 5.0 will change the default layout, so it will always include exception-details. See also #3637 (Since NLog prides itself on having good default values, but here it failed on first attempt).
Alright, thanks.
@snakefoot: ${message}${exception:format=tostring} misses a space between the message and the exception, and adding that space makes it so it's always present no matter what even when there's no exception.
Is there a trick to handle that or do I have to wait Nlog 5.0 ?
You can try this:
${message:withexception=true:exceptionSeparator= }
See also: https://github.com/NLog/NLog/wiki/Message-Layout-Renderer
Thanks that works.
Most helpful comment
The default Layout in NLog 4.7 is like this:
NLog has some small tricks, where
${message}does a fallback to LogEvent-Exception, when no actual LogEvent-message could be found.But if you override the default Layout to include
${exception:format=tostring}, then you might get the wanted output:NLog 5.0 will change the default layout, so it will always include exception-details. See also #3637 (Since NLog prides itself on having good default values, but here it failed on first attempt).