Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog arguments for errorException message

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:

  • the second option not use the layout options for exceptions.
  • the third creates two entries in the log and maybe will be confusing.
  • the fourth option is much less clear.

There is some more?

like image 855
Narkha Avatar asked Sep 16 '13 14:09

Narkha


1 Answers

Update,

This is possible since NLog 4.0

logger.Error(ex, "Error with query {0}", query);

See news post - consistent logging of exceptions

like image 190
Julian Avatar answered Oct 20 '22 19:10

Julian