I have a log.txt file on the root of my application. I would like to be able to have my log class be able to access this file and write to it. But i am not sure how to tell it the correct path of the file in my code. Thanks for any help
using (StreamWriter w = File.AppendText("log.txt"))
{
Log(e.ToString(),w);
w.Close();
}
Use
Server.MapPath("~/log.txt")
to get the full file name.
Update:
If you're not inside an ASP page, but in a regular class, try:
System.Web.HttpContext.Current.Server.MapPath("~/log.txt")
Update 2: It looks like you are trying to make your own logging solution. You may consider using an existing solution, like NLog or Log4Net .
Normally you would get that Path from the Web.config .
And it would be a physical file path, not a relative URL path. No MapPath needed.
The easiest way: create it with the Settings designer tool and read it as
string fname = Properties.Settings.Default.LogFile;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.