Click here to Skip to main content
15,868,141 members
Articles / Web Development / ASP.NET
Article

Database Logging in .NET

Rate me:
Please Sign up or sign in to vote.
2.69/5 (4 votes)
11 Jan 20041 min read 59.5K   906   22   2
Component that caches updates to a log and then persists them to a database on a timed interval.

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


Written By
Chief Technology Officer
United States United States
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.

Comments and Discussions

 
QuestionCode snippet? Pin
Chris Maunder21-Jan-04 2:11
cofounderChris Maunder21-Jan-04 2:11 
GeneralOther logging packages Pin
Giles12-Jan-04 9:06
Giles12-Jan-04 9:06 

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

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