❔ Logging Sudden Application Shutdown
Hey guys,
I would like to ask if there is a possibility to log the event of an unexpected application shutdown? I'm aware of IHostApplicationLifetime which exposes OnStopping and OnStopped but they only trigger when the app is shutting down gracefully.
14 Replies
This probably isn't the "correct" way of doing this, but if you go in to the app's properties, under debug you can set an environment variable for
DOTNET_EnableCrashReport
with a value 1 and it will create a crashreport.json
file on crash
if it's not a .NET, the variable name is COMPlus_EnableCrashReport
@BokoGanga ping for good measureThis is an option I havent seen before. Yes, it is a dotnet app. I will test it and get back to you. Thank you very much.
you have to differentiate between the reasons the application shut down
is it an unhandled exception?
then you could use a unhandledexceptionhandler
did the process got a SIGTERM event? i think the OS provides something to catch that
is it a SIGKILL?
to my knowledge you are out of luck
probably not the right place to ask this, but can you point me in the direction of a good guide for an unhandledexceptionhandler?
right now i just wrap entire threaded methods in a trycatch and it feels crude
I have already an exception handler that handles most of my app's exceptions, but due to the nature of the app, I need logs for when the source of the crashes is outside the app's logic.
this seems to only work with .NET6 and above. Mine is on .NET5 unfortunately
COMPlus should still work on .NET 5
Dumps - .NET
An introduction to dumps in .NET.
https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.unhandledexception
it doesnt catch everything tho.. stackoverflows and iirc outofmemoryexceptions result in a hard crash anyways
AppDomain.UnhandledException Event (System)
Occurs when an exception is not caught.
ty 🙂
https://learn.microsoft.com/en-us/windows/win32/wer/collecting-user-mode-dumps this may also prove useful if you're running the app locally. if you're trying to have end users create crash dumps, it's different
Collecting User-Mode Dumps - Win32 apps
Starting with Windows Server 2008 and Windows Vista with Service Pack 1 (SP1), Windows Error Reporting (WER) can be configured so that full user-mode dumps are collected and stored locally after a user-mode application crashes.
for local analysis i also recommend https://learn.microsoft.com/en-us/sysinternals/downloads/procdump which can create full dumps on crashes even stackoverflows and outofmemory
ProcDump - Sysinternals
This command-line utility is aimed at capturing process dumps of otherwise difficult to isolate and reproduce CPU spikes.
sysinternals is life
I will check those out and play around with them. Thanks a lot guys. I really appreciate it
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.