0
  • When customErrors mode ="Off", it shows custom error page and save exception in the log file.

  • When customErrors mode ="On", it shows custom error page but does not create any log file.

What is the issue?

Code:

protected void Application_Error(object sender, EventArgs e)
            {
     Exception filterContext = Server.GetLastError();
                //Server.ClearError();
                StringBuilder builder = new StringBuilder();
                builder
                    .AppendLine("start")
                    .AppendLine(DateTime.Now.ToString()+filterContext.Source)
    SendMail("****@gmail.com", "Error Details", builder.ToString());
string filePath = Server.MapPath("~/App_Data/Error_" + 
DateTime.Now.ToString("dd_MM_yyyy") + ".log");
        using (StreamWriter writer = System.IO.File.AppendText(filePath))
        {
            writer.Write(builder.ToString());
            writer.Flush();
        }
    }

and my custom error page

 public ActionResult NotFound()
            {
                return View();
            }

            public ActionResult Error()
            {
                return View();
            }

how to store log into text file

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Thirupathi cst
  • 127
  • 2
  • 10
  • Do I missing something here but there is no code that saves to file and you expect log to be in file. – Renatas M. Jul 18 '19 at 11:12
  • string filePath = Server.MapPath("~/App_Data/Error_" + DateTime.Now.ToString("dd_MM_yyyy") + ".log"); using (StreamWriter writer = System.IO.File.AppendText(filePath)) { writer.Write(builder.ToString()); writer.Flush(); } – Thirupathi cst Jul 18 '19 at 11:13
  • 2
    I'd strongly encourage you to utilize a logging component such as NLog for this purpose. Rather than rolling your own logic, utilize something that's already been well developed and tested. Available in NuGet. [https://nlog-project.org/](https://nlog-project.org/) – DBro Jul 18 '19 at 11:19
  • 2
    Try to use 'log4net' instead rolling out your own logic. Why to reinvent the wheel? – TheKingPinMirza Jul 18 '19 at 11:28
  • I will second the previous two comments. They give you additional features for managing the lifetime of logs and also sending logs to a central location. The security group usually requires this. – Rogala Jul 18 '19 at 11:31
  • Possible duplicate of [How to log Application Errors when customErros is set to On?](https://stackoverflow.com/questions/4290676/how-to-log-application-errors-when-customerros-is-set-to-on) – nilsK Jul 18 '19 at 11:48

0 Answers0