We have a program with thousands of lines such as:
Console.Write("{0},{1} - {2}", e1, "My custom error", e2); // Write a custom error
However, the following will throw an error, as “{3}” is greater than the number of parameters:
Console.Write("{0},{1} - {3}", e1, "My custom error", e2); // Write a custom error
How can I scan my entire project to pick this up at compile time?
Update:
Unfortunately, ReSharper won’t work. I’ve created my own logging class, “w”, which calls Console.Write and other logging behind the scenes. ReSharper picks up errors in “Console.Write”, but ignores any parameters for said logging method.
Update:
FXCop doesn’t pick up this sort of error. That’s surprising – why not pick up an obvious error which will result in the entire program crashing with an exception?
Update:
Thanks for the tip @adudley – I ended up replacing all instances of “Console.Write” and “String.Format”, in the logging class, with calls to specialized versions with try/catch for any errors. This avoids the entire program falling over if a single, rarely used logging function is called with bad parameters. I’d rather tell the program to ensure its bulletproof, rather than manually check 1000 logging functions for difficult-to-spot errors.
you’ve got to ask why would they be wrong? They should be correct at the time of typing, and if you translate them, check when importing translations (that’s what we do).