9

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"?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Finch
  • 91
  • 2
  • 1
    This looks bad for you: http://stackoverflow.com/questions/11657922/resharper-custom-pattern-replacement-with-negative-equality-expression I think it's effectively a dupe, in a roundabout fashion. – spender May 09 '13 at 13:21
  • Ah damnit! I spent so much time on searches, didn't think of 'negative equality'! That said, there is no agreeable answer to my solution on that thread other than 'don't do it'. – Finch May 09 '13 at 13:52
  • 2
    File a feature request here: http://youtrack.jetbrains.com/issues/RSRP - ReSharper Team is usually very quick on fixing important issues. – Vladimir Reshetnikov May 09 '13 at 14:22
  • 1
    The lack of responses / resources on this matter pretty much answers my "is Resharper worth the cost" query anyway. I would expect something like this to be in the base set of features for a tool charging similar to ReSharper. Looks like FxCop may be more applicable in this instance. – Finch May 15 '13 at 15:18
  • 1
    I would think carefully about doing this anyway. I've seen people do things like this before and end up with terrible log spam in the case where exceptions are regularly rethrown. Often mechanical approaches to these kind of issues are problematic. Would it not be more appropriate to take a more case-by-case approach and enforce via code reviews? – jamesmus May 22 '13 at 08:37
  • @jamesmus - I agree that logging output can be a bit of a headache to wade through. However, I feel we have our outputs configured in such a manner that this is a non-issue. Issue with code reviews are that a) It's done by humans; they get tired, miss things, have friends etc. Plus it absorbs time and money. b) As part of the 'bigger picture', we would look to include this as part of a continuous integration process meaning that offensive code will never reach build and uat environments, regardless of weather a senior tried to sneak it past code-review or it was comited at 4am from home. – Finch May 22 '13 at 15:38
  • @Finch: You kind of act like "I have a problem and need to blame someone". What you encounter here is a recursive or self-contained pattern. You want to solve the occurence of a pattern by replacing it with the pattern itself. Sorry to tell, but if you don't get your programmers writing code for smart logging, you're part of the problem. – alzaimar May 24 '13 at 05:21
  • @Finch Are you implying by "Plus it absorbs time and money" that code reviews are not cost effective? If so, I think you need to hire better developers. I've also agreed (up voted) all the comments here that suggest you tackle this problem in a different way. FWIW, logging exceptions and continuing regardless is a terrible pattern. – Bernhard Hofmann Oct 16 '14 at 07:21

1 Answers1

1

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 :)

Vlad Mysla
  • 1,181
  • 12
  • 15