bingo
bingo

Reputation: 2308

How can I keep a log of NullReferenceException errors that occur in a razor view?

Occasionally a developer will not instantiate a required part of a viewmodel and the corresponding razor view will throw a NullReferenceException error. From there, customErrors redirects to a generic server error 500 view.

I want to log that error in a repository so these incidents can be discovered and fixed. Is there part of the framework that can handle this?

Upvotes: 0

Views: 25

Answers (1)

Kevin B Burns
Kevin B Burns

Reputation: 1067

You can use logging frameworks like ELMAH or LOGNet, which are great tools but sometimes you just want to shove it into a database or send a simple email. To manage something like that I have found the best solution is to create a base controller that all of your controllers will inherit from and override the following:

protected override async void OnException(ExceptionContext filterContext)
{
}

Anytime an exception handles in the view or in the controller it will hit here before doing anything. You can still even have other frameworks work with this also.

Upvotes: 1

Related Questions