In this article I will explain with an example, how to write a Log File in ASP.Net MVC.
 
 
 

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.
Then, the raised Exception is caught in the Catch block and the LogError function is called.
Inside the LogError function, the ErrorLog Text file path is referenced using Server.MapPath function and StreamWriter class object is created where we pass the path of file.
Finally, using the StreamWriter class object the details of the exception (error) are written to an ErrorLog Text file along with current date and time.
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/yyyyhh: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

Write a Log File in ASP.Net MVC
 
 

Downloads