Create application log file in C# / Write to File example

2016/04/19 17:18
Application log file example

private string logFile = "full path and file name (C:\\temp\\MyApplicationLog.txt)";

...

WriteToLogFile("your application message", logFile); // USE any time to add record to the log file

...

public void WriteToLogFile(string strMessage, string outputFile)

{

string line = DateTime.Now.ToString() + " | ";

line += strMessage;

FileStream fs = new FileStream(outputFile, FileMode.Append, FileAccess.Write, FileShare.None);

StreamWriter swFromFileStream = new StreamWriter(fs);

swFromFileStream.WriteLine(line);

swFromFileStream.Flush();

swFromFileStream.Close();

}