using System.Text;
public string LogPath = "D:\LogFiles\";
public string LogFile = LogPath + DateTime.Now.Year + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second + "_Log" + ".txt";
System.IO.File.AppendAllText(LogFile, "Error in connection." + "\r\n");
System.IO.File.AppendAllText(LogFile, "Connected." + "\r\n");
System.IO.File.AppendAllText(LogFile, "Connection closed." + "\r\n");
just write above line at each step where ever you want to write in log file.