Programmatically log error and send email

1,240 views
Skip to first unread message

clayboy

unread,
Jun 24, 2009, 3:25:00 AM6/24/09
to ELMAH
Hello

I am programmatically logging an error to elmah using:
string errorId = Elmah.ErrorLog.GetDefault(HttpContext.Current).Log
(new Elmah.Error(new Exception("im an error"));

The reason for using this command is that i need the errorId generated
for the logged error for uses later on down the line.

i also have ErrorMail enabled as well.

My question is:
When i call the above command, will elmah log to both the error log(in
this case sql server 2005) as well as send an email if the errormail
settings have been configured correctly in the web.config?

Atif Aziz

unread,
Jun 24, 2009, 3:49:44 AM6/24/09
to el...@googlegroups.com
will elmah log to both the error log(in
this case sql server 2005) as well as send an email if the errormail
settings have been configured correctly in the web.config?

Nope. For that, you'll want to rely on error signaling. See:

The reason for using this command is that i need the errorId generated
for the logged error for uses later on down the line.

There may be a simpler way of getting at the same information. The ErrorLogModule raises the Logged event whenever an error has been successfully logged. You can trap this in your Global.asax and get the identifier of the logged error, which is delivered via the event arguments. This way, you don't have to bother with programmatically logging the error yourself.

- Atif

clayboy

unread,
Jun 24, 2009, 6:00:46 AM6/24/09
to ELMAH
Hi

I have put the following code in my global.asax Application_Error(...)
method:

Elmah.ErrorLogModule logModule = new Elmah.ErrorLogModule();
logModule.Logged += new Elmah.ErrorLoggedEventHandler
(logModule_Logged);
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);

I have created the logModule_Logged method as well to retrieve the
errorId generated as follows, and put a breakpoint in it but it never
breaks, can you see anything wrong with what i have done?
private void logModule_Logged(object sender,
Elmah.ErrorLoggedEventArgs args)
{
string errorId = args.Entry.Id;
}

On Jun 24, 9:49 am, Atif Aziz <aziza...@gmail.com> wrote:
> > will elmah log to both the error log(in
> > this case sql server 2005) as well as send an email if the errormail
> > settings have been configured correctly in the web.config?
>
> Nope. For that, you'll want to rely on error signaling. See:
>
>    - Section "Programmatically Logging Errors," in the tutorial, "Logging
>    Error Details with
> ELMAH<http://www.asp.net/learn/hosting/tutorial-14-cs.aspx>
>    "
>    - Signaling errors<http://code.google.com/p/elmah/wiki/DotNetSlackersArticle#Signaling_e...>
>
> The reason for using this command is that i need the errorId generated
>
> > for the logged error for uses later on down the line.
>
> There may be a simpler way of getting at the same information. The
> ErrorLogModule raises the Logged event whenever an error has been
> successfully logged. You can trap this in your Global.asax and get the
> identifier of the logged error, which is delivered via the event arguments.
> This way, you don't have to bother with programmatically logging the error
> yourself.
>
> - Atif
>

clayboy

unread,
Jun 24, 2009, 7:55:23 AM6/24/09
to ELMAH
Hi

Read through some earlier discussions and found the solution at:
http://groups.google.com/group/elmah/browse_thread/thread/fbf7ad9f3b609c5d

Renamed the method to:
private void ErrorLog_Logged(object sender, Elmah.ErrorLoggedEventArgs
args)

as well as:
logModule.Logged += new Elmah.ErrorLoggedEventHandler
(ErrorLog_Logged);

and it works.

Cheers for now :)

Atif Aziz

unread,
Jun 24, 2009, 8:12:59 AM6/24/09
to el...@googlegroups.com
The problem is they way you're subscribing to the event, which works slightly differently with module events in ASP.NET. You have to create a method named after the module name as found in the configuration followed by an underscore, followed by the event name and arguments. IOW, if you registered ELMAH's ErrorLogModule under the name "ErrorLog" then the event handler looks like this:

void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
  ...
}

You don't need to do anything such as subscribing to the event. ASP.NET will do this automagically on your behalf. See the following section of ASP.NET documentation for more on this:

Using Modules with the Global.asax File

The relevant bit is:

The code that you write to handle the events exposed by an HttpModule must conform to the following naming pattern:

FriendlyModuleName_EventName(AppropriateEventArgumentSignature)

- Atif

clayboy

unread,
Jun 25, 2009, 2:31:49 AM6/25/09
to ELMAH
Hi Atif

Thanks for clarifying it for me.
However my approach to using ErrorSignal is to programmatically log an
error to elmah in the global.asax as well as enable it to send out the
ErrorMail as well as return the id of the logged error. I do not wish
for elmah to log the error when it occurs but let it bubble up to the
global.asax then allow me to log it programmatically. By me using the
ErrorLogModule Logged event to return the loggedError object i have to
register the module in the httpmodules section of the web.config and
by then the error would have already been logged to elmah resulting in
logging the same error twice. I have removed the ErrorLog entry in the
httpmodules section of web.config to not allow elmah to log the error
when it occurs. As a result of removing the entry from the web.config
the errorlogmodule logged event method is not raised and i am now
unable to get the id of the logged error.

Is there any other way i can still programmatically log the error +
send out errormail and be able to retrieve the id of the logged error?

Hope this enough information so that you can see where im coming from.

Clayboy

On Jun 24, 2:12 pm, Atif Aziz <aziza...@gmail.com> wrote:
> The problem is they way you're subscribing to the event, which works
> slightly differently with module events in ASP.NET. You have to create a
> method named after the module name as found in the configuration followed by
> an underscore, followed by the event name and arguments. IOW, if you
> registered ELMAH's ErrorLogModule under the name "ErrorLog" then the event
> handler looks like this:
>
> void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
> {
>   ...
>
> }
>
> You don't need to do anything such as subscribing to the event. ASP.NET will
> do this automagically on your behalf. See the following section of
> ASP.NETdocumentation for more on this:
>
> Using Modules with the Global.asax
> File<http://msdn.microsoft.com/en-us/library/szzd570s%28VS.71%29.aspx>
>
> The relevant bit is:
>
> The code that you write to handle the events exposed by an HttpModule must
>
> > conform to the following naming pattern:
>
> > FriendlyModuleName_EventName(AppropriateEventArgumentSignature)
>
> - Atif
>

Atif Aziz

unread,
Jun 26, 2009, 6:17:40 PM6/26/09
to el...@googlegroups.com
Is your application running under full trust?
Reply all
Reply to author
Forward
0 new messages