| |
 |
|
|
|
|
|
|
Introduction |
The motivation to write the CherryLog4Net is to log information into a database
so that we can easily retrieve the information from the log database using SQL.
CherryLog4Net was designed to log information into databases that have their OLE
DB Data prividers, and to support log buffering, aging and
log level (severity) as well.
Although it was designed to support all databases that have their OLE DB Data
providers, CherryLog4Net is preferably to log information into lightweighted embedded
databases such as SQLite so that it will not rely on network connectivity.
The source code for CherryLog4Net is free of distribution. You may use it for commercial
purposes. You may modify the source code provided you keep the license agreement
intact in the source code.
|
|
Using CherryLog4Net |
CherryLog4Net was intended to be used in .NET 2.0 applications. CherryLog4Net class
library exposes class CherryLog that implemented IDisposable. The CherryLog class
has Add and Flush methods.
Add method allows you to add a log entry and save all log entries into a database
if the buffer is full. Flush allows you explicitly save all log entries in the buffer
before the buffer is full.
Below is our C# sample code that demostrates how to use the CherryLog4Net.
|
using System;
using System.Collections.Generic;
using System.Text;
using CherryLog4Net;
namespace CherryLogSample
{
class Program
{
static void Main(string[] args)
{
CherryLog.LogLevel = CherryLogLevel.Information;
CherryLog.LogFile = "C:\\MyLog.cdb";
CherryLog.Bufferings = 3;
CherryLog.Add(CherryLogLevel.Information, "Main",
"CherryLogSample started.");
System.Console.WriteLine("Please enter an integer below");
string s1 = System.Console.ReadLine();
CherryLog.Add(CherryLogLevel.Information, "Main", "User Input S1: " + s1);
System.Console.WriteLine("");
System.Console.WriteLine("Please enter another integer");
string s2 = System.Console.ReadLine();
CherryLog.Add(CherryLogLevel.Information, "Main", "User Input S2: " + s2);
try
{
CherryLog.Add(CherryLogLevel.Debug, "Main", "Parsing user inputs ...");
int i1 = int.Parse(s1);
int i2 = int.Parse(s2);
CherryLog.Add(CherryLogLevel.Information, "Main",
"Calculating S1/S2 ...");
int i3 = i1 / i2;
CherryLog.Add(CherryLogLevel.Information, "Main",
"S1/S2 = " + i3.ToString());
System.Console.WriteLine("S1/S2 = " + i3.ToString());
}
catch (System.Exception e)
{
CherryLog.Add(CherryLogLevel.Error, "Main",
"There is an error on S1/S2 calculation: " + e.Message);
System.Console.WriteLine("There is an error on S1/S2 calculation: "
+ e.Message);
}
System.Console.WriteLine("Hit any key to terminate");
System.Console.ReadKey();
CherryLog.Purge();
}
}
}
|
|
Customizing Your Own Logging |
Normally, most of you could use CherryLog4Net library 'AS IS'. Some of you may want
to customize it to accommodate your specific logging cases.
For example, you may have a web application that allow users to log on it. You may
want to log error, warning, or information on user bases to figure out what might
cause problems. In this case, you want to add a user column when you CreateTable()
in class CherryLogTable. In the CherryLog class, you want to modify the Save and
Add method to make log the user information. Of cause, you may create your derived
classes instead of modifying the existing classes.
|
|
Retrieving Information from Log Database |
The current CherryLog4Net implementation utilize SQLite embedded database to store
log information. You can use SQlite3.exe command line tool to retrieve log information.
The screenshot below demostrates how we retrieve log information. The first query
retrieves all log information. The second query retrieve only error log information.
Image, if we have millions of records, it will be a lot easier to write a query
to get needed information out of a database than to find a needle in a hay if we
log information into a huge file.
Using SQLite OLE DB provider with Visual Studio 2005, we can also readily retrieve log entries.
|
|
Download |
Update history:
-
Initial source code for CherryLog4Net, Aug. 1, 2006
CherryLog4Net Source Code
|
|
Disclaim |
This whole post here including the downloads is provided 'as-is', without any express
or implied warranty. In no event will the author be held liable for any damages
arising from the use of this software. |
|
|
|
Copyright © Cherry City Software LLC, 2006 - 2009. All Rights Reserved. |