I am trying to put a log.txt file on the root of my application and would like to write to it. But it seems like this is not working. Thanks for any help.
Error message (I dont think i am writing to the proper file, this is local host by the way. I would like this to work if its local or on the webhost):
Access to the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\log.txt' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
This is my code:
public static void LogErrorMessage(Exception e)
{
using (StreamWriter w = File.AppendText("log.txt"))
{
Log(e.ToString(),w);
w.Close();
}
}
private static void Log(String logMessage, TextWriter w)
{
w.Write("\r\nLog Entry : ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
w.WriteLine(" :");
w.WriteLine(" :{0}", logMessage);
w.WriteLine("-------------------------------");
// Update the underlying file.
w.Flush();
}
I think you need:
public static void LogErrorMessage(Exception e, string Directory)
{
using (StreamWriter w = File.AppendText(Directory + "/log.txt"))
{
Log(e.ToString(),w);
w.Close();
}
}
ASPX Class
public void Page_Load()
{
Logger.LogErrorMessage(ex, Server.MapPath("~"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With