Tuesday, January 18, 2011

Redirect console to a file

string sourceDirectory = @"C:\Log";


if (!Directory.Exists(sourceDirectory))

Directory.CreateDirectory(sourceDirectory);



//write to a log file also all the output messages for debugging later

using (FileStream fs = new FileStream(Path.Combine(sourceDirectory, "CreateGroups.log"), FileMode.Create))

{

using (StreamWriter sw = new StreamWriter(fs))

{

Console.SetOut(sw);

Console.WriteLine(string.Format("{0} - ", DateTime.Now) + "Starting to add group " + groupName);
Console.WriteLine(string.Format("{0} - ", DateTime.Now) + "Found the NT directory.");
sw.Close();

}

}

No comments:

Post a Comment