0

I am using ELMAH to log exceptions that are caught during application execution.

On of the most frequent error messages is like so:

The file '/page/default.aspx' does not exist.

The file. '/error/404/default.aspx' does not exist.

The file. '/another/test/page/default.aspx' does not exist.

Since I can filter certain exceptions from ELMAH by setting regular expressions in web.config, I am trying to write a regular expression to catch messages like this.

This is what I have so far, but gut instinct tells me it's going to fail potentially.

The file '((\/\w)+|(\w))+\.aspx' does not exist.

Could anyone offer any advise on what I can do to make this expression more stable?

It only needs to handle *.aspx pages.

Community
  • 1
  • 1
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • Is your example correct? The first two are a little weird, i.e. `...does not exist. The file.` Did you mean for that to be on the next line? – basher Jul 12 '13 at 16:36
  • 1
    ELMAH can filter by status code. Wouldn't that be easier in this case? – Sven Jul 12 '13 at 16:36
  • @basher, sorry, I didn't check the formatting before I posted! :-S – Matthew Layton Jul 12 '13 at 16:53
  • @Sven, but what happens if other exceptions are thrown with the same status code that aren't related to this? – Matthew Layton Jul 12 '13 at 16:54
  • What do you think could go wrong? What about the simpler `"^The file '.*\.aspx' does not exist.$"`? Wouldn't you want to catch any other missing files? – Andrew Morton Jul 12 '13 at 17:55
  • ELMAH will almost certainly only catch 404 errors related to managed ASP.NET pages or resources anyway - static files will be served by IIS before the ASP.NET runtime is involved, therefore bypassing ELMAH. If you are wanting to log 404 errors for static resources but not ASPX pages then ELMAH may not be the best solution. – pwdst Jul 14 '13 at 05:30
  • It's entirely possible to use the ASP.NET handler to serve up static content, hence having ELMAH be able to report on them. You _really_ don't want to do that if performance is even remotely a concern, though. – Mels Jul 23 '13 at 13:37

1 Answers1

1

You could make it less specific. The following expression would also do the trick perfectly:

The file '.+\.aspx' does not exist\.

Note the backslash escaping the period at the end. The . character is special, it usually means "match any character other than a linebreak". It would work without the backslash, but then it would match any single character after exist.

Mels
  • 476
  • 2
  • 17