DebugOfTheRoad / elmah

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

Document how to log an error programmatically #30

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
We should have a page to document how to publish an exception to ELMAH
programmatically. Sometimes a developer inherits code that swallows an
exception but still wants to publish it.

Original issue reported on code.google.com by haac...@gmail.com on 21 Aug 2007 at 5:06

GoogleCodeExporter commented 8 years ago
Simplified summary line

Original comment by azizatif on 21 Aug 2007 at 8:09

GoogleCodeExporter commented 8 years ago
This issue still needs to be addressed via perhaps a wiki page, but meanwhile 
here 
is some new information regarding logging programmatically (yes, cheap 
documentation)...

The ErrorSignal class provides a more powerful way of logging errors. See:
http://elmah.googlecode.com/svn/trunk/src/Elmah/ErrorSignal.cs

Calling ErrorSignal.Raise will go through the same set of events as when an 
unhandled exception is encountered. In effect, you get logging plus mailing and 
even 
filtering if that's what's currently configured.

Original comment by azizatif on 30 Oct 2007 at 9:30

GoogleCodeExporter commented 8 years ago
I was doing it as follows. Is there anything wrong with this?

            try
            {
                ds.WriteXml(xml);
            }
            catch (UnauthorizedAccessException uae)
            {
                Elmah.SqlErrorLog sqlErrorLog = new Elmah.SqlErrorLog
(Common.Config.strCnn);
                sqlErrorLog.Log(new Elmah.Error(uae, 
System.Web.HttpContext.Current));

                //or even...
                throw new Exception(string.Format("Could not write to the file {0}, 
maybe file is read only", xml), uae);
            }

Original comment by abdullah...@gmail.com on 5 Nov 2007 at 2:13

GoogleCodeExporter commented 8 years ago
There is nothing technically wrong with it but you'll be tying in to the 
SqlErrorLog 
implementation. If you switch to another implementation in your configuration 
then 
you might be getting half your errors in one log and the other half in another. 
It 
could be maintenance nightmare to have to go and update your code each time you 
change the configuration. The idea with ErrorSignal was to log an exception 
explicitly yet using whatever happens to be your current configuration. With 
ErrorSignal, you decouple yourself with dependencies on a particular setup.

Original comment by azizatif on 5 Nov 2007 at 2:31

GoogleCodeExporter commented 8 years ago
As of now, one way "to publish an exception to ELMAH
programmatically" seems to be like this...

Elmah.ErrorSignal.FromCurrentContext().Raise(new ApplicationException("my 
msg"));

...so that might help.

-- Mark Kamoski

Original comment by mkamo...@gmail.com on 29 Apr 2009 at 7:57

GoogleCodeExporter commented 8 years ago
I'm trying this in v1.1, but I'd like to know if this is the approved way to do 
it

Dim elmahError As New Elmah.Error()
elmahError.Message = "Something happened"
elmahError.Time = Date.Now()

Dim elmahErrorLog As Elmah.ErrorLog = 
Elmah.ErrorLog.GetDefault(HttpContext.Current)
elmahErrorLog.Log(elmahError)

Original comment by madcoo...@gmail.com on 12 Oct 2009 at 10:39

GoogleCodeExporter commented 8 years ago
@madcookie: Signaling is the more recommended way to go. See:
http://code.google.com/p/elmah/wiki/DotNetSlackersArticle#Signaling_errors

Original comment by azizatif on 12 Oct 2009 at 10:43

GoogleCodeExporter commented 8 years ago
Thank you. This is exactly what I was trying to find. Sorry I couldn't. 
Hopefully
others will find what they need because of your response.

I would like to add this link. It lists common exceptions to help those of us 
needing
a reminder. 

http://blogs.msdn.com/brada/archive/2005/03/27/402801.aspx

Original comment by madcoo...@gmail.com on 12 Oct 2009 at 11:01

GoogleCodeExporter commented 8 years ago
Hi 
I've tried this solution on WCF Service and gets:
System.ArgumentNullException: Value cannot be null.
Parameter name: context
   at Elmah.ErrorSignal.FromContext(HttpContext context) in
c:\builds\ELMAH\src\Elmah\ErrorSignal.cs:line 67
   at Elmah.ErrorSignal.FromCurrentContext() in
c:\builds\ELMAH\src\Elmah\ErrorSignal.cs:line 61

is it not possible to use this method in WCF Service?

Original comment by Cohen.O...@gmail.com on 6 Dec 2009 at 6:20

GoogleCodeExporter commented 8 years ago
Maybe the priority on this needs to go up.

See discussion here:

 http://groups.google.com/group/elmah/browse_thread/thread/9ea4b51420fd5dfa

Original comment by jjspr...@gmail.com on 15 Feb 2010 at 2:22

GoogleCodeExporter commented 8 years ago
Can anyone please explain how I can resolve this issue 
http://code.google.com/p/elmah/issues/detail?id=30#c9

I'm facing the same problem.

I cant understand anything on this discussion
http://groups.google.com/group/elmah/browse_thread/thread/9ea4b51420fd5dfa

Original comment by ismailsi...@gmail.com on 8 Jun 2010 at 12:15

GoogleCodeExporter commented 8 years ago
This issue has been migrated to:
https://github.com/elmah/Elmah/issues/30
The conversation continues there.
DO NOT post any further comments to the issue tracker on Google Code as it is 
shutting down.
You can also just subscribe to the issue on GitHub to receive notifications of 
any further development.

Original comment by azizatif on 25 Aug 2015 at 8:15