ilitirit
ilitirit

Reputation: 16352

Run code every time an ASP.NET page is loaded?

I have to change an existing Web App to enable User Activity logging. For the most part, logging every time someone accesses a page is fine. We've tried extracting the info from the IIS logs but it records the proxy details instead of the user that's logged on.

So then I thought about running code on the Page_Load of every form. The only problem is that this app consists of about 120 forms. I tried putting the code in a base class by performing a search/replace to change the inheritance, but then I get hundreds of warnings about member hiding.

Is there another way of accomplishing what I want to do?

Upvotes: 0

Views: 179

Answers (2)

user2655145
user2655145

Reputation:

Add a httpModule to your application. Modules are hit for every request so add your logic here.

Upvotes: 2

Dominic Zukiewicz
Dominic Zukiewicz

Reputation: 8454

You can add hook up to one of the events, to get around this issue.

However, the order of the events are not guaranteed - e.g. BasePage.Load may fire before or after InheritedPage.Load.

To be more granular, use the PreInit, Init etc..

Step 1

Do a find/replace for the System.Web.UI.Page to BasePage

Step 2

Add this class to your web project

public abstract class BasePage : System.Web.UI.Page
{
   // Assuming nlog
   private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

   protected BasePage()
   {
      this.Load += new EventHandler(LogPageLoad);
   }

   protected void LogPageLoad(object sender, EventArgs e)
   {
      Logger.Trace("Loading Page: {0}",  this.Title);
   }
}

Upvotes: 1

Related Questions