如何在ELMAH中连接自定义电子邮件提供商?

时间:2015-04-02 15:58:01

标签: asp.net-mvc elmah

我在谷歌小组的elmah小组上问过这个问题,但我仍然感到神秘,所以我想我会把它扔到一个更大的游泳池。这是我目前所用代码的原始注释。


我在我的MVC 5应用程序上设置了ELMAH,我想使用自定义电子邮件服务来发送电子邮件,而不是使用默认设置的smtp发送。 (这是组织的偏好。)我一直在探索它,但无法弄清楚如何做到这一点。我的服务调用工作得很好,但是我无法将其挂起以便在出现异常时运行。任何帮助将不胜感激。

我设置的内容如下:

Web.config(删除了与非elmah相关的行):

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <sectionGroup name="elmah">
      <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><appSettings>
    ...
    <add key="elmah.mvc.disableHandler" value="false" />
    <add key="elmah.mvc.disableHandleErrorFilter" value="false" />
    <add key="elmah.mvc.requiresAuthentication" value="false" />
    <add key="elmah.mvc.IgnoreDefaultRoute" value="false" />
    <add key="elmah.mvc.allowedRoles" value="*" />
    <add key="elmah.mvc.allowedUsers" value="*" />
    <add key="elmah.mvc.route" value="elmah" />
</appSettings>
  <connectionStrings>
    <add name="BFRDPDB" providerName="System.Data.SqlClient" connectionString="blahblah;" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <customErrors mode="Off" defaultRedirect="~/Home/Error">
      <error statusCode="404" redirect="~/Home/Error" />
    </customErrors>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <add name="ErrorMail" type="BFRDP.Infrastructure.ErrorMailModule" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </httpModules>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorMail" type="BFRDP.Infrastructure.ErrorMailModule" preCondition="managedHandler" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
    </modules>
  </system.webServer>
  <elmah>
    <security allowRemoteAccess="false" />
    <errorLog type="Elmah.SqlErrorLog, Elmah"
              connectionStringName="BFRDPDB"
              applicationName="FarmAnswers.org" /> 
  </elmah>
  <location path="elmah">
    <system.web>
      <httpHandlers>
        <add verb="POST,GET,HEAD"
             path="elmah.axd"
             type="Elmah.ErrorLogPageFactory, Elmah" />
      </httpHandlers>
      <authorization>
        <allow roles="SuperAdmin" />
        <deny users="*" />
      </authorization>
    </system.web>
    <system.webServer>
      <handlers>
        <add name="ELMAH"
             verb="POST,GET,HEAD"
             path="elmah"
             type="Elmah.ErrorLogPageFactory, Elmah"
             preCondition="integratedMode" />
      </handlers>
    </system.webServer>
  </location>
</configuration>

我的ErrorMailModule.cs文件如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace BFRDP.Infrastructure
{/// <summary>
    /// Summary description for ErrorMailModule
    /// </summary>
    public class ErrorMailModule : Elmah.ErrorMailModule
    {
        public ErrorMailModule()
        {
        }



        protected override void SendMail(MailMessage mail)
        {
            if (mail == null)
                throw new ArgumentNullException("mail");
            // Code to send email here through internal provider
        }

    }
}

我的错误确实记录在数据库中,但是从未到达发送电子邮件的代码。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

在我的web.config部分添加了errorMail设置,该应用程序可以访问我的自定义模块:

  <elmah>
    <security allowRemoteAccess="false" />
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="BFRDPDB" applicationName="FarmAnswers.org" />
    <errorMail
    from="elmah@example.com"
    to="admin@example.com"
    subject="..."
    priority="Low"
    async="true"
    smtpPort="25"
    smtpServer="smtp.example.com"
    useSsl="true"
    userName="johndoe"
    password="secret"
    noYsod="true" />
  </elmah>