Global.asax Application_Error not working?

Discussion in 'ASP .Net' started by Ron Weldy, Jun 4, 2005.

  1. Ron Weldy

    Ron Weldy Guest

    I'm trying to get asp.net errors to write a entry in the server Application
    log and have created an global.asax file and placed it in the root folder
    where my web files reside but when I trigger an server error in some code I
    do not get a log entry. The file contents are below. Any ideas why it does
    not work?

    <%@ Import Namespace="System.Diagnostics" %>

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

    void Application_Error(object sender, EventArgs e)

    {

    Exception objErr = Server.GetLastError().GetBaseException();

    string err = "Error Caught in Application_Error event\n" +

    "Error in: " + Request.Url.ToString() +

    "\nError Message:" + objErr.Message.ToString()+

    "\nStack Trace:" + objErr.StackTrace.ToString();

    EventLog log = new EventLog();

    log.Source = "myApplication";

    log.WriteEntry(err, EventLogEntryType.Error);

    }

    </script>
    Ron Weldy, Jun 4, 2005
    #1
    1. Advertising

  2. Ron Weldy

    Steve Lutz Guest

    Ron,
    Did you actually create your event source? In order to begin logging, you
    must first create the event source. This is more like registering an event
    source for a specific event log.

    Here's a sample for MSDN: (modified for your situation)

    if(!EventLog.SourceExists("MyApplication"))
    {
    EventLog.CreateEventSource("MyApplication", "Application");
    }

    // then you can continue with your code:

    EventLog log = new EventLog();
    log.Source = "myApplication";
    log.WriteEntry(err, EventLogEntryType.Error);

    Hope that Helps

    Steve



    "Ron Weldy" <> wrote in message
    news:...
    > I'm trying to get asp.net errors to write a entry in the server
    > Application log and have created an global.asax file and placed it in the
    > root folder where my web files reside but when I trigger an server error
    > in some code I do not get a log entry. The file contents are below. Any
    > ideas why it does not work?
    >
    > <%@ Import Namespace="System.Diagnostics" %>
    >
    > <script language="C#" runat="server">
    >
    > void Application_Error(object sender, EventArgs e)
    >
    > {
    >
    > Exception objErr = Server.GetLastError().GetBaseException();
    >
    > string err = "Error Caught in Application_Error event\n" +
    >
    > "Error in: " + Request.Url.ToString() +
    >
    > "\nError Message:" + objErr.Message.ToString()+
    >
    > "\nStack Trace:" + objErr.StackTrace.ToString();
    >
    > EventLog log = new EventLog();
    >
    > log.Source = "myApplication";
    >
    > log.WriteEntry(err, EventLogEntryType.Error);
    >
    > }
    >
    > </script>
    >
    >
    Steve Lutz, Jun 4, 2005
    #2
    1. Advertising

  3. Ron Weldy

    Ron Weldy Guest

    Hi Steve,

    Thanks for the tip. I tried but it didn't work. Interestingly enough, on
    page 629 of Dino Esposito's Programming Microsoft ASP.NET he says "Your code
    doesn't necessarily have to create the event source." He goes on to say it
    will automatically place it in the application log so that's why I don't
    have it in my code.

    There is one thing I noticed and I am wondering if it could be the problem.
    My 'application' does not have a name when the error page comes up in the
    browser. It just says "Server Error in '/' Application." Could this be a
    problem?

    - Ron


    "Steve Lutz" <> wrote in message
    news:eYrC0%...
    > Ron,
    > Did you actually create your event source? In order to begin logging, you
    > must first create the event source. This is more like registering an event
    > source for a specific event log.
    >
    > Here's a sample for MSDN: (modified for your situation)
    >
    > if(!EventLog.SourceExists("MyApplication"))
    > {
    > EventLog.CreateEventSource("MyApplication", "Application");
    > }
    >
    > // then you can continue with your code:
    >
    > EventLog log = new EventLog();
    > log.Source = "myApplication";
    > log.WriteEntry(err, EventLogEntryType.Error);
    >
    > Hope that Helps
    >
    > Steve
    >
    >
    >
    > "Ron Weldy" <> wrote in message
    > news:...
    >> I'm trying to get asp.net errors to write a entry in the server
    >> Application log and have created an global.asax file and placed it in the
    >> root folder where my web files reside but when I trigger an server error
    >> in some code I do not get a log entry. The file contents are below. Any
    >> ideas why it does not work?
    >>
    >> <%@ Import Namespace="System.Diagnostics" %>
    >>
    >> <script language="C#" runat="server">
    >>
    >> void Application_Error(object sender, EventArgs e)
    >>
    >> {
    >>
    >> Exception objErr = Server.GetLastError().GetBaseException();
    >>
    >> string err = "Error Caught in Application_Error event\n" +
    >>
    >> "Error in: " + Request.Url.ToString() +
    >>
    >> "\nError Message:" + objErr.Message.ToString()+
    >>
    >> "\nStack Trace:" + objErr.StackTrace.ToString();
    >>
    >> EventLog log = new EventLog();
    >>
    >> log.Source = "myApplication";
    >>
    >> log.WriteEntry(err, EventLogEntryType.Error);
    >>
    >> }
    >>
    >> </script>
    >>
    >>

    >
    >
    Ron Weldy, Jun 4, 2005
    #3
  4. Ron Weldy

    Ron Weldy Guest

    Okay... I moved this code into a Page_Error event and found this error when
    it tries to write to the log.

    System.Security.SecurityException: Requested registry access is not
    allowed.

    What is the best practice on configuring security to allow writing to the
    server logs? Or should I do something different?




    "Ron Weldy" <> wrote in message
    news:...
    > I'm trying to get asp.net errors to write a entry in the server
    > Application log and have created an global.asax file and placed it in the
    > root folder where my web files reside but when I trigger an server error
    > in some code I do not get a log entry. The file contents are below. Any
    > ideas why it does not work?
    >
    > <%@ Import Namespace="System.Diagnostics" %>
    >
    > <script language="C#" runat="server">
    >
    > void Application_Error(object sender, EventArgs e)
    >
    > {
    >
    > Exception objErr = Server.GetLastError().GetBaseException();
    >
    > string err = "Error Caught in Application_Error event\n" +
    >
    > "Error in: " + Request.Url.ToString() +
    >
    > "\nError Message:" + objErr.Message.ToString()+
    >
    > "\nStack Trace:" + objErr.StackTrace.ToString();
    >
    > EventLog log = new EventLog();
    >
    > log.Source = "myApplication";
    >
    > log.WriteEntry(err, EventLogEntryType.Error);
    >
    > }
    >
    > </script>
    >
    >
    Ron Weldy, Jun 4, 2005
    #4
  5. Ron Weldy

    Ron Weldy Guest

    Okay... I was able to solve this by using an existing event source, I just
    picked "Active Server Pages" for now. I found that I can go into the
    registry and add my own source if I want to but that is not high on the
    priority list right now.

    It's funny how the book I'm working with and the tech articles discussing
    this technique dance around this subject. It would have been helpful if they
    just stated the fact that you should use an existing source or just used an
    existing source in the example instead of making it up. In fact in Dino's
    book there is a big CAUTION about permissions but it talks about the event
    logs not the event source. So there is my 2 cents....

    So, Steve you were correct in your diagnosis but the real problem were the
    permissions that the ASPNET account runs under.


    "Ron Weldy" <> wrote in message
    news:%...
    > Hi Steve,
    >
    > Thanks for the tip. I tried but it didn't work. Interestingly enough, on
    > page 629 of Dino Esposito's Programming Microsoft ASP.NET he says "Your
    > code doesn't necessarily have to create the event source." He goes on to
    > say it will automatically place it in the application log so that's why I
    > don't have it in my code.
    >
    > There is one thing I noticed and I am wondering if it could be the
    > problem. My 'application' does not have a name when the error page comes
    > up in the browser. It just says "Server Error in '/' Application." Could
    > this be a problem?
    >
    > - Ron
    >
    >
    > "Steve Lutz" <> wrote in message
    > news:eYrC0%...
    >> Ron,
    >> Did you actually create your event source? In order to begin logging, you
    >> must first create the event source. This is more like registering an
    >> event source for a specific event log.
    >>
    >> Here's a sample for MSDN: (modified for your situation)
    >>
    >> if(!EventLog.SourceExists("MyApplication"))
    >> {
    >> EventLog.CreateEventSource("MyApplication", "Application");
    >> }
    >>
    >> // then you can continue with your code:
    >>
    >> EventLog log = new EventLog();
    >> log.Source = "myApplication";
    >> log.WriteEntry(err, EventLogEntryType.Error);
    >>
    >> Hope that Helps
    >>
    >> Steve
    >>
    >>
    >>
    >> "Ron Weldy" <> wrote in message
    >> news:...
    >>> I'm trying to get asp.net errors to write a entry in the server
    >>> Application log and have created an global.asax file and placed it in
    >>> the root folder where my web files reside but when I trigger an server
    >>> error in some code I do not get a log entry. The file contents are
    >>> below. Any ideas why it does not work?
    >>>
    >>> <%@ Import Namespace="System.Diagnostics" %>
    >>>
    >>> <script language="C#" runat="server">
    >>>
    >>> void Application_Error(object sender, EventArgs e)
    >>>
    >>> {
    >>>
    >>> Exception objErr = Server.GetLastError().GetBaseException();
    >>>
    >>> string err = "Error Caught in Application_Error event\n" +
    >>>
    >>> "Error in: " + Request.Url.ToString() +
    >>>
    >>> "\nError Message:" + objErr.Message.ToString()+
    >>>
    >>> "\nStack Trace:" + objErr.StackTrace.ToString();
    >>>
    >>> EventLog log = new EventLog();
    >>>
    >>> log.Source = "myApplication";
    >>>
    >>> log.WriteEntry(err, EventLogEntryType.Error);
    >>>
    >>> }
    >>>
    >>> </script>
    >>>
    >>>

    >>
    >>

    >
    >
    Ron Weldy, Jun 4, 2005
    #5
    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. tafs7
    Replies:
    3
    Views:
    3,986
    Alvin Bruney
    Dec 22, 2003
  2. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    800
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  3. Mark Rae

    Global.asax / Global.asax.cs in v2

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

Share This Page