Application_Error not emailing at global.asax

  • Thread starter Thread starter tafs7
  • Start date Start date
T

tafs7

My code below is supposed to email me when an error occurs on the
application, but it's not emailing anything. Am I missing something?
I know the smtp servers I've tried work. I even added a App_Start
handler to see if I could get emailed at all even from the first
request of the app, but to no avail. Could someone please help me
out? Thanks a lot!

--Thiago
Web developer
AgniTEK


GLOBAL.ASAX:
------------
<%@ Application Description="FOO" %>
<%@ Import Namespace = "System.Diagnostics" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SqlClient" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Web.Mail" %>
<%@ Import Namespace = "System.Text" %>


<script language="c#" runat="server">

void Application_Error (Object sender, EventArgs e)
{
String msg = "\n\nURL:\n " + Request.ApplicationPath
+ "\n\nMESSAGE:\n " + Server.GetLastError().Message
+ "\n\nSTACK TRACE:\n" + Server.GetLastError().StackTrace;

// Create Event Log if it does not exist

String LogName = "Application";
if (!EventLog.SourceExists(LogName)) {
EventLog.CreateEventSource(LogName, LogName);
}

// Insert into Event Log
EventLog Log = new EventLog();
Log.Source = LogName;
Log.WriteEntry(msg, EventLogEntryType.Error);


//create an email message for the admin
MailMessage mailMsg = new MailMessage();
mailMsg.To = "(e-mail address removed)";
mailMsg.From = "(e-mail address removed)";
mailMsg.Subject = "Application Error";
mailMsg.Body = msg;

//send the email
SmtpMail.SmtpServer = "[server name here]";
SmtpMail.Send(mailMsg);
}

void Application_Start (Object sender, EventArgs e)
{
//create an email message for the admin
MailMessage mailMsg = new MailMessage();
mailMsg.To = "(e-mail address removed)";
mailMsg.From = "(e-mail address removed)";
mailMsg.Subject = "Application started";
mailMsg.Body = "App started";

//send the email
SmtpMail.SmtpServer = "[server name here]";
SmtpMail.Send(mailMsg);
}

</script>
 
a cursory examination of the code reveals nothing out of the ordinary. I
suspect your problem lies with permissions and/or the smtp object. Here is
one approach. Declare a static global variable in your global aspx file. In
your application start, write some text to this variable. In another web
page, try to read this variable. Do the same for application error. If you
can read that variable correctly, then the problem lies with permissions.
Another thing you may want to do is set the smtp.Server = "localhost".
 
Alvin, thanks for the reply.

Ok, now the Application_Start handler is working if I use some
different smtp server (localhost didn't work quite right). However,
what I really need is the Application_Error handler to work. So my
question is: what type of errors get handled by this event (please be
more specific than "any unhandled exceptions")? Because I have tried
creating errors on one of my web forms, but the handlers does not
perform the mail. For example, I tried mispelling a sql statement
line that populates a list on the page. I get the error message about
the error when accessing it, but the handler doesn't seem to be
emailing anything to me. I have also tried mispelling the id of a
Label control on the page_load event so it can't find it on the page.
I get the error page with details, but no email from the app_Error
handler. Please, if you have any idea why that is, let me know. I
appreciate your help

--Thiago Silva


Alvin Bruney said:
a cursory examination of the code reveals nothing out of the ordinary. I
suspect your problem lies with permissions and/or the smtp object. Here is
one approach. Declare a static global variable in your global aspx file. In
your application start, write some text to this variable. In another web
page, try to read this variable. Do the same for application error. If you
can read that variable correctly, then the problem lies with permissions.
Another thing you may want to do is set the smtp.Server = "localhost".

--
Regards,
Alvin Bruney
Got DotNet? Get it here
http://home.networkip.net/dotnet/tidbits/default.htm
tafs7 said:
My code below is supposed to email me when an error occurs on the
application, but it's not emailing anything. Am I missing something?
I know the smtp servers I've tried work. I even added a App_Start
handler to see if I could get emailed at all even from the first
request of the app, but to no avail. Could someone please help me
out? Thanks a lot!

--Thiago
Web developer
AgniTEK


GLOBAL.ASAX:
------------
<%@ Application Description="FOO" %>
<%@ Import Namespace = "System.Diagnostics" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SqlClient" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Web.Mail" %>
<%@ Import Namespace = "System.Text" %>


<script language="c#" runat="server">

void Application_Error (Object sender, EventArgs e)
{
String msg = "\n\nURL:\n " + Request.ApplicationPath
+ "\n\nMESSAGE:\n " + Server.GetLastError().Message
+ "\n\nSTACK TRACE:\n" + Server.GetLastError().StackTrace;

// Create Event Log if it does not exist

String LogName = "Application";
if (!EventLog.SourceExists(LogName)) {
EventLog.CreateEventSource(LogName, LogName);
}

// Insert into Event Log
EventLog Log = new EventLog();
Log.Source = LogName;
Log.WriteEntry(msg, EventLogEntryType.Error);


//create an email message for the admin
MailMessage mailMsg = new MailMessage();
mailMsg.To = "(e-mail address removed)";
mailMsg.From = "(e-mail address removed)";
mailMsg.Subject = "Application Error";
mailMsg.Body = msg;

//send the email
SmtpMail.SmtpServer = "[server name here]";
SmtpMail.Send(mailMsg);
}

void Application_Start (Object sender, EventArgs e)
{
//create an email message for the admin
MailMessage mailMsg = new MailMessage();
mailMsg.To = "(e-mail address removed)";
mailMsg.From = "(e-mail address removed)";
mailMsg.Subject = "Application started";
mailMsg.Body = "App started";

//send the email
SmtpMail.SmtpServer = "[server name here]";
SmtpMail.Send(mailMsg);
}

</script>
 
right,

3 Things
Either you are suppressing the error OR your email code is at fault

1. On your webpage, search for this line in your initializecomponent
function
this.Error += new System.EventHandler(this.Default_Error);
or something similar to that. This suppresses the error bubble mechanism
so it wouldn't reach the global application_error event handler
OR
this.Page.Error += ... //same thing
This is most likely your problem why application_error isn't getting
called.

2.

If this is the case, what you need to do is remove all your email code to a
local routine in your webpage and
debug it to make sure it is working correctly. Once it is working, move it
back to the application_error handler.

Please note that if your application encounters and error that is not
handled within a try catch block or the error event handler on the page (it
isn't suppressed), it
will bubble up to the error handler. If the context error property is not
cleared at this point, the error is allowed to spill over and crash the
application, defaulting to a stack dump on the webpage (depending on config
file settings).

3.

At any point in time before that, it is possible to supress
the error bubble event up the stack by doing a Context.ClearError() so first
check to see that this is not in your code.

--
Regards,
Alvin Bruney
Got DotNet? Get it here
http://home.networkip.net/dotnet/tidbits/default.htm
tafs7 said:
Alvin, thanks for the reply.

Ok, now the Application_Start handler is working if I use some
different smtp server (localhost didn't work quite right). However,
what I really need is the Application_Error handler to work. So my
question is: what type of errors get handled by this event (please be
more specific than "any unhandled exceptions")? Because I have tried
creating errors on one of my web forms, but the handlers does not
perform the mail. For example, I tried mispelling a sql statement
line that populates a list on the page. I get the error message about
the error when accessing it, but the handler doesn't seem to be
emailing anything to me. I have also tried mispelling the id of a
Label control on the page_load event so it can't find it on the page.
I get the error page with details, but no email from the app_Error
handler. Please, if you have any idea why that is, let me know. I
appreciate your help

--Thiago Silva


"Alvin Bruney" <vapor at steaming post office> wrote in message
a cursory examination of the code reveals nothing out of the ordinary. I
suspect your problem lies with permissions and/or the smtp object. Here is
one approach. Declare a static global variable in your global aspx file. In
your application start, write some text to this variable. In another web
page, try to read this variable. Do the same for application error. If you
can read that variable correctly, then the problem lies with permissions.
Another thing you may want to do is set the smtp.Server = "localhost".

--
Regards,
Alvin Bruney
Got DotNet? Get it here
http://home.networkip.net/dotnet/tidbits/default.htm
tafs7 said:
My code below is supposed to email me when an error occurs on the
application, but it's not emailing anything. Am I missing something?
I know the smtp servers I've tried work. I even added a App_Start
handler to see if I could get emailed at all even from the first
request of the app, but to no avail. Could someone please help me
out? Thanks a lot!

--Thiago
Web developer
AgniTEK


GLOBAL.ASAX:
------------
<%@ Application Description="FOO" %>
<%@ Import Namespace = "System.Diagnostics" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SqlClient" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Web.Mail" %>
<%@ Import Namespace = "System.Text" %>


<script language="c#" runat="server">

void Application_Error (Object sender, EventArgs e)
{
String msg = "\n\nURL:\n " + Request.ApplicationPath
+ "\n\nMESSAGE:\n " + Server.GetLastError().Message
+ "\n\nSTACK TRACE:\n" + Server.GetLastError().StackTrace;

// Create Event Log if it does not exist

String LogName = "Application";
if (!EventLog.SourceExists(LogName)) {
EventLog.CreateEventSource(LogName, LogName);
}

// Insert into Event Log
EventLog Log = new EventLog();
Log.Source = LogName;
Log.WriteEntry(msg, EventLogEntryType.Error);


//create an email message for the admin
MailMessage mailMsg = new MailMessage();
mailMsg.To = "(e-mail address removed)";
mailMsg.From = "(e-mail address removed)";
mailMsg.Subject = "Application Error";
mailMsg.Body = msg;

//send the email
SmtpMail.SmtpServer = "[server name here]";
SmtpMail.Send(mailMsg);
}

void Application_Start (Object sender, EventArgs e)
{
//create an email message for the admin
MailMessage mailMsg = new MailMessage();
mailMsg.To = "(e-mail address removed)";
mailMsg.From = "(e-mail address removed)";
mailMsg.Subject = "Application started";
mailMsg.Body = "App started";

//send the email
SmtpMail.SmtpServer = "[server name here]";
SmtpMail.Send(mailMsg);
}

</script>
 
Back
Top