0

I'm using log4net to log errors to a SQL Server database. It works ok but there is one problem. The log table in the database contains a column called Exception this never gets populated but all the other columns do. My config is setup ok, can anyone tell me why this should be happening ?

Here's the commandtext section in my web.config file

commandText value="INSERT INTO Log ([Date], [Thread], [Level], [Logger],[Message], [Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

If you want to fill the error column, you need to give the exception as argument to the log.Error method:

try{
  throw new Exception();
catch (Exception ex){
  _log.Error("error msg", ex);
}

See the ILog interface

Error(Object) Logs a message object with the Error level.

Error(Object, Exception) Log a message object with the Error level including the stack trace of the Exception passed as a parameter.

ErrorFormat(String, Object) Logs a formatted message string with the Error level.

ErrorFormat(String,Object[]) Logs a formatted message string with the Error level.

ErrorFormat(IFormatProvider, String,Object[]) Logs a formatted message string with the Error level.

ErrorFormat(String, Object, Object) Logs a formatted message string with the Error level.

ErrorFormat(String, Object, Object, Object) Logs a formatted message string with the Error level.

Community
  • 1
  • 1
Peter
  • 27,590
  • 8
  • 64
  • 84
0

You need to use the correct overload, ie

void Error(Object message,Exception exception)

As you are using this._log.ErrorFormat(ex.Message), you are not passing the exception, but just the message as a string to log4net.

sgmoore
  • 15,694
  • 5
  • 43
  • 67