Click here to Skip to main content
15,892,161 members
Articles / Web Development / ASP.NET

How to Capture Web Events in log4net

Rate me:
Please Sign up or sign in to vote.
4.76/5 (11 votes)
20 Dec 2006CPOL3 min read 55.2K   530   46  
Details how to capture the web events exposed by the ASP.NET framework through log4net. Also provides the details of how to create custom web events that can provide custom logging behavior.
// This software is provided "AS IS" with no warranties of any kind.
// The entire risk arising out of the use or performance of the software
// and source code is with you.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using log4net;
using log4net.Config;

namespace HealthMonitoring
{
	public class Global : System.Web.HttpApplication
	{
		/// <summary>
		/// Handles the Start event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Application_Start(object sender, EventArgs e)
		{
			// Configure log4net
			XmlConfigurator.Configure();
		}

		/// <summary>
		/// Handles the End event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Application_End(object sender, EventArgs e)
		{

		}

		/// <summary>
		/// Handles the PreRequestHandlerExecute event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
		{
			// Add the properties so the session can be traced with the logging statements
			string userName = "none";
			try
			{
				if (User.Identity.IsAuthenticated)
				{
					userName = User.Identity.Name;
				}

			}
			catch (Exception)
			{
				// Do nothing the user could not be fetched
			}
			ThreadContext.Properties["UserName"] = userName;


			string sessionId = "none";
			try
			{
				if (Session != null)
				{
					sessionId = Session.SessionID;
				}
			}
			catch (Exception)
			{
				// Do nothing session just did not exist (for instance the webresource.axd)
			}

			ThreadContext.Properties["SessionId"] = sessionId;
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
United States United States
.NET Software Developer

Education
- Working towards M.S. Computer Science
- B.S. Computer Science
- B.S. Computer Information Systems
- Minor Mathematics

Professional
- Microsoft Certified Application Developer
- Microsoft Certified Professional
- C# ASP.NET Developer (4 years)

Other Interests
- Flash Remoting
- Video Encoding (Windows Media and Flash Video)
- Microsoft Content Management Server

More Programming Thoughts and Tips
- http://gabewishnie.blogspot.com

Comments and Discussions