What does an MVC baseController with logging look

2019-07-31 18:19发布

问题:

I would like to set up a base controller for my MVC3 application. Something that my other controllers would inherit from. What I want to do is implement logging. I saw this following example:

public class baseController : Controller
{

     protected override void OnException(ExceptionContext filterContext)
     {
         filterContext.ExceptionHandled = true;
         this.View("Error").ExecuteResult(this.ControllerContext);
     }

}

But I am not sure what to log and how to log it. Can someone give me some advice on what the error handling in a base controller looks like. I hope someone can help. thank you.

回答1:

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.)

Here's the example: add this to your Global.asax:

protected void Application_Error(object sender, EventArgs e)
{
     Exception exception = Server.GetLastError();
     Response.Clear();

     //Do your logging here.
     //Redirect to an appropriate error page.
}

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:

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();
    }
}

Then, in your Application_Error method (that we defined above) just call:

new Log().WriteErrorMessage("Global error has occurred.", Request.Url.ToString(), exception);


回答2:

If you want to log unhandled exceptions, you are probably better off handling that at the application level. ELMAH is a great place to start, or just use the built-in heartbeat handling.

Insofar as logging stuff, the best bet is to use something like log4net to handle the details.