Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CHTMLLogFile class in C#

0.00/5 (No votes)
23 May 2005 1  
The CHTMLLogFile class presented in this article implements an HTML format for a log file with some (useful) features.

Sample Image

Introduction

In order to track the execution (events) of an application, log files may be useful. Usually they are text files that record the events by adding lines at the end of the file. The CHTMLLogFile class presented in this article implements an HTML format for the log file with some (useful) features:

  1. The newest event is recorded first. (Using IE and F5 will always show the latest event at the top of the list without the need to scroll down.)
  2. Loglevel control. (User defined level for each type of entry. Only events with the right detail level will be recorded.)
  3. Different colors for different events/log entries.
  4. Log file size control. (When reaching the size limit, the file is renamed with a suffix and a new one is created with the same name so there is no need to close/open IE when file changes. Just F5 is enough.)

Background

The structure of the log file is presented below:

Sample Image

When adding new events to the log file, the header is assumed to be of fixed size since it is read and skipped.

The execution sequence is:

  1. Read existing file.
  2. Truncate existing file.
  3. Write header
  4. Write new event
  5. Parse the read file by skipping the header and add the old events to the file.

Using the code

There are two classes: LogItem and CHTMLLogFileClass which are part of the CHTMLLogNamespace namespace. (Add the CHTMLLogfile.cs file to your project.)

The LogItem class listed below implements some sort of structure with details about a specific event on the application's execution. For instance, if we want to log an error we will create a LogItem with the following values:

Name="ERROR", Color="red", FontSize=2, Level= 1

(1 is the highest severity. The higher the level the lower the severity.)

    public class LogItem : IComparable
    {
        public string    m_strName;
        public string    m_strColor;
        public int       m_nFontSize;
        public int       m_nLevel;

        public LogItem(string strName,string strColor, int nFontSize,int nLevel)
        {
            m_strName =strName;
            m_strColor = strColor;
            m_nFontSize = nFontSize;
            m_nLevel=nLevel;
        }

        public Int32 CompareTo(Object obj) 
        {
            LogItem tmpObj = (LogItem) obj;
            return (this.m_strName.ToLower().CompareTo(tmpObj.m_strName.ToLower()));
        }
    }

Note: The CompareTo method is not used at this point. It can be used to sort an array of LogItem elements if needed.

Follow the guidelines below to enable and use the class in your application:

using CHTMLLogNamespace;
public class MainForm : System.Windows.Forms.Form
{
  private System.ComponentModel.Container components = null; 
  private CHTMLLogFileClass m_logfileMain;
  public MainForm() 
  { 
   // Required for Windows Form Designer support 

   InitializeComponent(); 
   // TODO: Add any constructor code after InitializeComponent call 

   InitializeApplication();
  }

  private bool InitializeApplication()
  { 
    //******************************************************************** 

    string strPath = AppDomain.CurrentDomain.BaseDirectory+"Log\\"; 
    string strName = System.DateTime.Now.ToString("yyyy-MM-dd")+ ".html"; 
    m_logfileMain = new CHTMLLogFileClass(strPath+strName); 
    m_logfileMain.LOGLEVEL = 4; 
    m_logfileMain.MAXSIZE = 5; 
    m_logfileMain.AddHeaderItem("ERROR","red",2,1); 
    m_logfileMain.AddHeaderItem("Performance","blue",2,2);
    m_logfileMain.AddHeaderItem("Event","green",2,3); 
    m_logfileMain.AddHeaderItem("Info","black",2,4); 
    //********************************************************************
    return true;   
   } 
}

m_logfileMain.LOGLEVEL = 4;

The log level 4 will record only events created with the 4th parameter in m_logfileMain.AddHeaderItem("Info","black",2,4); with a value of 4 or below. A log entry created with a level of 5 will not be recorded when the LogMessage method is invoked in the application.

Whenever there is a need to log an event, the LogMessage method should be invoked. The 1st parameter is the name of the entry created in the code snippet above. The comparison is case insensitive so it is not important if you call the method below with "Error" or "error" for an entry type added like this:

m_logfileMain.AddHeaderItem("ERROR","red",2,1);
m_logfileMain.LogMessage("Performance","test");
m_logfileMain.LogMessage("ERROR","test"); 
m_logfileMain.LogMessage("Event","test"); 
m_logfileMain.LogMessage("Info","test");

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