Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#第一次机会异常不完整堆栈跟踪_C#_.net_Exception_Unhandled Exception_First Chance Exception - Fatal编程技术网

C#第一次机会异常不完整堆栈跟踪

C#第一次机会异常不完整堆栈跟踪,c#,.net,exception,unhandled-exception,first-chance-exception,C#,.net,Exception,Unhandled Exception,First Chance Exception,我有一个windows服务,代码如下 AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; private static void CurrentDomain_FirstChanceException(object send

我有一个windows服务,代码如下

AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

private static void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
    {
          if (e.Exception == null)
            return;

          LogException(e.Exception, "FirstChance Exception ");
    }

    public void Test()
    {
      Task.Factory.StartNew(x =>
            {
               foreach(item in blockingCollection.GetConsumingEnumerable())
               {
                  try
                  {
                     Convert.ToInt32("Test");
                  }
                  catch (Exception ex)
                  {
                  }
               }
            });            
        }
在测试方法捕获中,我可以看到完整的堆栈跟踪

 at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.Convert.ToInt32(String value)
   at Test.TestService.<QueueConsumer>b__11_0(Object x) in C:\SourceControl\TestProject.Risk\Application\TestService.cs:line 157
如果我删除
try{}catch{}
CurrentDomain\u未处理的异常事件不会被激发


我想避免仅仅为了记录异常而尝试{}catch{}。如何在第一次机会异常中获得完整的堆栈跟踪?

您不能。从CurrentDomain_FirstChanceException()方法返回后,缺少的信息将添加到异常中。您不能这样做,因为这是完整的堆栈跟踪(或者更确切地说,只是打包为堆栈跟踪的方法信息)。“First chance”表示在解开堆栈以查找异常处理程序(如果有)之前调用处理程序。除了附加一个调试器并让它通过执行堆栈遍历来处理第一次通知(我很确定您不能从自己的进程内部执行),我认为这是不可能的。考虑使用<代码> catch(Exchange EX){log(Ex);抛出;} < /Cord>模式。这应该谨慎使用,因为处理的异常通常不是您记录的东西。这是有道理的。但即使我删除try-catch,服务也不会崩溃。我已经编辑了
Test()
,并添加了TPL代码。这是因为您是。如果您需要服务在未处理的异常上硬崩溃,请注册一个
.ContinueWith(onlynfaulted)
continuation,该continuation显式创建引发异常的新线程。您也可以使用,但这仅在任务被垃圾收集时触发。或者,当然,只是得到结果。我有几十项任务。如果在所有位置添加continueWith,则效率低下。是否有类似于AppDomain.CurrentDomain.UnhandledException的内容?您不能。从CurrentDomain_FirstChanceException()方法返回后,缺少的信息将添加到异常中。您不能这样做,因为这是完整的堆栈跟踪(或者更确切地说,只是打包为堆栈跟踪的方法信息)。“First chance”表示在解开堆栈以查找异常处理程序(如果有)之前调用处理程序。除了附加一个调试器并让它通过执行堆栈遍历来处理第一次通知(我很确定您不能从自己的进程内部执行),我认为这是不可能的。考虑使用<代码> catch(Exchange EX){log(Ex);抛出;} < /Cord>模式。这应该谨慎使用,因为处理的异常通常不是您记录的东西。这是有道理的。但即使我删除try-catch,服务也不会崩溃。我已经编辑了
Test()
,并添加了TPL代码。这是因为您是。如果您需要服务在未处理的异常上硬崩溃,请注册一个
.ContinueWith(onlynfaulted)
continuation,该continuation显式创建引发异常的新线程。您也可以使用,但这仅在任务被垃圾收集时触发。或者,当然,只是得到结果。我有几十项任务。如果在所有位置添加continueWith,则效率低下。是否有类似于AppDomain.CurrentDomain.UnhandledException的内容?
System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)