473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Application_Error not emailing at global.asax

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
3 3012
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DalePres | last post by:
I have an error handling page that is called by Global.ASAX in the Application_Error handler. When I test Session.Count in Application_Error, it has a count of 2 and I can access the two session...
1
by: robertlair | last post by:
I am having issues with a production server with the GetLastError() method. Here is what I am doing: Application_Error on Global ASAX: Note: I am doing it this way (with ClearError) because...
8
by: tshad | last post by:
I have an Application_Error function in my Global.asax function that works fine until I try to access my Session variables. I am emailing the results to myself whenever I get an error and would...
1
by: David Herbst | last post by:
VS 2005 / .NET 2.0 Windows 2000 Server sp4 (on development as well as testing server) Using Web Deployment Project to build assemblies for testing and production servers. I added a Global.asax...
0
by: Brian | last post by:
Greetings group! I've got a weird one. I have an ASP.NET 1.1 application that has been running on a dual-processor Windows 2000/IIS 5 server for a couple of years. Global.asax has an...
1
by: vikram | last post by:
I want to provide a custom erro handler for my application. henc I decided to handle all error in Global.asax application_error event and then process the error respectively. Is there any problem...
10
by: Nemisis | last post by:
Hi everyone, I am trying to create a custom error page, that the user gets shown when a error has occurred within my website. The code works perfectly, apart from when an invalid URL is typed...
2
by: =?Utf-8?B?QWxleCBNdW5r?= | last post by:
I am using VS2005 Professional, DOT.NET FrameWork 2. During testing and debugging through the IDE using the development machine LocalHost server whenever an error is encountered the...
1
by: jobs | last post by:
I'm forcing an error/exception in one page as follows Protected Overrides Sub OnLoad(ByVal e As EventArgs) Dim badthing As New Exception("XXX") Throw badthing End Sub And attempting to set...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.