What does the MVC baseController with logging look like?

I would like to configure the base controller for my MVC3 application. What my other controllers inherited. What I want to do is do the logging. I saw the following example:

public class baseController : Controller
{

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

}

But I'm not sure what to register and how to register it. Can anyone give me some tips on what error handling looks like in the base controller. I hope someone can help. Thank.

+1
source share
2 answers

, - . Application_Error Global.asax. , ( ..).

: 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.
}

, , , . , . ( Log) - , , , :

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

Application_Error ( ) :

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

, , , . ELMAH - .

- log4net .

0

All Articles