473,396 Members | 1,963 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,396 software developers and data experts.

Global.asax Application_Error not working?

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>
Nov 19 '05 #1
4 6123
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" <ro******@msn.com> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
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>

Nov 19 '05 #2
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" <sl***@nospam.comcast.net> wrote in message
news:eY***************@TK2MSFTNGP12.phx.gbl...
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" <ro******@msn.com> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
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>


Nov 19 '05 #3
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" <ro******@msn.com> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
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>

Nov 19 '05 #4
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" <ro******@msn.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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" <sl***@nospam.comcast.net> wrote in message
news:eY***************@TK2MSFTNGP12.phx.gbl...
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" <ro******@msn.com> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
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>



Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JP | last post by:
I need to be able to trap errors at the application level. I added this code to the Global.asax file. The code I wrote is supposed to get the last error that was generated and write to the event...
3
by: tafs7 | last post by:
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...
5
by: vbMental | last post by:
I am deep into a project and cannot get this to work correctly. I am trying to make a custom error page that will be able to know what exception occurred. I already know about the defaultRedirect...
14
by: paul downing via DotNetMonster.com | last post by:
global file email send -------------------------------------------------------------------------------- guys, i'm developing an application and want to send an email with an error if it occurs...
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
2
by: tshad | last post by:
I have my error handling set up in Global.asax: Sub Application_Error(Sender As Object, E as EventArgs) In this procedure I call a function in a dll that will write out to a history file. ...
1
by: Anonieko | last post by:
Global.asax? Use HttpModules Instead! In a previous post, I talked about HttpHandlers - an underused but incredibly useful feature of ASP.NET. Today I want to talk about HttpModules, which are...
19
by: furiousmojo | last post by:
This is a strange problem. I have a project where the contents of global.asax application_error are not firing. It is an asp.net 2.0 application using web application projects. I have another...
1
by: paulo | last post by:
Hello, I have a DLL library containing some web services which are declared in each .asmx file in the following way: <%@ WebService Language="C#" Class="LibraryName.WebService" %> I would...
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: 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: 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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.