views:

76

answers:

5

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.

alt text

thank you

A: 

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.

Hassan Syed
so, how can I know if page fails or not? not http errors.
loviji
if i can't get compiler, how i can know one thing - page fail or not with .net?
loviji
edited my post, remember to accept answer and vote post if it solves your problem.
Hassan Syed
A: 

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.

R. Bemrose
+1  A: 
string pagetext = (new System.Net.WebClient()).DownLoadString(<url>);

//Add a better control here
if(pagetext.Contains("Server Error"))
{
     `enter code here`
 }
TheQult
+1  A: 

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.

terR0Q
You're right, but my logApplication is Service, WinApplication. I can't implement global.asax
loviji
Where do you want to catch exceptions? In remote win application that pokes web server or on web server itself?
terR0Q
A: 

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.

Zhaph - Ben Duguid
I thinks, you solve another problem, not mine
loviji
Well, you mentioned using the HttpWebRequest, and your question is a bit vague on *where* you are trying to log these errors - when you call the page from somewhere else with an HttpWebRequest, or on the server when a page is requested?
Zhaph - Ben Duguid