DharmeshPatel33 / elmah

Automatically exported from code.google.com/p/elmah
Apache License 2.0
0 stars 0 forks source link

System.FormatException in Elmah.ErrorMailModule.ReportError, which is terminating the w3wp process and forcing a web-application restart #342

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

I couldn't know exactly when this happens, but it happens when emails are sent 
on a background thread (async=true).  More details can be found on the SO 
issue: http://stackoverflow.com/questions/18313854/

Basically, an error is happening on the background thread on which emails are 
sent, forcing the web-application process to terminate.  I discovered this from 
seeing a lot of application restarts in the logs. 

Below are the stack trace / error details:

Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.FormatException
Stack:
   at System.Text.StringBuilder.AppendFormat(System.IFormatProvider, System.String, System.Object[])
   at System.String.Format(System.IFormatProvider, System.String, System.Object[])
   at Elmah.ErrorMailModule.ReportError(Elmah.Error)
   at Elmah.ErrorMailModule.ReportError(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

I have since then switched off async=true, and the application stopped randomly 
restarting. 

I would expect that these error should not occur, or if there is anything which 
I am doing wrong, the outputted error would  be indicative rather than just 
'System.FormatException'.

I am using the latest stable version as of 23-Aug-2013 through NuGet.

Original issue reported on code.google.com by k...@casasoft.com.mt on 23 Aug 2013 at 2:57

GoogleCodeExporter commented 9 years ago
If you had the PDB deployed too, we could have the exact offending line. In 
absence of that, I'm guessing it's [line #489 of ErrorMailModule.cs]([1]). Are 
you using a custom subject setting in configuration? The subject can have {0} 
and {1} tokens that get replaced with error message and type. However, you have 
to escape { and } otherwise if they appear in the subject format. Could you 
check if that's not causing this issue?

[1] 
https://code.google.com/p/elmah/source/browse/src/Elmah/ErrorMailModule.cs?repo=
1x&name=v1.2-sp2#489

Original comment by azizatif on 23 Aug 2013 at 3:31

GoogleCodeExporter commented 9 years ago
How do I get the PDB for Elmah?  I've added the reference via NuGet, and it 
doesn't seem to download the PDB.  

These are the settings for ELMAH (some details XXX-ed out)

        <elmah>
        <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/errors/" />
        <security allowRemoteAccess="1" />
        <errorMail from="xxxxx@xxxxxxx.com" to="xxxxx@xxxxxxx.com" subject="{Error_Encountered} [XXXXX XXXXX]" 
                   async="true" smtpPort="25" smtpServer="mail.xxxxxxxxxx.com.mt" userName="xxxxxxxxxxx" password="xxxxxxxxxxxxx" />
    </elmah>

Does the `subject` attribute mean I am using a custom subject?  Could it be 
that it is because I am using the curly brackets? If so, I would suggest adding 
a more descriptive error message to it, by catching it and saying what the 
problem might be.  Future users checking the logs could immediately get a 
better idea what is wrong.

Original comment by k...@casasoft.com.mt on 23 Aug 2013 at 3:43

GoogleCodeExporter commented 9 years ago
The PDB can be obtained from the Downloads[1] section in the following zip:
https://code.google.com/p/elmah/downloads/detail?name=ELMAH-1.2-sp2-bin-core.zip

You can copy it out and place it next to Elmah.dll in your web site bin 
directory. However, looking at your settings, the problem seems to be clear. 
The braces around {Error Encountered} are throwing String.Format off. The 
following:

  string.Format("{Error_Encountered} [XXXXX XXXXX]")

will throw FormatException. You need to escape the braces like this:

  string.Format("{{Error_Encountered}} [XXXXX XXXXX]")

Try changing the subject attribute to read like this:

  subject="{{Error_Encountered}} [XXXXX XXXXX]"

Does it help?

[1] https://code.google.com/p/elmah/wiki/Downloads?tm=2

Original comment by azizatif on 23 Aug 2013 at 3:56

GoogleCodeExporter commented 9 years ago
I am testing it out, and it seems that it did work.  It wasn't evident to me 
that this was using the .Net string.Format, hence the {} mean something.  I 
don't have the need to use {}, so I've now replaced them with 
(Error_Encountered) and problem solved.

Maybe you could add a try-catch to it, and a more appropriate message would be 
shown?

Thanks a lot for your help, much appreciated!

Original comment by k...@casasoft.com.mt on 23 Aug 2013 at 4:23

GoogleCodeExporter commented 9 years ago
Closing this as Invalid as it's not problem is external (String.Format rules) 
though point taken about it not being clear that subject is a format as opposed 
to plain text.

Original comment by azizatif on 23 Aug 2013 at 4:33

GoogleCodeExporter commented 9 years ago
Agreed, and thanks once again for your help.

Original comment by k...@casasoft.com.mt on 23 Aug 2013 at 4:48