Loging is a common procedure to view the happenings in future, It will help us to check the flow of the program or software and also helps us to track errors occured while processing.
So I recommend you to use a log fiel to store related information. The file name can be constructed according to the date or can be a single file with predefined name.
Another advantage of loging is thath we can request the client to send the log file to view the happenings at client side.
Here is a simple log file creation and appending code.. Hope it will help you to make a better software
using System,IO;
public void WriteLog(string message) {//MESSAGE TO WRITE IN THE LOG, THIS MAY BE AN EXCEPTION MESSGE OR PROGRAMMER DEFINED MESSAGE try { string fileNameWithPath = "";
fileNameWithPath = @"C:\MyFolder\MyLogFile.txt"; //INSTED OF THIS YOU CAN DYNAMICALLY MADE THE FILENAME
if (!(string.IsNullOrEmpty(fileNameWithPath))) {
if (!(string.IsNullOrEmpty(message))) { FileStream fileStream = new FileStream(fileNameWithPath, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter objStreamWriter = new StreamWriter(fileStream); objStreamWriter.BaseStream.Seek(0, SeekOrigin.End); message = " -> " + DateTime.Now.ToLongDateString() + " At " + DateTime.Now.ToLongTimeString() + " : " + message; objStreamWriter.WriteLine(message); objStreamWriter.Flush(); objStreamWriter.Close(); } } } catch (Exception logException) { }
}
NOTE : you need proper rights to create file in hard drive
|
No responses found. Be the first to respond and make money from revenue sharing program.
|