如何将异常保存到txt文件中?

6 浏览
0 Comments

如何将异常保存到txt文件中?

public DataTable InsertItemDetails(FeedRetailPL objFeedRetPL)

{

DataTable GetListID = new DataTable();

try

{

SqlParameter[] arParams = new SqlParameter[4];

arParams[0] = new SqlParameter("@Date", typeof(DateTime));

arParams[0].Value = objFeedRetPL.requestdate;

}

catch (Exception ex)

{

string dir = @"C:\Error.txt"; // 文件夹位置

if (!Directory.Exists(dir))

{

Directory.CreateDirectory(dir);

File.AppendAllText(Server.MapPath("~/Error.txt"), "异常信息:" + ex.Message + "
" + Environment.NewLine + "堆栈跟踪:" + ex.StackTrace +

"" + Environment.NewLine + "日期:" + DateTime.Now.ToString());

string New = Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine;

File.AppendAllText(Server.MapPath("~/Error.txt"), New);

}

}

}

0