2

ASP.NET の既定のエラー ハンドラーは、誰もがよく知っています。次のように、ソース エラー (エラーが発生した 5 行のコード) とソース ファイル (ファイル名と行番号) を含む黄色のボックス:

Source Error:

Line 48:         public ActionResult TriggerException()
Line 49:         {
Line 50:            throw new SystemException("This is a generated exception to test the global error handler.");
Line 51:         }
Line 52:         


Source File: c:\MyApp\Controllers\TestToolsController.cs    Line: 50 

カスタム エラー ハンドラを作成していて、これらと同じ情報を取得したいのですが、それらは例外オブジェクトに含まれていません。これらのアイテムを取得する方法を知っている人はいますか?

4

1 に答える 1

6

行番号は Exception 自体では利用できませんが、次のように StackTrace で利用できます。

try
{
    // code that throws an Exception here
}
catch (Exception exc)
{
    var frame = new StackTrace(exc, true).GetFrame(0); // where the error originated
    var lineNumber = frame.GetFileLineNumber();
    // Handle line numbers etc. here
}
于 2013-01-29T15:17:41.053 に答える