While you could handle the error here that is probably not the best choice. The issue is that you'll likely have many places in code where you want to log errors. In general unless you intend to actually handle the error then you shouldn't be using exception handling inside individual methods. This is especially true if you need to "report it" to the user as most classes (this one included) are too low level to do that.
Since you tagged this as MVC I assume you want to report this to the view that your user is going to see. Therefore it needs to be handled in the controller action that is called.
public class MyController : Controller
{
public ActionResult MyAction ()
{
try
{
DoWork();
return View();
} catch (Exception e)
{
ReportError(e); //Your helper method to log the error
return View("Error"); //Your error view you want to show or however you "report" errors in the UI
};
}
}
But as mentioned earlier this becomes tedious as you add more controller actions. Therefore the preferred approach in MVC is to create an exception filter as fully documented here. In this approach you create a custom exception filter that does the generic error handling logic (logging in this case). Then let the exception flow on. Register the filter globally at app startup, in most cases. MVC is already configured to automatically show your error page on an unhandled exception so you don't need to do anything else. If you don't want that then you'll need to customize how errors are returned on a per-action basis. But going this route makes it harder for your actions to know when something has failed. Refer to the docs linked earlier for a complete example of how to do this.
For the exception filter itself logging is very common. In fact unless you have rolled your own then if you're using a third party logging system like Log4Net or NLog then they already have providers that can do this. You just add the appropriate logger (however that works for the system you're using) and you're done. If you did roll your own then in the exception filter simply open the DB connection and write to your error log.
public class LogErrorsExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException ( HttpActionExecutedContext context )
{
//TODO: Should probably ignore common exceptions that aren't log worthy such as 401s, 404s, etc
//Log it
using (var conn = new SqlConnection(...))
{
var cmd = new SqlCommand("your query to insert a log entry", conn);
//Set parameters here...
conn.Open();
cmd.ExecuteNonquery();
};
}
}
Note that exception filters must be error resilient. If the filter fails while trying to handle an exception you lose the original exception therefore be very careful what you do. For example in the above you are connecting to a DB but if the DB is unavailable (and that is the error) then your logger will fail as well. Make sure your filter doesn't cause its own exceptions. Additionally this will be potentially slow in error cases so consider logging async instead which is outside the scope of this question.