Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom inspection pattern for catch block logging

Tags:

c#

resharper

I have just downloaded the trial of Resharper 7.1. My goal is to enforce a rule where our custom logger must be used on all catch blocks within our c# code-base. Example;

try{
    // Any amount of code
}
catch(Exception e){

}

should be illegal, however:

try{
    // Any amount of code
}
catch(Exception e){
    Logger.LogException(e.Message, e);
}

is perfectly acceptable. To this end, I have the following pattern set up to detect and re-factor.

Search pattern:

try{
    $anystatements$
}
catch($exceptiontype$ $exceptionarg$){
    $anycatchstatements$
}

Replace pattern:

try{
    $anystatements$
}
catch($exceptiontype$ $exceptionarg$){
    Logger.LogException($exceptionarg$.Message, $exceptionarg$)
    $anycatchstatements$
}

Resharper is detecting the smells fine, but is treating the replace pattern as a smell in itself as the added line for Logging is being matched by $anycatchstatement$ placeholder.

How can I define a placeholder to describe "match any number of statements in the catch block which are NOT calls to custom logger, and simply append a call to the logger"?

like image 422
Finch Avatar asked May 09 '13 12:05

Finch


1 Answers

Unfortunatley no, i'm using Resharper 8 EAP (http://confluence.jetbrains.com/display/ReSharper/ReSharper+8+EAP) and it still don't has such option.

Provably you should take a look on Code Contracts, or http://www.postsharp.net/ or something similar.

Also Vladimir Reshetnikov was right - their team can help you a lot. You can easy contact with them trough email or web form. They have really good devs, and company allows direct communications with their customers. They are from Russia :)

like image 76
Vlad Mysla Avatar answered Sep 24 '22 22:09

Vlad Mysla