2
Reply

how to solve NullReferenceException exception in Log.Error

Sudipta Saha

Sudipta Saha

Jan 24 2018 2:30 PM
170
My C# code is like this
  1. protected void Application_Error(object sender, EventArgs e)  
  2. {  
  3. var ex = Server.GetLastError();  
  4. Log.Error(ex);  
  5. var httpStatusCode = (ex is HttpException) ? (ex as HttpException).GetHttpCode() : 500;  
  6. var httpContext = ((MvcApplication)sender).Context;  
  7. httpContext.ClearError();  
  8. httpContext.Response.Clear();  
  9. httpContext.Response.StatusCode = httpStatusCode;  
  10. var shouldHandleException = true;  
  11. Exception exceptionToReplace;  
  12. }  
And Log.Error(ex); throws this
 
System.NullReferenceException: Object reference not set to an instance of an object
error cause var ex = Server.GetLastError(); is null .
 
My question is how to solve this error?Any info will be helpful regarding this

Answers (2)