Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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

Background

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");
   System.Diagnostics.Trace.Listeners.Add(listener);
   listener.Start();
}    

void Application_End(object sender, EventArgs e) 
{
   try
   {
     if (System.Diagnostics.Trace.Listeners.Count > 0)
      if (System.Diagnostics.Trace.Listeners[0] is Jhc.Diagnostics.EvListener)
      {
        Jhc.Diagnostics.EvListener listener = 
          (Jhc.Diagnostics.EvListener)System.Diagnostics.Trace.Listeners[0];
        listener.Stop();
      }
   }
   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();
   }
}
else
   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.

Links

License

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

About the Author

Maze
Software Developer
Romania Romania
No Biography provided

Comments and Discussions

 
GeneralGood article PinmemberDonsw8-Jan-09 5:57 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 7 Mar 2008
Article Copyright 2008 by Maze
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid