简体   繁体   中英

Received multiple email errors form Elmah

2 for my ASP.NET web pages .NET 4.0 app. I was using Enterpriselibrary to log and send error via email before the app was upgraded to .NET 4.0 but since its upgraded the EnterpriseLibrary logging has stopped logging so I have decided to switch to Elmah.

In part of my web.config file of the app:

<add name="ErrorHandler" type="myWebApp.ErrorHandlingModule, myWebApp" preCondition="managedHandler" />
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />

As you can see there is already an Errorhandling module there (ErrorHandlingModule), which creates a custom error message ("ex" object in the following code). Originally in the ErrorHandlingModule:

try
            {
                ExceptionPolicy.HandleException(ex, "myWebApp Error Handling");
            }
            catch (Exception exApplicationBlockError)
            {
                try
                {

                    EventLog log = new EventLog("Application");
                    log.Source = "Application Error";
                    log.WriteEntry(exApplicationBlockError.ToString(), EventLogEntryType.Error);
                }
                catch
                {
                }
            }

Since I switch to Elmah:

 try
            {
                //ExceptionPolicy.HandleException(ex, "myWebApp Error Handling");

                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);                
            }
            catch (Exception exApplicationBlockError)
            {
                try
                {

                    EventLog log = new EventLog("Application");
                    log.Source = "Application Error";
                    log.WriteEntry(exApplicationBlockError.ToString(), EventLogEntryType.Error);
                }
                catch
                {
                }
            }

It works very well but the problem now is if there is any error it raises 2 error emails: one is the custom one, and the other the default Elmah one; and of course I only want the custom one.

I haven't added any extra Elmah ErrorFilter in the program but I do need to have the custom exception in my email.

Should I add a custom Elmah filter to filter out ALL the exceptions so Elmah won't send me the default error email? (Or any other suggestions?)

Also attach the elmah config in web.config file if it is any help:

 <elmah>        
    <security allowRemoteAccess="false" />
    <errorMail from="xxxx@myorg.au"  to="xxxxx@myorg.au"  subject="myWebApp error"  async="true" />
  </elmah>

Thanks in advance. Happy holiday!

If you don't want Elmah to email, then don't configure it to do so in the first place. Remove the module configuration:

<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah">

And any other ErrorMail Elmah email related configuration such as

<errorMail from="xxxx@myorg.au" to="xxxxx@myorg.au" subject="myWebApp error" async="true" />

Silly me I should not need to include: Elmah.ErrorSignal.FromCurrentContext().Raise(ex); It's why the email was sent twice. But thanks for the help.

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