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.
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);
}
else
{
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:
log.WriteLine(strclsname);
log.WriteLine(strmethodname);
log.WriteLine(strlineno);
log.WriteLine(strdescription);
log.WriteLine(strmethodstarttime);
log.WriteLine(strmethodendtime);
log.WriteLine(eol);
// Close the stream:
log.Close();
}
}