how to get the Compiler error message? asp.net
How can I get the Compiler Error Message if I have the pageUrl?
I tried using the HttpWebRequest class, but I haven't gotten the result yet.
I have collection of pages, that must execute automatically, and if the page fails, I need it to create a log.
Thank y开发者_开发知识库ou
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.
string pagetext = (new System.Net.WebClient()).DownLoadString(<url>);
//Add a better control here
if(pagetext.Contains("Server Error"))
{
`enter code here`
}
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.
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.
精彩评论