0

My ASP.NET web application's back-end is throwing an exception while in production (but not when debugging locally).

Ideally, I'd like to output the exception to a text file which can then be read. This is what I've tried so far:

catch (Exception ex)
{
    string exceptionString = ex.ToString();
    string errorLogPath = HttpContext.Current.Server.MapPath("~/Server/LogFiles");

    StreamWriter sw = new StreamWriter(errorLogPath + '\\' + "logs.txt", true);

    sw.WriteLine(exceptionString);
    sw.Close();
}

When I go to the logs.txt file there's nothing there. I'm pretty sure that there's an Exception being caught, so I don't see why it's not been written to the text file. Perhaps my code is wrong?

What are the best ways to view back-end Exceptions thrown in production without attaching a debugger?

ataravati
  • 8,891
  • 9
  • 57
  • 89
full_prog_full
  • 1,139
  • 10
  • 17
  • 1
    Does the user account under which the app pool is running have privileges to write to the file system in that location? If not, your attempt to writing to a log file might be throwing another exception – user469104 Dec 01 '15 at 18:35
  • 1
    **If** this code is hit, which is impossible for us to tell, chances are this code throws another exception, namely that it cannot write to the specified directory. Don't reinvent logging, use a logging library. Just search for "asp.net error handling" to find plenty of approaches. – CodeCaster Dec 01 '15 at 18:35
  • 4
    Have a look at [ELMAH](http://www.asp.net/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/logging-error-details-with-elmah-cs), or use a logging tool like log4net or an abstract logging tool like common.logging – KiwiPiet Dec 01 '15 at 18:37
  • If you just wanna view the exception, just send the exception to an Error view. Why do you want to write it in a log file? – ataravati Dec 01 '15 at 18:43
  • why don't you try refactoring the code and wrap your streaming around a `using (StreamWriter sw = new StreamWriter(errorLogPath + "\\" + logs.txt". true)) { }` also pay attention to how you are using `"\\"` that should be a Literal "@" if you want to use single backslash `"\"` there is a difference between `"'\\'"` and `"\\"` also add code to `Flush` the streamwriter so that you can immediately see the error being written to the log file when you go to open it – MethodMan Dec 01 '15 at 18:44
  • I totally agree with @KiwiPiet. ELMAH is a fantastic tool for logging exceptions and even logs unhandled exceptions. – heymega Dec 01 '15 at 18:45
  • You might also try writing to a database table instead of using a file. This eliminates file permission issues, locked file issues, and other, unwanted issues. This has worked great for me. – Brian Payne Dec 01 '15 at 18:50
  • Why not have custom errors on, and a custom error page and check the Windows event logs afterwards? The exception handler of yours is also not thread safe, and also swallows the exception. – Oguz Ozgul Dec 01 '15 at 19:00
  • Use log4net or NLog for logging. Those are well tested. I think your issues is permission to that directory ! – Shyju Dec 01 '15 at 19:13

1 Answers1

0

My question has been solved.

  • The log writing issue was indeed caused by a lack of permissions which would not allow the StreamWriter to write on the directory. This guide helped for updating permissions. As a side note: the exception being thrown was also caused by a lack of permissions.

  • For future reference: Elmah seems really useful to allow easy reading of Exceptions thrown in production. You can even get the familiar yellow screen of death. It can be added really easily. This guide was very helpful for adding Elmah to my MVC project.

Community
  • 1
  • 1
full_prog_full
  • 1,139
  • 10
  • 17
  • In regards to ELMAH, there's actually quite a lot of additional features not described in the guide you're linking to. This [ELMAH tutorial](http://blog.elmah.io/elmah-tutorial/) should be more complete and the [ELMAH documentation](https://code.google.com/p/elmah/w/list) is quite good as well. – ThomasArdal Dec 03 '15 at 11:58