Click here to Skip to main content
12,822,050 members (45,381 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 19 May 2009

Simple File Tracer

, 20 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Custom component responsible for logging into a file


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.


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


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:

private void CalculateNumbers()
using (TraceEnterExit tee = new TraceEnterExit("CalculateNumbers"))
      int counter = 0;
      int userCounter = Convert.ToInt32(txtSucessfullLogging.Text);
      for (int i = 0; i < 5; i++)
	    ( "CalculateNumbers", "Counter after processing = " + counter.ToString());
    catch(Exception 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.


  • v1.0


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


About the Author

Wael Al Wirr
Program Manager INVESTBANK
Jordan Jordan
Wael Al Wirr’s Specialties:
•Microsoft BizTalk Server 2004, 2006, 2010
•ASP.Net, C#, ADO.Net and .Net Framework (1.1, 2.0, 3.0, 3.5, 4.0)
•Windows Communication Foundation
•Windows Workflow Foundation
•Windows AppFabric
•ADO.NET Entity Framework
•Microsoft Line Of Business Adapter Framework
•SQL Server 2000, 2005
•Web development
•Database design
•Web Services
•Build, deployment script
•Microsoft Source Safe, Microsoft Team Foundation Server

You may also be interested in...

Comments and Discussions

Generalanother logger Pin
Donsw13-Jun-09 9:02
memberDonsw13-Jun-09 9:02 
GeneralRe: another logger Pin
Wael Al Wirr13-Jun-09 20:42
memberWael Al Wirr13-Jun-09 20:42 
QuestionWhy another logger? Pin
Robert Taylor19-May-09 23:27
memberRobert Taylor19-May-09 23:27 
AnswerRe: Why another logger? Pin
Wael Al Wirr20-May-09 0:43
memberWael Al Wirr20-May-09 0:43 
GeneralRe: Why another logger? Pin
Robert Taylor20-May-09 3:00
memberRobert Taylor20-May-09 3:00 
GeneralRe: Why another logger? Pin
Wael Al Wirr20-May-09 3:21
memberWael Al Wirr20-May-09 3:21 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 20 May 2009
Article Copyright 2009 by Wael Al Wirr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid