Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to log an error programmatically #30

Open
atifaziz opened this issue Aug 25, 2015 · 11 comments
Open

Document how to log an error programmatically #30

atifaziz opened this issue Aug 25, 2015 · 11 comments

Comments

@atifaziz
Copy link
Member

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.


Originally reported on Google Code with ID 30

Reported by @haacked on 2007-08-21 17:06:58

@atifaziz
Copy link
Member Author

Simplified summary line

Reported by @atifaziz on 2007-08-21 20:09:19

@atifaziz
Copy link
Member Author

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.

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.


Reported by @atifaziz on 2007-10-30 21:30:22

@atifaziz
Copy link
Member Author

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);
}

Reported by abdullah.adam on 2007-11-05 14:13:24

@atifaziz
Copy link
Member Author

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.


Reported by @atifaziz on 2007-11-05 14:31:36

@atifaziz
Copy link
Member Author

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


Reported by @mkamoski on 2009-04-29 19:57:37

@atifaziz
Copy link
Member Author

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)

Reported by @MADCookie on 2009-10-12 22:39:21

@atifaziz
Copy link
Member Author

@MADCookie: Signaling is the more recommended way to go.


Reported by @atifaziz on 2009-10-12 22:43:17

@atifaziz
Copy link
Member Author

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


Reported by @MADCookie on 2009-10-12 23:01:08

@atifaziz
Copy link
Member Author

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?


Reported by Cohen.Omri on 2009-12-06 18:20:50

@atifaziz
Copy link
Member Author

Maybe the priority on this needs to go up.

See discussion here:

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


Reported by jjspraul on 2010-02-15 02:22:28

@atifaziz
Copy link
Member Author

Can anyone please explain how I can resolve this issue #30 (comment)

I'm facing the same problem.

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


Reported by ismailsidhpuri on 2010-06-08 12:15:31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant