Distinguish exceptions and determine what to log

363 Views Asked by At

Is there any known approach for logging different information depending on the exception type?

The idea would be only log relevant information per exception type. eg: known exceptions/business logic exceptions don't need a stack trace logged, since they are "expected"

Basically what I'm asking is if there are any known practices while logging exceptions to make the log as helpful as possible w/o any trash.

1

There are 1 best solutions below

0
On

You can filter in the <rules>, e.g.

<rules>
    <logger name="*" writeTo="file1">
        <filters>
            <when condition="'${exception:format=Type}' == 'ExpectedException' " action="LogFinal" />
            <when condition="true" action="Ignore" />

        </filters>
    </logger>
    <logger name="*" writeTo="file2">
        <filters>
            <when condition="'${exception:format=Type}' == 'OtherException' " action="LogFinal" />
            <when condition="true" action="Ignore" />

        </filters>
    </logger>    
</rules>

See when filter and conditions docs.