我想将日志异常从捕获中保留并发送到数据库或文本文件?
try
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
谢谢
这将创建一个文本文件或附加TXT,如果已在抛出异常的日期和时间已经存在。我希望这就是您想要的。
catch(Exception Ex)
{
StreamWriter sw = null;
String Logfile = "C:ExceptionLog.txt";
if (!System.IO.File.Exists(LogFile))
{
sw = File.CreateText(LogFile);
sw.WriteLine(String.Format("Exceptiont DateTime"));
}
else
{
sw = File.AppendText(@"C:ExceptionLog.txt");
}
sw.WriteLine(String.Format("{0}t {1}", Ex,
DateTime.Now.ToString("MM/dd/yyy HH:mm:ss"));
sw.Close();
}
简单地,如果您使用实体,请创建数据库上下文的对象 框架并将其插入表格(例如,包含的表(ID, 异常名称,innermessage,错误编号,createDateTime等)。
catch (Exception ex)
{
DatabaseEntity DbObj = new DatabaseEntity();
ExceptionTable ExObj = new ExceptionTable();
ExObj.ExceptionName = ex.Message;
ExObj.InnerException = ex.InnerException;
.....
//Code according to your need
.....
DbObj.ExceptionTable.Add(ExObj);
DbObj.SaveChanges();
MessageBox.Show(ex.Message);
}
您可以关注此 如何在文件中记录异常? 这可能会帮助您