【问题标题】:Filter HttpRequestValidationException Elmah Logs by IP Address按 IP 地址过滤 HttpRequestValidationException Elmah 日志
【发布时间】:2011-07-19 19:23:00
【问题描述】:

我已将此添加到 global.asax 页面,以帮助我过滤掉登录到 elmah 的错误,但只过滤包含 HttpRequestValidationException 的任何异常错误。我收到来自 McAfee 扫描的这些错误。我想知道是否有办法检查 IP 地址,如果 IP 地址与 McAfee 的 IP 地址匹配,则不要记录错误。我试着做:

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

这对我不起作用。如果有办法获取 IP 地址,请告诉我,我需要添加什么命名空间才能使其工作。

【问题讨论】:

    标签: filter ip-address elmah


    【解决方案1】:

    我将此添加到我的 web.config 文件中以检查某些 IP 地址,这将有助于过滤掉我不想登录数据库的错误。

    <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>
    

    【讨论】:

      【解决方案2】:

      要在ErrorLog_Filtering 中执行此操作(我也在ErrorMail_Filtering 中执行此操作),就像 OP 尝试执行的那样,这将起作用:

      if(Context.Request.ServerVariables["REMOTE_ADDR"] == "173.15.183.122") e.Dismiss()
      

      我更喜欢使用.StartsWith("173.15.183"),因为这些服务经常使用该范围内的大量 IP(如果同一块中其他人的错误一旦发生,我也不太在意)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2011-05-01
        • 2016-12-04
        • 2022-12-02
        • 2015-05-30
        • 1970-01-01
        相关资源
        最近更新 更多