I have a c# controller
and inside a method I have code with a try catch
statement:
My controller
has a top level class attribute LogAttribute
:
public class LogAttribute : HandleErrorAttribute
{
public ILogManager Logger { get; set; }
public override void OnException(ExceptionContext filterContext)
{
Exception exception = filterContext.Exception;
filterContext.ExceptionHandled = true;
/* start log error */
var logHelper = new Helpers.LogHelper (this.Logger);
Guid errorUid = Guid.NewGuid();
// call log4net
logHelper.LogErrorString(errorUid, exception);
/* end log error */
}
}
[Log]
public class TestController
{
public JsonResult TestMethod(){
try{
//...some code here
List<string> list = null;
//Line 65 force expection Object reference not set to an instance of an object
list.count();
}
catch(Exception ex) {
// ... do some code here
throw ex; // Line 82
}
return null;
}
}
The Log4Net
logging is working fine but when I debug just after the catch
and see the Exception ex
I can see that the StackTrace
line is the 65 as the exception line, and this is totally correct because is where the exception happened. But when Log4Net
logs the error it shows as lines 82 that is exactly where I manually trhow the exception.
** UPDATE **
I just checked that the excepction
StackTrace
is correct in theCatch
statement but exactly when theMVC
Log attribute OnException handler catch that then thestacktrace
is being reset. So is problem of theOnException
method.Any clue?