Application_Error not emailing at global.asax

Discussion in 'ASP .Net' started by tafs7, Dec 20, 2003.

  1. tafs7

    tafs7 Guest

    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 = "";
    mailMsg.From = "";
    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 = "";
    mailMsg.From = "";
    mailMsg.Subject = "Application started";
    mailMsg.Body = "App started";

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

    </script>
     
    tafs7, Dec 20, 2003
    #1
    1. Advertising

  2. tafs7

    Alvin Bruney Guest

    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" <> wrote in message
    news:...
    > 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 = "";
    > mailMsg.From = "";
    > 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 = "";
    > mailMsg.From = "";
    > mailMsg.Subject = "Application started";
    > mailMsg.Body = "App started";
    >
    > //send the email
    > SmtpMail.SmtpServer = "[server name here]";
    > SmtpMail.Send(mailMsg);
    > }
    >
    > </script>
     
    Alvin Bruney, Dec 21, 2003
    #2
    1. Advertising

  3. tafs7

    tafs7 Guest

    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:<>...
    > 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" <> wrote in message
    > news:...
    > > 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 = "";
    > > mailMsg.From = "";
    > > 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 = "";
    > > mailMsg.From = "";
    > > mailMsg.Subject = "Application started";
    > > mailMsg.Body = "App started";
    > >
    > > //send the email
    > > SmtpMail.SmtpServer = "[server name here]";
    > > SmtpMail.Send(mailMsg);
    > > }
    > >
    > > </script>
     
    tafs7, Dec 22, 2003
    #3
  4. tafs7

    Alvin Bruney Guest

    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" <> wrote in message
    news:...
    > 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:<>...
    > > 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" <> wrote in message
    > > news:...
    > > > 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 = "";
    > > > mailMsg.From = "";
    > > > 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 = "";
    > > > mailMsg.From = "";
    > > > mailMsg.Subject = "Application started";
    > > > mailMsg.Body = "App started";
    > > >
    > > > //send the email
    > > > SmtpMail.SmtpServer = "[server name here]";
    > > > SmtpMail.Send(mailMsg);
    > > > }
    > > >
    > > > </script>
     
    Alvin Bruney, Dec 22, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    899
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  2. Ron Weldy
    Replies:
    4
    Views:
    7,133
    Ron Weldy
    Jun 4, 2005
  3. Mark Rae

    Global.asax / Global.asax.cs in v2

    Mark Rae, May 23, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,198
    Mark Rae
    May 23, 2006
  4. vikram
    Replies:
    1
    Views:
    873
    Karl Seguin [MVP]
    Oct 4, 2006
  5. jobs
    Replies:
    1
    Views:
    1,534
    Juan T. Llibre
    Nov 15, 2007
Loading...

Share This Page