简体   繁体   English

ELMAH:您可以将其设置为仅远程发送电子邮件错误吗?

[英]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. 在本地调试解决方案时,我不希望收到ELMAH错误电子邮件。 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). 我遇到了同样的问题,并且拒绝为本地和远程使用不同的web.configs的想法,因为这在源代码管理中不能很好地发挥作用(但请参见下面的评论)。

Instead, I added this to a Global.asax file in each site which uses Elmah: 相反,我将其添加到使用Elmah的每个站点的Global.asax文件中:

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: OnErrorMailing()是在我们的网络库中声明的扩展方法,如下所示:

    /// <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. 当然,如果您只有一个使用Elmah的站点,或者您不介意代码重复,也可以将该代码直接放入global.asax。

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. 只有我从本地计算机收到例外,生产例外会转到在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. 我找到了一种在VS 2010中使用配置转换的解决方案。我从基本web.config中删除了<errorMail />标记,然后使用转换标记将其添加到了Web.Qa.Config和Web.Release.Config中。 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. 或者只是拥有两个不同的web.configs并确保已关闭“开发人员一个”。

You can have emails go to a pickup folder on your local machine instead of the SMTP server by modifying the web.config file 通过修改web.config文件,可以使电子邮件转到本地计算机上的提取文件夹而不是SMTP服务器。

<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. 或者,您可以将其放在开发框中的Machine.config设置中,这样就不必一直触摸web.config文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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