问题
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