这是我用来记录应用程序异常的方法。它在Visual Studio中不起作用,但在IIS中运行良好。如果我改变格式" PresentMonth"和"文件名" ,它在VS中工作,但在IIS上不工作。我不知道什么是错的。
public static void WriteLog(string sExceptionName)
{
try
{
StreamWriter log;
//String Filepath = "C:\\Quasar_Logs\\";
String Filepath = ConfigurationManager.AppSettings["FilePath"].ToString();
String PresentMonth = DateTime.Now.ToString("-MMM-yy");
String Filename = "Quasar_Log"+ PresentMonth +".txt";
if (!File.Exists(Filepath + Filename))
{
log = new StreamWriter(Filepath + Filename);
}
else
{
log = File.AppendText(Filepath + Filename);
}
log.WriteLine("Data Time:" + DateTime.Now +"\n\n");
//log.WriteLine("Exception Name:" + sExceptionName);
log.WriteLine(sExceptionName);
//log.WriteLine("Error Line No.:" + nErrorLineNo);
log.Close();
}
catch (Exception ex)
{
HttpContext.Current.Response.Redirect("../Access/UnabletoLog.htm");
WriteLog("Line Number: " + ex.ToString());
}
}
答案 0 :(得分:0)
如果文件不存在,首先创建目录,matbe目录不存在,SreamWriter创建文件而不是文件所在的目录。这对我有用:
if (!File.Exists(FilePath + FileName))
{
Directory.CreateDirectory(FilePath);
log = new StreamWriter(FilePath + FileName);
}
else
{
log = File.AppendText(FilePath + FileName);
}