How to log exceptions with network objects in NLog

I use the NLog logging framework and try to get exception and stacktrace information displayed in any UDP logger application, such as Sentinel and Log2Console , but can only display part of the log message. File output works well, since most examples do just that, so the problem is with using network targets using NLog.

Bonus if custom format can be applied to internal exceptions and stacktrace, but this is not required. Exception.ToString () will go a long way.

Note on code example: With Log2Console I found articles on how to send an exception as a separate log entry. Although this worked, I was not happy with the solution.

Example exception registration code:

Logger Log = LogManager.GetCurrentClassLogger(); try { throw new InvalidOperationException("My ex", new FileNotFoundException("My inner ex1", new AccessViolationException("Innermost ex"))); } catch (Exception e) { Log.ErrorException("TEST", e); } 

Example NLog.config:

 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets async="true"> <!-- Send by UDP to Sentinel with NLogViewer protocol --> <target name="network" xsi:type="NLogViewer" address="udp://192.168.1.3:9999" layout="${message}${onexception:inner=${newline}${exception:format=tostring}}" /> <!-- Send message by UDP to Log2Console with Chainsaw protocol --> <target name="network2" xsi:type="Chainsaw" address="udp://192.168.1.3:9998" appinfo="Grocelist"/> <!-- Send exception/stacktrace by UDP to Log2Console with generic network protocol --> <target name="network2ex" xsi:type="Network" address="udp4://192.168.1.3:9998" layout="${exception:format=ToString}" /> <target name="logfile" xsi:type="File" layout="${longdate}|${level:uppercase=true}|${logger}|${message}|${exception:format=tostring}" createDirs="true" fileName="${basedir}/logs/${shortdate}.log" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="logfile" /> <logger name="*" minlevel="Debug" writeTo="network" /> <logger name="*" minlevel="Debug" writeTo="network2" /> <logger name="*" minlevel="Warn" writeTo="network2ex" /> </rules> </nlog> 

Some links:

Edit: After searching a little more, this seems to be a limit to the end of NLog. A recent patch seems to exist: log4jxmlevent does not display Exception

Edit2: I restored NLog with the patch, but it did not seem to help in the Sentinel or Log2Console applications. I may have to try log4net to make sure that these applications really support what I'm trying to achieve.

Edit3: I am currently using string.Format () to join and format the message and the exception text. It works well, but I'm not looking here.

+7
source share
6 answers

You can also extend NLog to include exceptions for online logging.

Create an advanced layout:

 [Layout("Log4JXmlEventLayoutEx")] public class Log4JXmlEventLayoutEx : Log4JXmlEventLayout { protected override string GetFormattedMessage(LogEventInfo logEvent) { string msg = logEvent.Message + " ${exception:format=Message,Type,ToString,StackTrace}"; msg = SimpleLayout.Evaluate(msg, logEvent); LogEventInfo updatedInfo; if (msg == logEvent.Message) { updatedInfo = logEvent; } else { updatedInfo = new LogEventInfo( logEvent.Level, logEvent.LoggerName, logEvent.FormatProvider, msg, logEvent.Parameters, logEvent.Exception); } return base.GetFormattedMessage(updatedInfo); } } 

Create a goal that uses this layout.

 [Target("NLogViewerEx")] public class NLogViewerTargetEx : NLogViewerTarget { private readonly Log4JXmlEventLayoutEx layout = new Log4JXmlEventLayoutEx(); public override Layout Layout { get { return layout; } set {} } } 

Update NLog.config file:

 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <extensions> <add assembly="Assembly.Name.That.Contains.Extended.Target"/> </extensions> <targets> <target name="logViewer" xsi:type="NLogViewerEx" address="udp://localhost:7071"> </targets> ... </nlog> 
+3
source

I had this problem and I just upgraded my NLog nuget package to version 2.0.1.2

Now I have exceptions going to Log2Console just fine.

+2
source

A few years later, and this is pretty trivial, try adding

includeSourceInfo = "true"

to your target file, so it looks like this:

  <target name="viewer" xsi:type="NLogViewer" includeSourceInfo="true" address="udp://127.0.0.1:9999" /> 

Gives you information about the source file, line, class, and method.

+2
source

Have you tried the latest snapshot of the Chainsaw developer? It will display stack traces and supports log4net / UDP applications, and according to NLog you can also use it: http://nlog-project.org/wiki/Chainsaw_target

Try the last developer snapshot, it has a lot of features: http://people.apache.org/~sdeboy

+1
source

Just download and create the latest sources (NLog-Build-2.0.0.2007-0-g72f6495) from GitHub: https://github.com/jkowalski/NLog/tree/ This problem has been fixed there by the NLog developer.

+1
source

In your NLog.config, change the target as shown below.

 <target name="file" xsi:type="File" fileName="log.txt" layout="${longdate}:${message} ${exception:format=message,stacktrace:separator=*}" /> 

The part you are looking for

 ${exception:format=message,stacktrace:separator=*} 

For more information about this image here .

+1
source

All Articles