question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TemplateIsNotCompileTimeConstant should ignore NameOf()

See original GitHub issue

Hiya,

I often have messagetemplates like:

_logger.LogError(e, $"Caught exception in {nameof(PostStock)}");

Where instead of typing PostStock as constant string, I tend to use NameOf which is useful because the name may change. This could also include SizeOf - but i’m not sure whether that is a compile time variable as NameOf is… The docs mention ‘constants’ but still…

Related:

https://github.com/olsh/resharper-structured-logging/blob/f7ac19c8af6d15e5a2314d27a7cd9bef9082a3a1/rules/TemplateIsNotCompileTimeConstantProblem.md

I would love if the warning wouldn’t show up in the case of NameOf.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sommmencommented, Aug 5, 2021

Hi @sommmen

I believe this is the same issue #9 Would you mind checking the discussion and letting me know if it answers your question?

In a nutshell - the interpolation string is not constant per se.

Seems you’re right!

The following works:

_logger.LogError(e, "Caught exception in " + nameof(PostStock));

I’ll use that from now on. Thanks for your support! (would be a cool refactor action to add btw)

0reactions
sommmencommented, Feb 17, 2022

The extension works correctly with constant interpolated strings already. #9 (comment)

Lol I thought the latest version was c#9 for some reason. Funny I’ll try this out tomorrow although I’ve now gotten used to writing logs in the concatenated version 🙃

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Should be a compile time constant" warning when using ...
Yes, the nameof expression is evaluated at compile-time, but the interpolation is not. The compilation of this code fails. const string a =...
Read more >
CA1507: Use nameof instead of string (code analysis) - .NET
Rule CA1507 flags the use of a string literal as an argument to a method or constructor where a nameof ( NameOf in...
Read more >
Code Inspection: Use 'nameof' expression to reference name
The nameof operator, added in C# 6.0, addresses this — it allows capturing the string names of symbols that are in the scope....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found