Hi, how can I get Compiler Error Message if a have pageUrl.
I'm tried using HttpWebRequest class, but haven't get result yet.
Problem that, i have collection of pages, than must execute automatically. and if page fails, create log.
thank you
Hi, how can I get Compiler Error Message if a have pageUrl.
I'm tried using HttpWebRequest class, but haven't get result yet.
Problem that, i have collection of pages, than must execute automatically. and if page fails, create log.
thank you
You can write a program to visit the pages, if the response of the request is a HTTP error than you can investigate further.
If you do not want to write your own program to detect errors originating from HTTP requests, you can use a testing framework like selenium.
Disclaimer: I do very little with ASP.NET type stuff.
ELMAH might help you a bit. It's an error logger for ASP.NET projects.
string pagetext = (new System.Net.WebClient()).DownLoadString(<url>);
//Add a better control here
if(pagetext.Contains("Server Error"))
{
`enter code here`
}
You can catch all application errors in application global class (global.asax) in Application_Error
handler.
Other way. You can catch exceptions in custom error module as well, just register you module in <httpModules>
section and implement following function there:
void context_Error(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
Exception ex = context.Server.GetLastError();
//... here goes some code
}
Thus you have to ways to catch any error. Other task is to request all pages. As I can see from your post, you've already have such solution.
Where are you trying to catch these exceptions? In your website, or in an external application that is crawling a site?
In an external application, using an HttpWebRequest you'd do something like this:
string urlToTry = "http://www.example.com/ServerErrorPage.htm";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToTry);
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Process your success response.
}
catch (WebException we)
{
HttpWebResponse error = (HttpWebResponse)we.Response;
// If you want to log multiple codes, prefer a switch statement.
if (error.StatusCode == HttpStatusCode.InternalServerError)
{
// This is your 500 internal server error, perform logging.
}
}
The WebException class will give you messages like:
The remote server returned an error: (500) Internal Server Error.
Once you cast them to an HttpWebResponse, you can get at the StatusCode and perform whatever logging you require.