ELMAH: Can you set it up to email errors only remotely?

≡放荡痞女 提交于 2019-12-12 19:37:25

问题


While debugging my solution locally, I would like to not receive ELMAH error emails. I'd only like to receive them when the application is hosted on a remote machine. Is this possible?


回答1:


I faced the same problem and rejected the idea of having different web.configs for local and remote as that just doesn't play nicely with source control (but see comments below).

Instead, I added this to a Global.asax file in each site which uses Elmah:

void ErrorMail_Mailing(object sender, ErrorMailEventArgs e)
{
    e.OnErrorMailing(); // Prevent local machine errors from being mailed to the rest of the team
}

OnErrorMailing() is an extension method declared in our web library as follows:

    /// <summary>
    /// Prevent local machine errors from being mailed to the rest of the team. Feel free to add your own machine names/case blocks here
    /// </summary>
    public static void OnErrorMailing(this ErrorMailEventArgs e)
    {
        switch (e.Error.HostName.ToLower())
        {
            case "mymachinename":
                e.Mail.To.Clear();
                e.Mail.To.Add("MYEMAILADDRESS");
                break;
        }
    }

Of course if you have only one site which uses Elmah or you don't mind code duplication you can put that code straight into global.asax too.

I've no doubt there are more elegant solutions but this works for me. Only I receive the exceptions from my local machine and production exceptions go to the email address configured in web.config.




回答2:


I'm pretty sure you can do this. Just filter outgoing error emails. Of course use some setting to indicate that it's hosted remotely. Or just have two different web.configs and make sure "dev one" has emaling turned off.




回答3:


You can have emails go to a pickup folder on your local machine instead of the SMTP server by modifying the web.config file

<system.net>
    <mailSettings>
        <smtp deliveryMethod="SpecifiedPickupDirectory">
            <specifiedPickupDirectory pickupDirectoryLocation="c:\temp\" />
        </smtp>
    </mailSettings>
</system.net>

Or you can put this in your Machine.config settings on your dev box so you don't have to keep touching the web.config file.




回答4:


I found a solution which is to use config transforms with VS 2010. I removed the <errorMail /> tag from my base web.config then added it to my Web.Qa.Config and Web.Release.Config with transform tags. When I publish, it allows the remote machines to send out the emails while they are suppressed locally.



来源:https://stackoverflow.com/questions/8914463/elmah-can-you-set-it-up-to-email-errors-only-remotely

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!