Read & Write Writelog
How do I write the error faced into the log.txt?
Firstly, I get error file name from web config as follows:
string Errorlog = System.Configuration.ConfigurationManager.AppSettings["Errorlog.txt"];
Next, I try to get the full path of the te开发者_运维问答xt file; but i do not know which one will pull it out. This is a few. I want to get the full path by not making it static.
//string path = Global.getLogFilePath();
//string path = Path.GetFileName(directoryFullPath);
//string path = openFileDialog.FileName;
//string path = Path.Combine(System.Environment.CurrentDirectory);
//string path = Path.GetFullPath("Errorlog.txt");
//string path = Directory.GetCurrentDirectory();
//string path = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName());
//string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
//string path = Environment.CurrentDirectory.ToString();
//string path = System.Environment.GetEnvironmentVariable("TEMP");
//if (!path.EndsWith("\\")) path += "\\";
Put log.txt
in a Logs
sub-folder of your web site and then you can get the absolute path like this:
string logFile = HttpContext.Current.Server.MapPath("~/Logs/log.txt");
Also don't forget to restrict the public access to the Logs
folder to avoid anyone reading your application log files.
精彩评论