filterip-addresselmah

Filter HttpRequestValidationException Elmah Logs by IP Address


I've added this to the global.asax page to help me filter out errors from being logged onto elmah but that only filters any exception error that contains the HttpRequestValidationException. I am receiving these errors from McAfee scans. I was wondering if there was a way to check for an ip address, if the ip address matches that of McAfee, then do not log the error. I tried to do:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    if (Server.HtmlEncode(Request.UserHostAddress) == "173.15.183.122" 
        && e.Exception.GetBaseException() is HttpRequestValidationException) {
        e.Dismiss();
    }
}

That isn't working for me. If there is a way to grab an ip address please let me know and what namespace I would need to add to make it work.


Solution

  • I added this to my web.config file to check for certain ip addresses, which would help filter out what errors I do not want to log in my database.

    <elmah>
        <errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="ELMAH.SQLite" />
        <security allowRemoteAccess="yes" />
        <errorFilter>
          <test>
            <and>
              <equal binding="HttpStatusCode" value="500" type="Int32" />
              <regex binding="Context.Request.ServerVariables['REMOTE_ADDR']" pattern="((165.193.42.(6[7-9]|7[0-9]|8[0-6]|13[1-9]|14[0-9]|150))|(161.69.30.(13[6-9]|1[4-5][0-9]|16[0-7]))|(64.14.3.(196|21[4-9]|22[0-9]))|(64.41.168.(24[2-9]|25[0-4]))|(216.35.7.(9[8-9]|1[0-1][0-9]|12[0-6]))|(64.41.140.(9[8-9]|10[0-7]))|(161.69.14.(13[6-9]|1[4-5][0-9]|16[0-7])))$" type="String" />
            </and>
          </test>
        </errorFilter>
    </elmah>