I am using the LogEventInfo to log both to a log file and database entry. I also want to log the stacktrace when exception is thrown to the log file. I thought I just need to set the Exception property in the LogEventInfo to the exception I want to log, but it didn't work, The log file only contains "Error my message". Where did I do wrong? Thanks.
The NLog.config is like
<target xsi:type="File" name="localLogFile" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<logger name="*" minlevel="Debug" writeTo="localLogFile" />
And the C# code
LogEventInfo logentity = new LogEventInfo(LogLevel.Error, "", "my message");
logentity.Properties["errormsg"] = msg;
logentity.Exception = new Exception("Dummy");
logger.Error(logentity);
You need also to print the exception in the layout, using ${exception}
.
So a good layout would be:
layout="${longdate} ${uppercase:${level}} ${message} ${exception}" />
Check the docs for ${exception}
for various render options.