You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
With DE0002 How else are we going to be able to register the UnhandledException events on all platforms? #231
With DE0002 being given to projects that uses using System.Security.Permissions; just to be able to register an unhandled exception event and to control appdomains (which registering that event requires). How else are we going to be able to use that event without using the CAS that is says not to use?
I think the documentation should include ways of using that event without using CAS then...
consider a minimal example being a console application like so:
make a new console application dotnet new console and then add the package Microsoft.DotNet.Analyzers.Compatibility at version 0.2.12-alpha.
Replace the Program.cs file with the following code:
usingSystem;usingSystem.IO;usingSystem.Security.Permissions;usingSystem.Diagnostics;namespaceConsoleApplication1{publicstaticclassProgram{[SecurityPermission(SecurityAction.Demand,Flags=SecurityPermissionFlag.ControlAppDomain)]publicstaticvoidMain(string[]args){AppDomain.CurrentDomain.UnhandledException+=newUnhandledExceptionEventHandler(MyHandler);thrownewException("Unhandled.")}privatestaticvoidMyHandler(objectsender,UnhandledExceptionEventArgsargs){// log the exception to a file.vare=(Exception)args.ExceptionObject;_=Directory.CreateDirectory("logs");vartext=string.Empty;text+=e.GetType().Name+": "+e.Message;text+=e.StackTrace!;GetInnerException(text,e.InnerException);File.WriteAllText($"logs{Path.DirectorySeparatorChar}UnhandledExceptions.txt",text);Debug.WriteLine(e.GetType().Name+": "+e.Message);Debug.WriteLine(e.StackTrace);}// this is to recurse and capture all inner exceptions.privatestaticvoidGetInnerException(string?text,Exception?ex){if(ex!=null){text+="Inner Exception:\n";text+=ex.GetType().Name+": "+ex.Message;text+=ex.StackTrace!;Debug.WriteLine("Inner Exception:\n");Debug.WriteLine(ex.GetType().Name+": "+ex.Message);Debug.WriteLine(ex.StackTrace);GetInnerException(text,ex.InnerException);}}}
changed the title [-]With DE0002 How else are we going to be able to register the UnhandledException events?[/-][+]With DE0002 How else are we going to be able to register the UnhandledException events on all platforms?[/+]on May 30, 2020
Registering a handler for AppDomain.UnhandledException on .NET Framework does not require SecurityPermissionAttribute if the application domain already grants full trust to all assemblies; see AppDomain.IsFullyTrusted. So if you don't need to support partial-trust scenarios, you can delete that.
Activity
[-]With DE0002 How else are we going to be able to register the UnhandledException events?[/-][+]With DE0002 How else are we going to be able to register the UnhandledException events on all platforms?[/+]KalleOlaviNiemitalo commentedon Jun 19, 2020
Registering a handler for AppDomain.UnhandledException on .NET Framework does not require SecurityPermissionAttribute if the application domain already grants full trust to all assemblies; see AppDomain.IsFullyTrusted. So if you don't need to support partial-trust scenarios, you can delete that.
Your code sample seems to be using C# 8, which is not supported on .NET Framework anyway.
KalleOlaviNiemitalo commentedon Jun 19, 2020
dotnet/dotnet-api-docs#4384 is removing the SecurityPermissionAttribute from the AppDomain.UnhandledException example.
AraHaan commentedon Jun 19, 2020
It is targeting .NET Core 3.1 and uses that to use that event for unhandled exceptions.
But I also plan to switch it to
net5.0eventually though.