如何使日志异常不被捕获并将日志发送到数据库 C#?

how to keep logs exception from catch and send logs to database c#?

我想防止日志异常被捕获并发送到数据库或文本文件?

try
{
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message);  
}

谢谢

Simply, create object of your database context if you are using Entity framework and insert it in your table(e.g. a table containing(id, Exception Name, innermessage, error number, CreatedDateTime, etc)).

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);  
}

you may follow this How to Log Exception in a file?, this may help you

这将创建一个文本文件或追加 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("Exception\t 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();
            }