NLog arguments for error message

I am looking for a way to record 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 happened to me

 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 no one seems as simple as calling logger.Error () At the moment, the first option is my support, despite the formatting of the string, which may not be used:

  • The second option does not use layout options to exclude .
  • the third one creates two journal entries and can be confusing.
  • The fourth option is much less clear.

Is there a few more?

+6
source share
2 answers

Refresh

This is possible since NLog 4.0

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

See News Entry - Sequential Exception Logging

+3
source

There are no other overrides. You were unlucky.

The second example looks good.

When I look in Visual Studio Error() , there are 42 different signatures

ErrorException() has only one.

From the following link you can see this: (click on the link "Error" and "ErrorException")

http://nlog-project.org/documentation/v2.0.1/html/AllMembers_T_NLog_Logger.htm

0
source

Source: https://habr.com/ru/post/953925/


All Articles