Reputation: 1413
We use some extension methods, which allows us to write something like:
.Single(e => $"{nameof(SomeParameter)}: {SomeParameter}, ...")
instead of just
.Single()
where e
is an exception.
This greatly increases logging experience because the upper variant logs all parameters instead of useless "Sequence contains no elements", which the bottom one produces.
I need to write a test, which would find all non-compliant code, e.g. calls to .Single()
.
Sure, I can use a regex search or just a plain vanilla text search to look in all CS files starting from the solution root and maybe this is the way to go. I just wonder if there is already a simple and known solution for that.
Here is what I need:
Easily add / remove rules: e.g. find all files, which have .Single()
, find all which have .Single(
but which is not in the form of .Single(e => $"
- this is to account for a combined Where + Single code, etc...
The test(s) should run from xUnit.
Thanks a lot!
Upvotes: 0
Views: 67
Reputation: 888185
You should write a Roslyn analyzer, which will run as part of your build (and as you type in Visual Studio) and give compilation errors (or warnings) as you like.
The analyzer has full access to the syntax tree and semantic model as generated within the compiler, and can check for accesses to specific methods.
Upvotes: 5