Search code examples
c#-4.0exceptioncastle-dynamicproxy

c# how to stop exception from bubbling up


when exception is thrown in my controller, action's are getting executed again and again, when error is thrown this cycle is going on.

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

Solution

  • If you want the exception to stop bubbling up, don't re-throw it. In your code there is the : "throw ;" line. Remove it.