Log ASP.NET Application Events (Global.asax)


In this post I will be explaining to you how to monitor and “log” your ASP.NET Application's events that you can define and manage through the use of Global.asax file. (“Application_Error” event specifically. You can apply the same principles to the other events as well.)

Question: Why log events?

1) You may have some application-wide tasks to perform throught the use of your Global.asax and you think something is wrong/ not working and you want to know what is wrong.
2) Your application compiles just fine, no runtime errors when deployed on a shared hosting and you suspect something is wrong on application level.
3) Any other reasons you can think of.

Log Your Application's Events Into Windows Event Viewer

Lets do an example on Application_Error event:
(for most of you this might be the most important event to track anyway)

Coding for event logging is pretty simple and straight forward but when you run the code you may get an error like this:

The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security.”

(see below for both the code and the error)

Here is the code if you want to copy it:

string kaynak = “”;
Exception hata = Server.GetLastError(); //(1)
if(hata != null){
 while (hata.InnerException!=null) //(2)             {

hata = hata.InnerException;


kaynak = hata.Source;
string hedefsite = hata.TargetSite.ToString();
string stacktrace = hata.StackTrace;
string mesaj = “HATA KAYNA?I: “ + kaynak; //(3)

mesaj += ” HATA MESAJI: “ + hata.Message ; //(3)

mesaj += ” HEDEF: “ + hedefsite; //(3)

mesaj += ” STACK TRACE: “ + stacktrace; //(3)

EventLog EL = newEventLog(); //(4)

EL.Source = “SerdarWebApp”; //(5)

EL.WriteEntry(mesaj, EventLogEntryType.Error); //(6)

Explanation of the code:

1-Get the exception using System.GetLastError()
2-Use while loop to get to the core error
3-Build the error message using Source, Message, TargetSite, StackTrace values
4-Create a new EventLog object
5-Assign a Source to this object
6-Write to the Log

Reason of the Error:

Applications that run using the Network Service identity can write to the event log by using existing event sources, but they cannot create new event sources because of insufficient registry permissions. When you use the EventLog.Write method, if the specified event source does not exist, this method attempts to create the event source and a security exception is generated.
(Refer to this link for more info: msdn.microsoft.com)

What you should do:

If you take a quick look at the above link you will see that there are 2 options:

A-Create new event sources at application install time
B-Manually create new event source entry in the registry

I will be following the second option (B):

1- Run “Regedit.exe”
2- Go to “HKEY_LOCAL_MACHINE SYSTEM CurrentControlSet Services Eventlog Application”
3- Right-click on “Application” and add a new key (New-> Key)

4- Enter your event source name here. In my case it is “SerdarWebApp”

Run your code again. This time everyting should be fine and you should be seeing your Log entry in Windows Event Viewer

As you may realise in the above screenshot, I have created a custom view only for the “SerdarWebApp” source. Here is how you can do it:

1- Go to “Control Panel -> Administrative Tools -> Event Viewer”
2- Right-click on “Custom Views” and click “Create Custom View…”
3- Click “By source” and choose your event source from the list

4- Click “OK”

Simple as that.

Good Luck,

Leave a Reply

Your email address will not be published. Required fields are marked *