当我们将以下行添加到 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/