c# - Windows Console Application Global IErrorHandler -
i trying find way make exceptions in console application pass trough ierrorhandler (i use denomination because in wcf kind of operation possible ierrorhandler).
i searched on google results seem return wcf solutions.
what i'm looking solution little bit this console application, in wcf looks easy because can configure behavior each endpoint in windows application think kind of solution can reachable.
all exceptions cached, legacy code , calling method handle exception in every catch not solution problem.
thanks in advance.
you can either bubble exceptions root of console application , wrap in big try/catch handles exceptions, or @ start of console application can add event handler app domain "unhandled exception" event , exceptions go unhandled routed there.
using system; using system.security.permissions; public class example { [securitypermission(securityaction.demand, flags=securitypermissionflag.controlappdomain)] public static void main() { appdomain currentdomain = appdomain.currentdomain; currentdomain.unhandledexception += new unhandledexceptioneventhandler(myhandler); try { throw new exception("1"); } catch (exception e) { console.writeline("catch clause caught : {0} \n", e.message); } throw new exception("2"); } static void myhandler(object sender, unhandledexceptioneventargs args) { exception e = (exception) args.exceptionobject; console.writeline("myhandler caught : " + e.message); console.writeline("runtime terminating: {0}", args.isterminating); } }
update
not lot of great simple options doing you're trying do. 1 thing try out installing msysgit / cygwin , use unix tools find replace occurrences. pretty experimental , you'd have heavy testing.
find <project dir> -type f -exec sed -i '.bak' 's/catch\(exception ex\)\r\n{/catch\(exception ex\)\r\n{\r\nmodernexceptionhandler.handle(ex);\r\n/g' {} +
which should recursively walk project directories , replace occurrences of -
catch(exception ex) {
with
catch(exception ex) { modernexceptionhandler.handle(ex);
files modified have backup version saved alongside them .bak file extension.
more info on find / sed
Comments
Post a Comment