Writing a Log file.....
Here's a chunk of code that will write values to a log file. If the file doesn't exist, it creates it, otherwise it just appends to the existing file. You need to add "using System.IO;" at the top of your code, if it's not already there.
here is the full code....
string strLogText = "Some details you want to log.";
// Create a writer and open the file:
StreamWriter log;
if (!File.Exists(@"D:/logfile.txt"))
{
log = new StreamWriter(@"D:/logfile.txt");
}
else
{
log = File.AppendText(@"D:/logfile.txt");
}
// Write to the file:
log.WriteLine(DateTime.Now);
log.WriteLine(Message);
log.WriteLine();
// Close the stream:
log.Close();
here is the full code....
string strLogText = "Some details you want to log.";
// Create a writer and open the file:
StreamWriter log;
if (!File.Exists(@"D:/logfile.txt"))
{
log = new StreamWriter(@"D:/logfile.txt");
}
else
{
log = File.AppendText(@"D:/logfile.txt");
}
// Write to the file:
log.WriteLine(DateTime.Now);
log.WriteLine(Message);
log.WriteLine();
// Close the stream:
log.Close();
Labels: Writing a Log file.....
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home