In this article I will explain with an example, how to implement error logging and exception handling in ASP.Net MVC.
The errors and exceptions will be written to a Text file as it is easier to find the cause of the error as the Error Log Text File can be easily opened using the basic Notepad application in Windows.
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
Inside this Action method, an exception is raised by converting a string value to integer inside a Try-Catch block and inside the Catch block and the LogError function is called.
Inside the LogError function, the ErrorLog Text file is accessed using Server.MapPath function and then using the StreamWriter class object, the details of the exception (error) are written to an ErrorLog Text file along with current date and time.
Note: The second parameter is passed as TRUE to the StreamWriter class, so that the contents are appended to the Text file.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult RaiseException()
{
try
{
int i = int.Parse("Mudassar");
}
catch (Exception ex)
{
this.LogError(ex);
}
return View("Index");
}
private void LogError(Exception ex)
{
string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
message += string.Format("Message: {0}", ex.Message);
message += Environment.NewLine;
message += string.Format("StackTrace: {0}", ex.StackTrace);
message += Environment.NewLine;
message += string.Format("Source: {0}", ex.Source);
message += Environment.NewLine;
message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
string path = Server.MapPath("~/ErrorLog/ErrorLog.txt");
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(message);
writer.Close();
}
}
}
View
HTML Markup
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists of a Submit Button.
Submitting the Form
When the Submit Button is clicked, the exception (error) are written to an Error Log Text file along with current date and time.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("RaiseException", "Home", FormMethod.Post))
{
<input type="submit" value="Submit" />
}
</body>
</html>
Screenshot
Downloads