public class ErrorLog
{
//新增log的txt檔,並將錯誤訊息寫入,存進資料夾中
public void Log(string error_message,string log_path)
{
string full_log_path = log_path + ".txt";
if (!File.Exists(full_log_path))
{
using (StreamWriter sw = File.CreateText(full_log_path))
{
sw.WriteLine(error_message);
}
}
}
}
//將錯誤訊息錯誤訊息寫入log的txt檔中
ErrorLog error_log = new ErrorLog();
string logSavedPath = WebConfigurationManager.AppSettings["LogPath"];
string newFileName = string.Concat(
DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));
log_path = Path.Combine(Server.MapPath(logSavedPath), newFileName);
error_log.Log(error_message,log_path);