Narkha
Narkha

Reputation: 1197

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:

There is some more?

Upvotes: 6

Views: 5143

Answers (2)

Derek
Derek

Reputation: 8753

There are no other overrides. You are out of luck.

Your second example looks nice.

When I look in Visual Studio Error() has 42 different signatures

ErrorException() only has one.

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

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

Upvotes: 0

Julian
Julian

Reputation: 36700

Update,

This is possible since NLog 4.0

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

See news post - consistent logging of exceptions

Upvotes: 5

Related Questions