c# - How to make NLog terminate application on fatal errors? -
can somehow set event handler fatal errors in code? terminate application if such error happens, i.e. this:
void fail(string format, params object[] args) { logger.fatal(format, args); environment.exit(-1); } however happen automatically:
logger.fatal(...); // logs error , invokes environment.exit(-1); is there way setup kind of callback fatal errors or configuration option this?
yes, can using methodcall target (see https://github.com/nlog/nlog/wiki/methodcall-target)
here example:
namespace somenamespace { using system; public class myclass { public static void handlefataleventlog(string level) { if(level.equal("fatal", stringcomparison.ordinalignorecase)) { environment.exit(-1); } } } } and nlog.config file:
<?xml version="1.0" ?> <nlog xmlns="http://www.nlog-project.org/schemas/nlog.xsd" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <targets> <target name="m" xsi:type="methodcall" classname="somenamespace.myclass, myassembly" methodname="handlefataleventlog"> <parameter layout="${level}" /> </target> </targets> <rules> <logger name="*" minlevel="fatal" writeto="m" /> </rules> </nlog> ps: but, not recommend approach, logger should not shutdown application
Comments
Post a Comment