自定义异常处理数据库插入

时间:2014-12-17 20:18:58

标签: c# sql-server exception exception-handling

我希望你理解我

我有两个问题

1如何实现自定义异常处理?

2我有这张桌子

ID ExceptionName 班级名称 方法名 字段名 的ErrorMessage

我想记录以下表格的异常: 什么是错误页面? 什么是错误类名?
什么是错误方法和字段名称是什么?我如何获得Exception详细信息? 获取类别名称和方法名称从异常详细信息发生异常

MSSQL + C#mvc framework 4.0

谢谢

1 个答案:

答案 0 :(得分:0)

这是你必须在catch块中调用的函数,

 public static void LogException(Exception exc, string source)
{
    // Include enterprise logic for logging exceptions 
    // Get the absolute path to the log file 
    string logFile = "App_Data/ErrorLog.txt";
    logFile = HttpContext.Current.Server.MapPath(logFile);

    // Open the log file for append and write the log
    StreamWriter sw = new StreamWriter(logFile, true);
    sw.WriteLine("********** {0} **********", DateTime.Now);
    if (exc.InnerException != null)
    {
        sw.Write("Inner Exception Type: ");
        sw.WriteLine(exc.InnerException.GetType().ToString());
        sw.Write("Inner Exception: ");
        sw.WriteLine(exc.InnerException.Message);
        sw.Write("Inner Source: ");
        sw.WriteLine(exc.InnerException.Source);
        if (exc.InnerException.StackTrace != null)
        {
            sw.WriteLine("Inner Stack Trace: ");
            sw.WriteLine(exc.InnerException.StackTrace);
        }
    }
    sw.Write("Exception Type: ");
    sw.WriteLine(exc.GetType().ToString());
    sw.WriteLine("Exception: " + exc.Message);
    sw.WriteLine("Source: " + source);
    sw.WriteLine("Stack Trace: ");
    if (exc.StackTrace != null)
    {
        sw.WriteLine(exc.StackTrace);
        sw.WriteLine();
    }
    sw.Close();
}   

请参阅链接:http://msdn.microsoft.com/en-us/library/bb397417(v=vs.140).aspx