1

次のコードを含む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)
                  {
                  }
               }
            });            
        }

Test method catch 内で完全なスタック トレースを確認できます

 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

一方、private static void CurrentDomain_FirstChanceException不完全なスタックトレースが表示されます

System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)

try{}catch{}CurrentDomain_UnhandledException イベントを削除しても発生しません

例外をログに記録するためだけに try{}catch{} を実行したくありません。最初の例外で完全なスタック トレースを取得するにはどうすればよいですか?

4

0 に答える 0