Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Activity Logging and Error Logging in ASP.NET

, 27 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Activity logging and error logging in ASP.NET.

There are lots of error logging providers available such as log4Net, AnLogger, etc. they are easy to use as well, but just to keep it simple, I have written my own. I call it "Activity and Error Logger", it stores the data in XML format, request by request and session by session. You can actually create the user behaviour maps if you properly call the activity log while performing any major operations. It also takes care of the concurrency, two threads will not overwrite each other. It logs the errors on page in try catch block and also at the global level, in Application_Error event in global.asax.

  1. Include the Logging.cs class in the App_Code folder of your project
  2. Configure the web.config
  3. Set the Application_Error in global.asax
  4. Create the Logs_App and logs_Error folders in your project and you are done
  5. It creates one log file on an hourly basis, one for Activities and one for Errors
  6. Now to log the activity or error, you just have to call the Logging.LogInfo or Logging. LogException functions in your code wherever required.

Logging asp.net

Logging.cs

Include the Logging.cs class from the source code attached in this article.

Web.config

Add the below line in <configSections> if there is no configsections tag in your config, then create one right inside the <configuration> tag:

<configSections>
<section name="ErrorHandling" type="System.Configuration.DictionarySectionHandler"/>
</configSections>

Now, add the below section after the config sections:

<ErrorHandling>
<!–set true to enable the logging–>
<add key="EnableLogging" value="true"/>
<!– path to log folders, DO create the folder in app directory >
<add key="ErrorLogFilesDir" value="Logs_Error"/>
<add key="AppLogFilesDir" value="Logs_App"/>
</ErrorHandling>

Global.asax

Replace the Application_Error with the below code:

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Logging.LogException(Server.GetLastError(), "Global Error");
}

Default.aspx where the activity log and error log is implemented

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LoggingSample
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData(123);
}
}
public void GetData(int userID)
{
try
{
//{get data from database by passing userid}
////to check if it logs the error throw an argument exception
//throw new ArgumentException("generated error");
//on success Log the activity
Logging.LogInfo("Get successful for userid : " + userID.ToString(), true);
}
catch (Exception ex)
{
//on error log the exception
Logging.LogException(ex, "Error in data bind");
}
}
}
}

I have also made the admin part of the application, to create a report of activity logs, it applies the xslt on the activity log files and displays them in a grid. You can extend it to create one for error log files too:

Loggingadmin asp.net

Cheers!!

License

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

Share

About the Author

Shabbir Lathsaheb
Software Developer (Senior)
India India
I am Shabbir Husain Lathsaheb (@SHLathsaheb). Programming is both my passion and profession. I work with a Web Application Development firm as a Sr. Software Engineer and in my free time I am a consultant, I love to sleep and am a movie buff. Do visit My blog to know more.
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionLink to source does not work Pinmemberlingab3-Feb-14 4:13 
BugError downloading source. Pinmemberserberwww4-Nov-13 3:14 
GeneralMy vote of 5 PinmemberSk. Tajbir22-Aug-12 21:47 
QuestionBetter than ELMAH? PinmemberMember 423686531-Jul-12 13:58 
AnswerFormatting PinmemberClifford Nelson26-Jul-12 11: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 | Terms of Use | Mobile
Web03 | 2.8.1411019.1 | Last Updated 27 Jul 2012
Article Copyright 2012 by Shabbir Lathsaheb
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid