How to maintain the log file to track application performance using C#?

There is a sample code to check performance of the application by calculating execution time which is required for each and every methods in application. Code has been developed in C# whereas application is windows based.

How to maintain the log file to track application performance?
Description- Create a class library and put this code snippet in to it.
Calculate start time at the beginning of the method and endtime at the end of the method for which you are calculating the execution performance.
It can used to rectify if refactoring needs to be done for any method.
To call this method, you have to create an object of this class library and call.

class MethodLog
//This method is used for calculating method time in each class.
public void GetMethodLog(string clsname , string methodname , int lineno , string description , string methodstarttime , string methodendtime)
// get the base directory
string baseDir = AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.RelativeSearchPath;

// search the file below the current directory
string retFilePath = baseDir + "//" + "LogFile.txt";

// Create a writer and open the file:
StreamWriter log;

if (!File.Exists(retFilePath))
log = new StreamWriter(retFilePath);
log = File.AppendText(retFilePath);

string strclsname = "Class Name : " + clsname ;
string strmethodname = "Method Name : " + methodname ;
string strlineno = "Line Number : " + lineno ;
string strdescription = "Method Description :" + description ;
string strmethodstarttime = "Method Start Time :" + methodstarttime ;
string strmethodendtime = "Method End Time :" + methodendtime ;
string eol = "^^---------------------------------------------------------^^";

// Write log entries to the file:

// Close the stream:



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: