質問

私のMVC Webプロジェクトで

。私は、web.configファイルで「custromerrors」要素を使用せずに、私の訪問者にカスタムエラーページを表示しようとしています。

私は

以下のような例外をキャッチすることができます
protected void Application_Error(object sender, EventArgs e)
{

    Exception exception = Server.GetLastError();

    bool success = RaiseErrorSignal(exception);

    Response.Clear();

    HttpException httpException = exception as HttpException;

    RouteData routeData = new RouteData();
    routeData.Values.Add("controller", "Error");

    if (httpException == null)
    {
        routeData.Values.Add("action", "Index");
    }
    else //It's an Http Exception, Let's handle it.
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                // Page not found.
                routeData.Values.Add("action", "Error404");
                break;
            case 500:
                // Server error.
                routeData.Values.Add("action", "Error500");
                break;

            // Here you can handle Views to other error codes.
            // I choose a General error template  
            default:
                routeData.Values.Add("action", "Index");
                break;
        }
    }

    // Pass exception details to the target error View.
    routeData.Values.Add("error", exception);

    // Clear the error on server.
    Server.ClearError();

    // Call target Controller and pass the routeData.
    IController errorController = new ProjectName.WebSite.Controllers.ErrorController();
    errorController.Execute(new RequestContext(
         new HttpContextWrapper(Context), routeData));


}

private static bool RaiseErrorSignal(Exception e)
{
    var context = HttpContext.Current;
    if (context == null)
        return false;
    var signal = ErrorSignal.FromContext(context);
    if (signal == null)
        return false;
    signal.Raise(e, context);
    return true;
}

しかし、ELMAH iは、誤差信号を募集もエラーを記録カントます。

役に立ちましたか?

解決

私は、web.configファイルのセクションを逃し、問題を発見しました。私は<system.webserver><modules>に "のErrorLog" モジュールを追加しました。

私も<system.web><httpModules>に追加する必要があります。

追加した後、ELMAHは、エラーのログを開始します。

また、私はErrorSignal.Raise()メソッドを呼び出す必要はありません

、ELMAHは、シグナリングせずにエラーを検出することができます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top