Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

Simple File Tracer

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
19 May 2009CPOL2 min read 25.7K   272   12   6
Custom component responsible for logging into a file

Introduction

During software development, testing and even when it is in the production environment, error reporting and tracing are considered to be the most valuable requirement, since it eases the process of troubleshooting and bug fixing.
There are multiple repositories where you can log, event log, database, file, etc.
In my article, I will be introducing a custom component responsible for logging into a file.

Background

Before starting with this article, you should be familiar with the .NET Framework, C# and IO processing in C#.

Using the Code

In this article, you will find three main classes:

  1. FileTracer
  2. FileTracerHelper
  3. TraceEnterExit

FileTracer is where the actual logging takes place. The class is responsible for creating the logging directory if it does not exist. The class logs into the log file according to the trace level supplied, and keeps track to the file size. If it exceeds the maximum file size, it will archive the file and create a new one.

FileTracerHelper can be described as the Facade for the client, which will expose all tracer functionality.

TraceEnterExit is a disposable class responsible for reporting entering and exiting functions.
Logging Format will be ([|] Thread | Date Time | Level | Location | Data [-]) where:

  • Thread is the execution thread
  • Date Time is execution time
  • Level is (Information, Warning or error)
  • Location is function name
  • Data is user logged data

FileTracerHelper

C#
public static void Information(string location, string message)
{
   FileTracer.Write(TraceLevel.Info, location, message);
}

Like the above, there is a public function for logging warnings and errors, and can be used as follows:

C#
private void CalculateNumbers()
{
using (TraceEnterExit tee = new TraceEnterExit("CalculateNumbers"))
{
  try
   {
      int counter = 0;
      int userCounter = Convert.ToInt32(txtSucessfullLogging.Text);
      for (int i = 0; i < 5; i++)
      {
         FileTracerHelper.Information
	    ( "CalculateNumbers", "Counter after processing = " + counter.ToString());
         counter++;
      }
    }
    catch(Exception ex)
    {
      FileTracerHelper.Error(ex);
    }
}

Using the TraceEnterExit, you will keep track of whether the function was called and finished or not, logging information if needed and logging errors for troubleshooting.

Points of Interest

You can change the logging format by changing in the FileTracer class -> Write function by redefining the string builder.

History

  • v1.0

License

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


Written By
Program Manager
Jordan Jordan
Self-motivated, creative and results-driven technology executive who is well versed and experienced in leveraging an end-to-end, holistic vision of business objectives to drive full technology / business alignment.

Skilled in grasping business needs and sudden market demand’s shifts by diving into latest business / technology trends, selecting the best fit business model / technology to create a positive reflection on revenue. His multifaceted approach has enabled him to deliver key solutions across a wide range of disciplines including design, development, UX / UI, Business Intelligence.

Technical Specialties are in .Net, Java, Spring Boot, Maven, MS SQL, Oracle, Postgesql, Redis, Javascript, Bootstrap, Angular 2.

Comments and Discussions

 
Generalanother logger Pin
Donsw13-Jun-09 8:02
Donsw13-Jun-09 8:02 
GeneralRe: another logger Pin
Wael Al Wirr13-Jun-09 19:42
Wael Al Wirr13-Jun-09 19:42 

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.