I am looking for a way to log expressive messages when I catch an exception with NLog 2.0.1, something like
try {
....
}
catch(Exception ex) {
logger.ErrorException("Error with query {0}", query, ex);
}
But NLog does not support it. Other forms that have happened to me are
logger.ErrorException( String.Format("Error with query {0}", query)) , ex);
or
logger.Error("Error with query {0} {1} {2}", query, ex.Message, ex.StackTrace);
or
logger.Error("Error with query {0}", query);
logger.ErrorException("", ex);
or
LogEventInfo ei = new LogEventInfo(LogLevel.Error, logger.Name, null,
"Error with query {0}", new object[] { query }, ex);
logger.Log(ei);
But none seems so simple as calls to logger.Error ()
At the moment, the first option is my favority despite Format a string that maybe will not be used:
There is some more?
Update,
This is possible since NLog 4.0
logger.Error(ex, "Error with query {0}", query);
See news post - consistent logging of exceptions
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