简体   繁体   中英

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. I want to catch the exact error message I'm already using log4net for logging but no log from this error

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. 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.

<!-- 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)

First, set up the event handlers (in Main() or somewhere else that runs at startup)

// 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

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. Just overload 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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