asp.net-mvc-3 - 本地测试时如何禁用 elmah 发送电子邮件?

标签 asp.net-mvc-3 email localhost elmah

当我们将以下行添加到 web.config 时 -

<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />

Elmah 会针对发生的任何异常情况发送电子邮件。但我们希望这种情况仅发生在部署在 Web 服务器上的实时站点上。而当我们在我们的机器上本地测试站点时则不会。目前,当我们在本地测试站点时,它会这样做并发送电子邮件。有谁知道我们如何配置它?

最佳答案

将电子邮件日志记录添加到您的 Web.Release.config。我的基本 Web.config 根本不包含任何 Elmah 内容 - 当随版本一起编译时,所有内容都会添加进来。如果您编译发布并在本地运行,它将通过电子邮件发送并记录日志,但常规调试版本不会。

Web.Release.config

  <configSections>
      <sectionGroup name="elmah" xdt:Transform="Insert">
          <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
          <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
          <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
          <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
      </sectionGroup>
  </configSections>

  <connectionStrings>
    <clear/>
      <add xdt:Transform="Insert" name="ErrorLogs" connectionString="...." />
  </connectionStrings>

  <elmah xdt:Transform="Insert">
      <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ErrorLogs" />
      <security allowRemoteAccess="0" />
      <errorMail ...Email options ... />
  </elmah>

    <system.web>
        <compilation xdt:Transform="RemoveAttributes(debug)" />

        <httpModules xdt:Transform="Insert">
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
        </httpModules>
    </system.web>

    <system.webServer>
        <modules xdt:Transform="Insert" runAllManagedModulesForAllRequests="true">
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
        </modules>
    </system.webServer>

</configuration>

最后,应该注意您的基本 Web.config 应该有 <configSections>标签放在开头,即使它是空的:

Web.config

<configuration>
  <configSections /><!-- Placeholder for the release to insert into -->
  ....

关于asp.net-mvc-3 - 本地测试时如何禁用 elmah 发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6945550/

相关文章:

windows - 如何捕获本地网络服务器上的流量?

php - sqlite 不适用于 Windows 7 + php 中的本地主机

c# - MVC 3 图像未加载

c# - Ajax.BeginForm 似乎重定向 Action 完成

jquery - 使用 ValidationAttribute 的自定义验证不会触发客户端验证

html - Outlook 软件去除了电子邮件中的内联 CSS

ios 知道如何创建类似 "To:"短信或电子邮件字段的字段吗?

c# - 网页上的嵌套 foreach 循环

考虑到 gmail 的 + 检查唯一电子邮件的算法

php - 如何在 mysql 上找到我的主机和用户名?