Link to home
Start Free TrialLog in
Avatar of Sirdots
Sirdots

asked on

Generic error page not showin when an error is encountered - asp.net c# web forms

On page load I am testing displaying an error message
page called Error.htm. I am throwing an exception when
the page loads. The error message is logged but the
Error.htm does not display. How can I make this display?
I thought by placing it in the web.config file it should
be fine.
I am using log4net to log the errors.


      protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
           
                try
                {
                    throw new System.InvalidOperationException("Logfile cannot be read-only");
                   
                }
                catch (Exception ex)
                {
                    log.Info("Page load event- default page..... ");
                    log.Error("Page load event- default page " + ex.Message) ;
                }
             }
        }

Here is the entry in my web.config file...

  <customErrors mode="RemoteOnly" defaultRedirect="Error.htm"></customErrors>
Avatar of Craig Wagner
Craig Wagner
Flag of United States of America image

Your catch block is swallowing the exception. If the exception doesn't bubble up the call stack nothing is going to happen.
log.Info("Page load event- default page..... ");
log.Error("Page load event- default page " + ex.Message) ;
throw;

Open in new window

In general, it would make more sense to have your error page be an ASP.NET page and have it log the exception. That way you don't have to have the logging code scattered throughout your application.
Avatar of Sirdots
Sirdots

ASKER

Thanks I tried the throw and it didnt work. Here is what I got below highlighting the line 301 where throw is.

Logfile cannot be read-only
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Logfile cannot be read-only

Source Error:


Line 299:                    log.Info("Page load event- default page..... ");
Line 300:                    log.Error("Page load event- default page " + ex.Message) ;
Line 301:                    throw;
Line 302:                }
Line 303:            }
ASKER CERTIFIED SOLUTION
Avatar of Craig Wagner
Craig Wagner
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sirdots

ASKER

You are very correct. It works well now after changing the mode to on