简体   繁体   中英

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

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?

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM