2017-03-22 75 views
0

當在我的控制器中引發異常時,當循環正在進行時,動作的執行會一次又一次地執行。c#如何防止異常冒泡

try 
    { 
        LogManager.Info("Call:" + invocation.TargetType.Name + "::" + invocation.Method.Name); 
        var startTime = DateTime.Now; 
        invocation.Proceed(); 
        var endTime = DateTime.Now; 
        LogManager.Info("CallEnd:" + invocation.TargetType.Name + "::" + invocation.Method.Name + " Execution Time(Milliseconds): " + (endTime - startTime).Milliseconds); 
       } 
       catch (Exception ex) 
       {     
        //HttpContext.Current.Request.Abort(); 
        var builder = new StringBuilder(); 
        var dataSource = ConfigurationManager.ConnectionStrings["dbMain"].ToString().Split(';')[0]; 

        builder.AppendLine(); 
        builder.AppendLine(string.Concat(Enumerable.Repeat(">", 50))); 
        builder.AppendLine("Server Time:-" + DateTime.Now); 
        //builder.AppendLine("Requested Url:" + HttpContext.Current.Request.Url); 
        builder.AppendLine(dataSource); 
        builder.AppendLine("Error at " + invocation.TargetType.Name + "::" + invocation.Method.Name + "(" + JsonConvert.SerializeObject(invocation.Arguments, Formatting.Indented) + ")"); 
        builder.AppendLine(string.Concat(Enumerable.Repeat("-", 50))); 
        builder.AppendLine(JsonConvert.SerializeObject(ex, Formatting.Indented).Replace("\\n", System.Environment.NewLine)); 
        builder.AppendLine(string.Concat(Enumerable.Repeat("<", 50))); 

        LogManager.Error(builder.ToString()); 
        LogManager.SendMail(ex, builder.ToString()); 

        throw; 
       } 
+1

「我想停止冒泡並重復執行請求多次的異常」X 4 - 你是否想要元或什麼? –

回答

0

如果你想讓異常停止冒泡,不要重新拋出它。 在你的代碼中有: 「扔」;線。去掉它。