By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,423 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,423 IT Pros & Developers. It's quick & easy.

Application_Error not emailing at global.asax

P: n/a
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 = "ta***@yahoo.com";
mailMsg.From = "ta***@yahoo.com";
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 = "ta***@yahoo.com";
mailMsg.From = "ta***@yahoo.com";
mailMsg.Subject = "Application started";
mailMsg.Body = "App started";

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

</script>
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <ta***@yahoo.com> wrote in message
news:8a**************************@posting.google.c om...
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 = "ta***@yahoo.com";
mailMsg.From = "ta***@yahoo.com";
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 = "ta***@yahoo.com";
mailMsg.From = "ta***@yahoo.com";
mailMsg.Subject = "Application started";
mailMsg.Body = "App started";

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

</script>

Nov 18 '05 #2

P: n/a
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 news:<uW**************@TK2MSFTNGP10.phx.gbl>...
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" <ta***@yahoo.com> wrote in message
news:8a**************************@posting.google.c om...
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 = "ta***@yahoo.com";
mailMsg.From = "ta***@yahoo.com";
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 = "ta***@yahoo.com";
mailMsg.From = "ta***@yahoo.com";
mailMsg.Subject = "Application started";
mailMsg.Body = "App started";

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

</script>

Nov 18 '05 #3

P: n/a
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" <ta***@yahoo.com> wrote in message
news:8a**************************@posting.google.c om...
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

news:<uW**************@TK2MSFTNGP10.phx.gbl>...
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" <ta***@yahoo.com> wrote in message
news:8a**************************@posting.google.c om...
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 = "ta***@yahoo.com";
mailMsg.From = "ta***@yahoo.com";
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 = "ta***@yahoo.com";
mailMsg.From = "ta***@yahoo.com";
mailMsg.Subject = "Application started";
mailMsg.Body = "App started";

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

</script>


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.