Click here to Skip to main content
6,306,412 members and growing! (18,423 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Samples     Intermediate

Database Logging in .NET

By David Hall

Component that caches updates to a log and then persists them to a database on a timed interval.
C#, VB.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, ASP.NET, Dev
Posted:11 Jan 2004
Views:36,710
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
4 votes for this article.
Popularity: 1.62 Rating: 2.69 out of 5
2 votes, 50.0%
1

2
2 votes, 50.0%
3

4

5

Introduction

Often times, in a hosted environment, logging to a local file or the Event Log is not an option for the developer. To solve this problem, one requires a logging solution built on a database table. However, if the volume of log updates is large and frequent, posting a command to the database for each log entry is not favorable.

To overcome this, you can use the in-memory DataTable class provided by the .NET Framework. The Logger class simply encapsulates the ability to post updates to an in-memory table and then have those updates posted in batch mode to the database on some predefined interval.

Usage

To use the Logger component, add it to the form that needs the logging functionality. Also a DataTable (or DataSet) component and a corresponding SqlDataAdapter component. Assign the table that will hold the cached version of your log writes, to the ActiveTable property of the Logger and assign the adapter to the Adapter property. The default flush time is 15 minutes, which you can change to meet your environment's needs.

Somewhere in your code (like in the application's initialization), set the Enabled property of the Logger to true. Then, for each time you want to send an entry to the log, just call the Write method.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

David Hall


Member
I have been a Windows software developer since 1991. Most of what I create fills the need for some aspect of bigger projects that I consult on.
Occupation: Architect
Location: United States United States

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralCode snippet? PinadminChris Maunder3:11 21 Jan '04  
GeneralOther logging packages PinmemberGiles10:06 12 Jan '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Jan 2004
Editor: Smitha Vijayan
Copyright 2004 by David Hall
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project