If this is a Web Project (ASP.NET) then you should write your log file in the standard folders provided they exists under your root site folder. By default you are unable to write outside the root folder for security reasons
public void LogMessageToFile(string message)
{
// Get the physical path corresponding to the root folder of your site plus APP_DATA
string appData = Server.MapPath("~/APP_DATA");
// Create the log file name
string logFile = Path.Combine(appData, "ErrorLog.txt");
// Append to the log file and close/dispose the stream
using(StreamWriter aw = new StreamWriter(logFile, true))
{
sw.WriteLine(message);
}
}
The folder APP_DATA under your root folder is used when read/write permissions are required by your code (database files, log files and other data files).
Notice also that a StreamWriter should be enclosed in the using statement to ensure proper closing in the eventuality of an exception or other write problems.