如何记录数据库中的所有错误但仅在条件基础上发送电子邮件错误?

时间:2021-02-23 22:01:38

I want email to be sent only on a specific condition and log error in DB in all cases. But as I understand, filtering can't work for one of the two. Is that right? If so then how can I achieve it?

我希望只在特定条件下发送电子邮件,并在所有情况下在DB中记录错误。但据我了解,过滤不适用于其中之一。是对的吗?如果是的话那我怎么能实现呢?

Also to note that, right now I'm saving additional info to database on ErrorMail_Mailing in global.asax as replied by Atif Aziz. Because email will be sent only on conditional basis and ErrorMail_Mailing fires only while sending email, I wonder how would I be able to save additional info of all errors to database.

另外需要注意的是,现在我正在通过Atif Aziz回复的global.asax中的ErrorMail_Mailing将其他信息保存到数据库中。因为电子邮件只会在有条件的基础上发送而且ErrorMail_Mailing只在发送电子邮件时触发,我想知道如何将所有错误的其他信息保存到数据库。

UPDATE:
I have modified Elmah code a bit to satisfy my need.

更新:我已经修改了Elmah代码以满足我的需要。

4 个解决方案

#1


2  

The first step is to configure modules. Make sure you add Elmah.ErrorFilterModule after any of the logging modules from ELMAH, as shown here with ErrorLogModule:

第一步是配置模块。确保在ELMAH的任何日志记录模块之后添加Elmah.ErrorFilterModule,如下所示,使用ErrorLogModule:

<httpModules>
    ...     
    //email
    <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
    //sql
    <add name="ErrorSql" type="Elmah.SqlErrorLog, Elmah"/>
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>

    ...
</httpModules>

Then in your configuration section registered Elmah.ErrorFilterSectionHandler as shown here:

然后在您的配置部分注册Elmah.ErrorFilterSectionHandler,如下所示:

<configSections>
    <configSections>
      <sectionGroup name="elmah">
          <section name="errorFilter" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
      </sectionGroup>
  </configSections>

Now you can add filters to decide what errors to be ignored for what source. The following example shows how to prevent having 404 HTTP errors being mailed.

现在,您可以添加过滤器以确定要为哪些源忽略哪些错误。以下示例说明如何防止邮寄404 HTTP错误。

<elmah>
    <errorMail from="xx@xx.com" fromName="xx" to="xx@xx.com" subject="An unhandled exception occured xxx" priority="Normal" async="false" smtpServer="xx.xx.xx.com"/>
    //sql
    <errorLog name="ErrorSql" type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnectionString" />
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    <errorFilter>
        <test>
            <and>
                <equal binding="HttpStatusCode" value="404" type="Int32" />
                <regex binding="FilterSourceType.Name" pattern="mail" />
            </and>
        </test>
    </errorFilter>
</elmah>  

You can find out more detail information on the following link.

您可以在以下链接中找到更多详细信息。

http://code.google.com/p/elmah/wiki/ErrorFiltering

http://code.google.com/p/elmah/wiki/ErrorFiltering

#2


1  

The ELMAH documentation on error filtering has a section on exactly your scenario and is called, which amounts to filtering by source. For example, the following will prevent 404 HTTP errors from being mailed but they will be still logged (assuming both mailing and logging modules are registered):

关于错误过滤的ELMAH文档有一个关于您的场景的部分,并且被调用,相当于按源过滤。例如,以下内容将阻止404 HTTP错误被邮寄,但它们仍将被记录(假设邮件和日志记录模块都已注册):

<errorFilter>
    <test>
        <and>
            <equal binding="HttpStatusCode" value="404" type="Int32" />
            <regex binding="FilterSourceType.Name" pattern="mail" />
        </and>
    </test>
</errorFilter>

#3


0  

If you want to save all exceptions to the database you should just be able to use the ErrorLogModule like so which should be independent of what you are doing in the error mail module:

如果要保存数据库的所有异常,您应该只能使用ErrorLogModule,这应该与您在错误邮件模块中执行的操作无关:

<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />

and then in your elmah secition of your config:

然后在你的配置的elmah分区:

<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnectionString" />

#4


0  

You should try out the StackExchange.Exceptional

您应该尝试StackExchange.Exceptional

This project was inspired by ELMAH, but it didn't suit our particular needs for very, very high volume error logging when a network-level event occurs.

该项目的灵感来自ELMAH,但是当网络级事件发生时,它不适合我们对非常非常高容量的错误记录的特殊需求。

StackExchange.Exceptional is the error handler used internally by Stack Exchange and Stack Overflow for logging to SQL.

StackExchange.Exceptional是Stack Exchange和Stack Overflow内部用于记录到SQL的错误处理程序。

It also supports JSON and memory error stores, filtering of exceptions before logging, and fail/retry mechanisms for storing errors if there's an interruption in connecting to the error store.

它还支持JSON和内存错误存储,在记录之前过滤异常,以及在连接到错误存储时发生中断的故障/重试机制。

It's highly customizable and it's really easy to add something according your needs. As i can see an pull request has the email functionality implemented Email functionality so you can start from there.

它是高度可定制的,并且根据您的需要添加内容非常容易。正如我所看到的拉取请求具有电子邮件功能实现的电子邮件功能,因此您可以从那里开始。

To set it up you only need to look at the web.config and pick what to enable.

要进行设置,您只需要查看web.config并选择要启用的内容。

Hope it helps.

希望能帮助到你。

#1


2  

The first step is to configure modules. Make sure you add Elmah.ErrorFilterModule after any of the logging modules from ELMAH, as shown here with ErrorLogModule:

第一步是配置模块。确保在ELMAH的任何日志记录模块之后添加Elmah.ErrorFilterModule,如下所示,使用ErrorLogModule:

<httpModules>
    ...     
    //email
    <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
    //sql
    <add name="ErrorSql" type="Elmah.SqlErrorLog, Elmah"/>
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>

    ...
</httpModules>

Then in your configuration section registered Elmah.ErrorFilterSectionHandler as shown here:

然后在您的配置部分注册Elmah.ErrorFilterSectionHandler,如下所示:

<configSections>
    <configSections>
      <sectionGroup name="elmah">
          <section name="errorFilter" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
      </sectionGroup>
  </configSections>

Now you can add filters to decide what errors to be ignored for what source. The following example shows how to prevent having 404 HTTP errors being mailed.

现在,您可以添加过滤器以确定要为哪些源忽略哪些错误。以下示例说明如何防止邮寄404 HTTP错误。

<elmah>
    <errorMail from="xx@xx.com" fromName="xx" to="xx@xx.com" subject="An unhandled exception occured xxx" priority="Normal" async="false" smtpServer="xx.xx.xx.com"/>
    //sql
    <errorLog name="ErrorSql" type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnectionString" />
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    <errorFilter>
        <test>
            <and>
                <equal binding="HttpStatusCode" value="404" type="Int32" />
                <regex binding="FilterSourceType.Name" pattern="mail" />
            </and>
        </test>
    </errorFilter>
</elmah>  

You can find out more detail information on the following link.

您可以在以下链接中找到更多详细信息。

http://code.google.com/p/elmah/wiki/ErrorFiltering

http://code.google.com/p/elmah/wiki/ErrorFiltering

#2


1  

The ELMAH documentation on error filtering has a section on exactly your scenario and is called, which amounts to filtering by source. For example, the following will prevent 404 HTTP errors from being mailed but they will be still logged (assuming both mailing and logging modules are registered):

关于错误过滤的ELMAH文档有一个关于您的场景的部分,并且被调用,相当于按源过滤。例如,以下内容将阻止404 HTTP错误被邮寄,但它们仍将被记录(假设邮件和日志记录模块都已注册):

<errorFilter>
    <test>
        <and>
            <equal binding="HttpStatusCode" value="404" type="Int32" />
            <regex binding="FilterSourceType.Name" pattern="mail" />
        </and>
    </test>
</errorFilter>

#3


0  

If you want to save all exceptions to the database you should just be able to use the ErrorLogModule like so which should be independent of what you are doing in the error mail module:

如果要保存数据库的所有异常,您应该只能使用ErrorLogModule,这应该与您在错误邮件模块中执行的操作无关:

<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />

and then in your elmah secition of your config:

然后在你的配置的elmah分区:

<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="MyConnectionString" />

#4


0  

You should try out the StackExchange.Exceptional

您应该尝试StackExchange.Exceptional

This project was inspired by ELMAH, but it didn't suit our particular needs for very, very high volume error logging when a network-level event occurs.

该项目的灵感来自ELMAH,但是当网络级事件发生时,它不适合我们对非常非常高容量的错误记录的特殊需求。

StackExchange.Exceptional is the error handler used internally by Stack Exchange and Stack Overflow for logging to SQL.

StackExchange.Exceptional是Stack Exchange和Stack Overflow内部用于记录到SQL的错误处理程序。

It also supports JSON and memory error stores, filtering of exceptions before logging, and fail/retry mechanisms for storing errors if there's an interruption in connecting to the error store.

它还支持JSON和内存错误存储,在记录之前过滤异常,以及在连接到错误存储时发生中断的故障/重试机制。

It's highly customizable and it's really easy to add something according your needs. As i can see an pull request has the email functionality implemented Email functionality so you can start from there.

它是高度可定制的,并且根据您的需要添加内容非常容易。正如我所看到的拉取请求具有电子邮件功能实现的电子邮件功能,因此您可以从那里开始。

To set it up you only need to look at the web.config and pick what to enable.

要进行设置,您只需要查看web.config并选择要启用的内容。

Hope it helps.

希望能帮助到你。