2

I'd like to enforce a rule that log statements do not use string interpolation:

Example Log($"Added {count} messages.");

Instead I want to encourage/force developers to use structured logging, which means passing values in as separate arguments to a Log method that supports this concept.

How would one use NDepend to do this, assuming it's possible? If not NDepend, can it be done with a Roslyn analyzer?

ssmith
  • 8,092
  • 6
  • 52
  • 93

2 Answers2

1

SerilogAnalyzer is a Roslyn-based analyzer that will do that for you.

SerilogAnalyzer Example

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
0

Since string interpolation results in a call to System.String.Format(String,Object) you can write such a rule with NDepend CQLinq:

//<Name>Don't use string interpolation from Log method</Name>
warnif count > 0 
from m in Application.Methods where m.IsUsing ("System.String.Format(String,Object)")
where m.ParentNamespace.Name.Contains(".Log")
select m

String interpolation calls string format

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92