Click here to Skip to main content
12,998,759 members (67,606 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 7 Mar 2008

Trace in EventViewer from IIS

, 7 Mar 2008
Rate this:
Please Sign up or sign in to vote.
A class that allows you to trace from an IIS process in EventViewer.


This is a solution to trace from a web application in EventViewer.


Sometime ago, I needed to trace some error messages from a web application. A good solution for me at that time was to send all output to the EventViewer. I wrote a simple class that worked fine during the development phase. After the first release, I noticed that the listener didn't work on the server machine. I started to find what was missing, and after more investigations, I discovered the following problem. On my computer, the web application runs using WebDev.WebServer.exe under my own account. Because I am an administrator on my machine, the web application inherits all rights to access all resources. The situation on the web server is different. Here, the web application is hosted by asp_net.exe which runs under the ASPNET account. This is a special user created by Microsoft for IIS because this service executes code based on external user request. It doesn't have enough privileges to access the Registry or other resources required to write in the EventViewer.

The solution was to execute the code that writes in the EventViewer under a powerful user. To implement this, I used a class called Impersonator. Because the processes of impersonate and unimpersonate is very slow, I have to use this class on a separate thread, and I do it only one time during the web application lifetime.

Using the code

How to use the code:

// In Global.asax add the following lines :
// WebApp - can be your application name
void Application_Start(object sender, EventArgs e) 
   Jhc.Diagnostics.EvListener listener = 
     new Jhc.Diagnostics.EvListener("WebApp");

void Application_End(object sender, EventArgs e) 
     if (System.Diagnostics.Trace.Listeners.Count > 0)
      if (System.Diagnostics.Trace.Listeners[0] is Jhc.Diagnostics.EvListener)
        Jhc.Diagnostics.EvListener listener = 
   catch{ }

// Inside of the EventListener class replace MACHINE1 with
// a valid computer name and USER1/PASSWD1 with a powerfull user from that machine
// otherwise the code will be executed under the process account
if (Environment.MachineName == "MACHINE1")
   using (Impersonator impersonator = new Impersonator("USER1", "", "PASSWD1"))
   Run(); // run without impersonate,
          // you have to have enough rights
          // to write in EventViewer

I didn't use web.config because different users exist on different machines. Also, you can add more users inside of this class.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Romania Romania
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGood article Pin
Donsw8-Jan-09 5:57
memberDonsw8-Jan-09 5:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170622.1 | Last Updated 7 Mar 2008
Article Copyright 2008 by Maze
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid