Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Seems like you're looking for some kind of base-level application error handling. Why not add the definition for the Application_Error method in your Global.asax file. This will catch any un-handled exceptions that crop up in your application (from controllers, or other libraries or views etc.)</p> <p>Here's the example: add this to your Global.asax:</p> <pre><code>protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); Response.Clear(); //Do your logging here. //Redirect to an appropriate error page. } </code></pre> <p>If you're wondering about what to log, there's plenty of information within the exception object that you've got access to inside this method. I usually write a class which writes out some of this information to a text file. Here's an example (in a class called Log) - it's not the most comprehensive method and I'm sure more information can be extracted from the exception object but:</p> <pre><code>public class Log { private StreamWriter _writer; public void WriteErrorMessage(string errorMessage, string pageUrl, Exception e) { _writer = new StreamWriter("LOG_FILE_OUTPUT_PATH_HERE.txt", true); StringBuilder fullError = new StringBuilder(); fullError.AppendLine("Error log: " + DateTime.Now); fullError.AppendLine(errorMessage); fullError.AppendLine("Error raised on: " + pageUrl); fullError.AppendLine("Associated exception message: " + e.Message + "\n" + e.InnerException); fullError.AppendLine("Exception class: " + e.GetType().ToString()); fullError.AppendLine("Exception source: " + e.Source.ToString()); fullError.AppendLine("Exception method: " + e.TargetSite.Name.ToString()); fullError.AppendLine(); _writer.WriteLine(fullError); _writer.Flush(); _writer.Close(); } } </code></pre> <p>Then, in your Application_Error method (that we defined above) just call:</p> <pre><code>new Log().WriteErrorMessage("Global error has occurred.", Request.Url.ToString(), exception); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload