|
|
Posted on 2022-11-6 22:41:42
|
|
|
|

Requirements: When developing applications using .NET/C#, it is impossible for any developer to ensure stable and robust execution of the program, which may be related to the developer's code logic or the user's running environmentThere is no 100% guarantee that the application you develop will be problem-freeIt is not terrible to have a problem, but the terrible thing is that there is a problem that cannot find out the cause and solve the problem.
If the application is not executed as expected and an abnormal exit occurs, how can a DMP dump file be automatically generated to troubleshoot the problem?
A dump is a file that contains a snapshot of the process when it was created and can be used to check the state of the application. When it's difficult to attach a debugger to a .NET application, such as a production or CI environment, you can use dumps to debug the application. Using dumps captures the state of problematic processes, and the state can be checked directly without stopping the application.
Collect dumps
You can use environment variables to configure your application to collect dumps in the event of a failure. This is helpful if you want to know the cause of the failure. For example, capturing dumps when an exception is thrown helps identify issues by checking the app state if it fails.
The following table shows the environment variables that you can use to configure your application to collect dumps in the event of a failure.
environment variable | illustrate | Default | | COMPlus_DbgEnableMiniDump or DOTNET_DbgEnableMiniDump | If set to 1, enable core dump generation. | 0 | | COMPlus_DbgMiniDumpType or DOTNET_DbgMiniDumpType | The type of dump to collect. See the table below for details | 2 (MiniDumpWithPrivateReadWriteMemory) | | COMPlus_DbgMiniDumpName or DOTNET_DbgMiniDumpName | Write the file path to the dump. Ensure that the user running the dotnet process has write access to the specified directory. | /tmp/coredump.<pid> | | COMPlus_CreateDumpDiagnostics or DOTNET_CreateDumpDiagnostics | If set to 1, enable diagnostic logging for the dump process. | 0 | | COMPlus_EnableCrashReport or DOTNET_EnableCrashReport | (Requires .NET 6 or later) If set to 1, the runtime generates a JSON-formatted failure report that includes information about the threads and stack frames of the failed application. The crash report name is the dump path/name appended to .crashreport.json. | | | COMPlus_CreateDumpVerboseDiagnostics or DOTNET_CreateDumpVerboseDiagnostics | (Requires .NET 7 or later) If set to 1, enable detailed diagnostic logging for the dump process. | 0 | | COMPlus_CreateDumpLogToFile or DOTNET_CreateDumpLogToFile | (.NET 7 or later required) The file path to the diagnostic message should be written. If not set, a diagnostic message is written to the console of the failed application. | |
For these environment variables, .NET 7 standardizes the prefix DOTNET_ instead of COMPlus_. However, the COMPlus_ prefix will continue to work properly. If you areIn earlier versions of the .NET runtime, environment variables should still use the COMPlus_ prefix。
Create a new .NET Core 6 application test
Use VS 2022 to test a new .NET Core 6 app test with the following code:
Start the program using the cmd command, as shown in the image below:
COMPlus_EnableCrashReport=1 COMPlus_DbgEnableMiniDump=1 COMPlus_DbgMiniDumpName="C:\Users\itsvse_pc\AppData\Local\Temp\NET\test.dmp" COMPlus_CreateDumpDiagnostics=1 Unhandled exception. System.Exception: itsvse.com at Program. <Main>$(String[] args) in C:\Users\itsvse_pc\source\repos\WindowsFormsApp1\DumpDemo\Program.cs:line 9 [createdump] Writing minidump with heap to file C:\Users\itsvse_pc\AppData\Local\Temp\NET\test.dmp [createdump] Dump successfully written
You can see that after the program crashes abnormally, it is successfully under the directory we specifyThe dump .dmp file is generatedAs for how to analyze it, it's a different matter.
Windows Platform Universal Settings
In Windows, Windows Error Reporting (WER) can be configured to generate dumps when an application crashes.
This method works for all programs, not just . .NET programs, such as C++, Go, etc.; And and. NET、. NET Core version
- Open regedit.exe
- Open the directory HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps
- Create a KEY DumpFolder of type REG_EXPAND_SZ to configure the directory where the dump files are stored
- You can also create a KEY DumpCount of type REG_DWORD configure the total amount of dumps
Of course, you can also configure these using PowerShell commands:
According to the above configuration, if the program exits abnormally, it will create a program dump in the %LOCALAPPDATA%\CrashDumps directory.
Resources:
The hyperlink login is visible.
The hyperlink login is visible.
The hyperlink login is visible.
|
Previous:Detailed explanation of the storage rules for Discuz attachment images, avatar pictures, and album imagesNext:Practical Operation: Use BundleTransformer to replace System.Web.Optimization
|