简体   繁体   English

如何在 ASP.NET MVC C# 中捕获有关运行时错误的错误消息

[英]How to catch the error message on Runtime error in ASP.NET MVC C#

Please help me to catch the error message on my MVC ASP.NET C# application My application perfectly running on my development machine but when I publish and deploy it on the server I always get the error below.请帮助我在我的 MVC ASP.NET C# 应用程序上捕获错误消息 我的应用程序在我的开发机器上完美运行,但是当我在服务器上发布和部署它时,我总是收到以下错误。 I want to catch the exact error message I'm already using log4net for logging but no log from this error我想捕获确切的错误消息,我已经在使用 log4net 进行日志记录,但没有来自此错误的日志

Server Error in '/' Application. “/”应用程序中的服务器错误。

Runtime Error Description: An application error occurred on the server.运行时错误描述:服务器上发生应用程序错误。 The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons).此应用程序的当前自定义错误设置可防止远程查看应用程序错误的详细信息(出于安全原因)。 It could, however, be viewed by browsers running on the local server machine.但是,它可以被本地服务器机器上运行的浏览器查看。

Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application.详细信息:要在远程计算机上查看此特定错误消息的详细信息,请在当前 web 应用程序的根目录中的“web.config”配置文件中创建一个标签。 This tag should then have its "mode" attribute set to "Off".然后,此标记应将其“模式”属性设置为“关闭”。

<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

Notes : The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.注意:您看到的当前错误页面可以通过修改应用程序配置标签的“defaultRedirect”属性以指向自定义错误页面 URL 来替换为自定义错误页面。

<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>

One way to catch "in the field" errors is to create an exception handler and send the exception details somewhere useful (a Slack channel in my case but you could just use a text file)捕获“现场”错误的一种方法是创建一个异常处理程序并将异常详细信息发送到有用的地方(在我的情况下是一个 Slack 通道,但您可以只使用文本文件)

First, set up the event handlers (in Main() or somewhere else that runs at startup)首先,设置事件处理程序(在 Main() 或在启动时运行的其他地方)

// to only catch when not debugging
if (!System.Diagnostics.Debugger.IsAttached)
{
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

     //to catch anywhere in the application
     Application.ThreadException += new ThreadExceptionEventHandler(CurrentDomain_UnhandledException);
}

Then setup your exception handler to post the exception details somewhere useful.然后设置您的异常处理程序以将异常详细信息发布到有用的地方。 Could be a text file, I'm using Slack WebHooks可能是文本文件,我正在使用 Slack WebHooks

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                SlackClient slackClient = new SlackClient("Your-Webhooks-URL");
                SlackMessage message = new SlackMessage
                {
                    Text = (e.ExceptionObject as Exception).Message.ToString() + "\n" + (e.ExceptionObject as Exception).StackTrace.ToString()
                };
                slackClient.Post(message);
                throw e.ExceptionObject as Exception;
            }

You can setup a second handler to catch ThreadExceptionEventArgs as well if you want to catch exceptions anywhere in your application.如果您想在应用程序的任何位置捕获异常,您也可以设置第二个处理程序来捕获ThreadExceptionEventArgs Just overload CurrentDomain_UnhandledException只需重载CurrentDomain_UnhandledException

static void CurrentDomain_UnhandledException(object sender, ThreadExceptionEventArgs e)
        {
            SlackClient slackClient = new SlackClient("Your-Webhooks-URL");
            SlackMessage message = new SlackMessage
            {
                Text = e.Exception.Message.ToString() + "\n" + e.Exception.StackTrace.ToString()
            };
            slackClient.Post(message);
            throw e.Exception;
        }

For Slack messaging I'm using Slack.Webhooks on NuGet https://www.nuget.org/packages/Slack.Webhooks/1.1.4对于 Slack 消息传递,我在 NuGet https://www.nuget.org/packages/Slack.Webhooks/1上使用 Slack.Webhooks。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM