Add Referrer to Page or File not found Errors

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
5/23/2011 11:04:27 AM
Gravatar
Total Posts 76

Add Referrer to Page or File not found Errors

I would love to find the best way to add the Referrer to the error log.

This would help me locate the problem for some of the bad links and correct them.

 

Example:

2011-05-20 20:19:33,149 ERROR mojoPortal.Web.PageNotFoundHttpModule - xxx.xxx.xxx.xxx PageNotFoundHttpModule handled error.
System.Web.HttpException (0x80004005): The file '/crazy_page_name_that_has_MS_Word_Chars_in_title breaks_me.aspx' does not exist (Referrer: htttp://www.notmysite.com/some_parnterlink).....

 

I can try add this on my own, wonder if others would find it useful enough to add it into the project?

thoughts?

 

 

5/23/2011 11:13:09 AM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Add Referrer to Page or File not found Errors

Yes, if it's possible to add that, I would find it very helpful! We run into the same situation fairly regularly.

Jamie

5/23/2011 12:20:53 PM
Gravatar
Total Posts 18439

Re: Add Referrer to Page or File not found Errors

Google webmaster tools provides a report of broken links in your entire site and what pages the links are on.

Best,

Joe

5/23/2011 12:53:47 PM
Gravatar
Total Posts 76

Re: Add Referrer to Page or File not found Errors

Not sure how best to past the simple changes on... but here we go:

 

So far I modified 2 files:

Global.asax.cs: (Some small logic fix as well if (!errorObtained) then there is no need to check the HttpContext)

after line: 353

 

if (errorObtained)
      {
        // If we get the error, grab some info about the request
        string exceptionUrl = string.Empty;
        string exceptionIpAddress = string.Empty;
        string exceptionReferrer = "none";


        if (HttpContext.Current != null)
        {
          if (HttpContext.Current.Request != null)
          {
            exceptionUrl = CultureInfo.CurrentCulture.ToString() + " - " + HttpContext.Current.Request.RawUrl;
            exceptionIpAddress = SiteUtils.GetIP4Address();

            if (HttpContext.Current.Request.UrlReferrer != null)
            {
              exceptionReferrer = HttpContext.Current.Request.UrlReferrer.ToString();
            }
          }
        }

        if (ex is UnauthorizedAccessException)
        {
          // swallow this for medium trust?
          log.Error(exceptionIpAddress + "-" + exceptionUrl + "- Referrer(" + exceptionReferrer + ")", ex);
          return;
        }

        if (ex.Message == "File does not exist.")
        {

          log.Error(exceptionIpAddress + "-" + exceptionUrl + "- Referrer(" + exceptionReferrer + ")", ex);
          return;
        }

        log.Error(exceptionIpAddress + "-" + exceptionUrl + "- Referrer(" + exceptionReferrer + ")", ex);

 

PageNotFoundHttpModule.cs:

//After Line 91

if (((HttpException)(ex)).GetHttpCode() == 404)
          {
            string exceptionReferrer = string.Empty;
            if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.UrlReferrer != null)
            {
              exceptionReferrer = HttpContext.Current.Request.UrlReferrer.ToString();
            }
            else
            {
              exceptionReferrer = "none";
            }

            log.Error(SiteUtils.GetIP4Address() + " - Referrer(" + exceptionReferrer + ")  PageNotFoundHttpModule handled error.", ex);

 

 

5/23/2011 12:58:42 PM
Gravatar
Total Posts 76

Re: Add Referrer to Page or File not found Errors

Joe,

I use Webmaster tools, they are definite helpful but when an error comes in, being able to quickly and exactly see where problem came from make support easier.  Waiting for Google to crawl the page on low traffic sites doesn't make timely fixes easy.

Warner

5/23/2011 1:08:00 PM
Gravatar
Total Posts 18439

Re: Add Referrer to Page or File not found Errors

Fair enough, I will add the changes you suggested.

Best,

Joe

5/23/2011 1:25:38 PM
Gravatar
Total Posts 76

Re: Add Referrer to Page or File not found Errors

Thanks Joe~

I have tested the code i provided in a local environment, not in production.

I would not be opposed to the referrer info being added having a toggled via appsettings if you so desire.

Thanks again Joe~

You must sign in to post in the forums. This thread is closed to new posts.