Comments (6)
Well, after some research, I think that it's not a mistake.
Yes, the nameof
expression is evaluated at compile-time, but the interpolation is not.
The compilation of this code fails
const string a = $"Hello {nameof(String)}";
But this code is valid
const string a = "Hello " + nameof(String);
You may say that this irrelevant in the context of Serilog templates. Because both interpolation and concatenation produce an immutable string, but that's not completely true either.
Let's look at IL for the code
Console.WriteLine($"Hello {nameof(String)}");
On .NET Core 3.1+ it creates a constant
IL_0000: ldstr "Hello String"
IL_0005: call System.Console.WriteLine
IL_000A: ret
But on old full .NET Framework string.Format()
is called internally
IL_0000: ldstr "Hello {0}"
IL_0005: ldstr "String"
IL_000A: call System.String.Format
IL_000F: call System.Console.WriteLine
IL_0014: ret
And this is a performance issue
https://nblumhardt.com/2014/09/how-not-to-parameterize-serilog-events/
Donβt do this. While never particularly good logging practise (the string concatenation occurs regardless of whether logging is enabled or not, wasting RAM and CPU cycles), with Serilog, this is strongly considered an anti-pattern.
So I'm not sure if we want to treat the interpolation as a constant.
Please let me know what you think.
P.S.
There is a proposal for constant interpolated strings.
dotnet/csharplang#2951
from resharper-structured-logging.
Interesting, didn't consider what is happening under the hood. Ultimately, I don't think it's a big deal - if this is a potential performance issue then the warning is warranted. The only improvement I can think of would be to split off the warning into a different analyzer (something like "nameof() incurs a performance penalty"), maybe even detecting if it's running on Framework or .NET Core 3.1+ if that's possible, so it only gets displayed if it doesn't get compiled into an actual constant.
from resharper-structured-logging.
Hi @lennartb-,
The only improvement I can think of would be to split off the warning into a different analyzer (something like "nameof() incurs a performance penalty")
nameof
doesn't incur a performance penalty per se. The problem here is an interpolation.
const string a = "String";
Console.WriteLine($"Hello {a}");
IL_0000: ldstr "Hello {0}"
IL_0005: ldstr "String"
IL_000A: call System.String.Format
IL_000F: call System.Console.WriteLine
As you can see using a constant as a placeholder produces the same result.
maybe even detecting if it's running on Framework or .NET Core 3.1+ if that's possible, so it only gets displayed if it doesn't get compiled into an actual constant.
I believe this is possible. But relying on pretty low-level compiler optimization is dangerous.
In my opinion, the safest approach which will work for all .NET versions is to use old-plain concatenation
Log.Information("Hello " + nameof(String));
or Serilog way
Log.Information("Hello {ClassName}", nameof(String));
You'll get a ClassName
property here as a bonus.
from resharper-structured-logging.
Closed due to inactivity, feel free to re-open the issue.
from resharper-structured-logging.
@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
from resharper-structured-logging.
Well, it works if you target C# 10
C# 10
from resharper-structured-logging.
Related Issues (20)
- Incompatibility with ReSharper 2023.3 HOT 9
- Using "Convert to compile-time constant message template" select text one char too much on the left of selection
- Error when using the ReSharper CLI HOT 3
- Support LoggerMessage.Define and DefineScope
- `ToString` in parent classes is not considered HOT 1
- Compatibility with Rider 2022.3 EAP HOT 1
- Support latest Rider 2022.3 EAP 2 HOT 1
- Plugin Installation Error HOT 4
- Support for LoggerMessageAttribute
- Compatibility with Rider 2023.1 EAP HOT 1
- Specified cast is not valid on reading `ConstantValue.StringValue` HOT 5
- Convert to compile time constant codefix should cover more cases HOT 2
- Compatibility with Jetbrains Rider 2023.2 HOT 2
- Log Properties Naming Style not working HOT 2
- Convert to Roslyn Analyzer HOT 5
- Exception in Plugin (2023.2.0.297-eap02) HOT 14
- Support for ZLogger
- Structured Logging 2023.2.0.301-eap09 HOT 2
- Exception in plugin on Rider 2023.2 HOT 1
- Allow disabling analyzer in .editorconfig HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from resharper-structured-logging.