olsh/resharper-structured-logging

TemplateIsNotCompileTimeConstant should ignore NameOf()

sommmen opened this issue · 7 comments

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.

olsh commented

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.

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)

olsh commented

(would be a cool refactor action to add btw)

Yes, I think this is nice to have feature.
I'll probably implement it when I have some spare time.

@olsh Looks like C# 10 has constant interpolated strings, giving this issue some fresh wind:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/constant_interpolated_strings

@olsh Looks like C# 10 has constant interpolated strings, giving this issue some fresh wind: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/constant_interpolated_strings

Looks pretty good. Perhaps add a fresh issue to discuss this? Or perhaps wait when c# 10 is out.

olsh commented

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

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 🙃