The best VPN 2023

The Best VPS 2023

The Best C# Book

How to catch dump when .NET program crashes?

How to catch dump when .NET program crashes? For the dump of catching .NET program crashes, using these three methods can basically meet your requirements.

I have received several crash requests for help in the past few days. Maybe these friends have not played how to generate dumps. They can only teach by hand. It feels that it is not a way. So it is necessary to write a post, and if a friend consults in the follow-up , I can throw this article over, 😏😏😏, well, I probably summarized the following three ways:

  1. procdump -e
  2. procdump -> AEDebug
  3. Windows Error Reporting
How to catch dump when .NET program crashes?
How to catch dump when .NET program crashes?

How to catch dump

In fact, I always recommend procdump to do these things because it is a powerful and smart tool that can be crawled across platforms.

Prepare test cases for .net programs

Dump sample from the point of view, web program like the most, so here I will take Asp.NET MVC 5 as a case in RouteConfig class I use a Timer constantly throws an exception, the purpose is to hang w3wp process deceives, reference code is as follows:

    public class RouteConfig
    {
        public static Timer timer;
        public static void RegisterRoutes(RouteCollection routes)
        {
            timer = new Timer(new TimerCallback(m =>
            {
                var r = 10 / Convert.ToInt32("0");
            }), null, 60000, 5000);

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

By the way, there must be a friend asked: Why not throw an exception in Action, because Http pipeline would this anomaly packaged http 500, so we reach the crash effects.

Next to the deployment and running IIS, you can clearly see Windows Event viewer the success of the crash information is recorded, as shown below:

How to catch dump when .NET program crashes?
How to catch dump when .NET program crashes?

Three: Analysis of 3 crawling methods

1. Use procdump -e

Simple but this way is not stable, because there are a few friends told me, procdump error during crawl in, and because the process has exited, after all this depends on your luck, huh, here -eis exceptionshorthand, For details, please refer to the official document: https://docs.microsoft.com/zh-cn/sysinternals/downloads/procdump.

-e	Write a dump when the process encounters an unhandled exception. Include the 1 to create dump on first chance exceptions.

The complete reference command is as follows:

C:\Windows\system32>procdump -e -ma -w w3wp E:\test

ProcDump v10.0 - Sysinternals process dump utility
Copyright (C) 2009-2020 Mark Russinovich and Andrew Richards
Sysinternals - www.sysinternals.com

Waiting for process named w3wp...

...

Press Ctrl-C to end monitoring without terminating the process.

[21:12:08] Exception: 04242420
[21:12:08] Exception: E0434352.CLR
[21:12:09] Exception: E0434352.CLR
[21:12:09] Exception: E0434352.CLR
[21:12:09] Exception: E0434352.CLR
[21:12:09] Exception: E0434352.CLR
[21:12:09] Exception: E0434352.CLR
[21:12:14] Exception: C0000094.INT_DIVIDE_BY_ZERO
[21:12:14] Unhandled: C0000094.INT_DIVIDE_BY_ZERO
[21:12:14] Dump 1 initiated: E:\test\w3wp.exe_210525_211214.dmp
[21:12:14] Dump 1 writing: Estimated dump file size is 326 MB.
[21:12:15] Dump 1 complete: 326 MB written in 1.2 seconds
[21:12:15] Dump count reached.

It can be seen from the output information that the dump file has been successfully captured. If your machine has multiple w3wp, you can replace it with pid. The reference command is as follows:

C:\Windows\system32>procdump -e -ma 9320 E:\test

2. Use procdump as the default debugger for AeDebug

It’s probably how it works: When the program unhandled exception occurred, then activate the operating system Win32 unhandled exception filter, this filter will call the registry HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug debugger configuration of nodes, that is my Procdump, you want to procdump registry the AeDebug node can be used -ito register.

-i	Install ProcDump as the AeDebug postmortem debugger. Only -ma, -mp, -d and -r are supported as additional options.

The complete reference command is as follows:

C:\Windows\system32>procdump -ma -i E:\test

ProcDump v10.0 - Sysinternals process dump utility
Copyright (C) 2009-2020 Mark Russinovich and Andrew Richards
Sysinternals - www.sysinternals.com

Set to:
  HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug
    (REG_SZ) Auto     = 1
    (REG_SZ) Debugger = "C:\xcode\soft\Procdump\procdump.exe" -accepteula -ma -j "E:\test" %ld %ld %p

Set to:
  HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AeDebug
    (REG_SZ) Auto     = 1
    (REG_SZ) Debugger = "C:\xcode\soft\Procdump\procdump.exe" -accepteula -ma -j "E:\test" %ld %ld %p

ProcDump is now set as the Just-in-time (AeDebug) debugger.

From the output information has been successfully sent to see it to the registry, the next can be opened Registry Editor to verify.

dump 2
How to catch dump when .NET program crashes?

Finally, the web is up and running, you will see the success of after 1min E:\testdump file in the matter, shots are as follows:

dump 3
How to catch dump when .NET program crashes?

From the figure, there are 2 dumps. I don’t care why they are 2 dumps. I’m afraid they won’t be generated. 😏😏😏

3. Generate with Windows Error Reporting

It’s probably how it works by means of windows built-in Windows Error Reporting service to help us generate crash dump program, to be realized, the service must be turned on and configured in the registry exe good program you want to crawl, configure it a bit cumbersome, here There is a bat script that can be run directly, which is simple and rude.

SET DMPPATH=E:\test
SC CONFIG WerSvc START= AUTO
NET START WerSvc
ECHO activation completed
 
REG DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug\Debugger /f
REG DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\DbgManagedDebugger /f
 
REG DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AeDebug\Debugger /f
REG DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\DbgManagedDebugger /f
 
ECHO delete complete
 
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\w3wp.exe" /f
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\w3wp.exe" /t REG_SZ /v DumpFolder /d %DMPPATH% /f
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\w3wp.exe" /t REG_DWORD /v DumpCount /d 2 /f
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\w3wp.exe" /t REG_DWORD /v DumpType /d 2 /f
 
ECHO activation completed
 
PAUSE

There are 3 parameters that need a brief explanation.

  • DumpFolder: storage path of dump
  • DumpCount: Keep a few dump files at most
  • DumpType: 0:Custom dump. 1:Mini dump. 2:Full dump

After the bat is executed, you can go to the registry to verify it.

dump 4
How to catch dump when .NET program crashes?

Next, run the web. After 1 minute, you will see the generated dump file. The screenshot is as follows:

dump 5
How to catch dump when .NET program crashes?

Three: Summary

For the dump that grabs the crash of the .net program, these three methods can basically be foolproof. After the summary, it will save precious time for you and me.😘😘😘.

Reference: https://www.cnblogs.com/huangxincheng/p/14811953.html

Leave a Comment