GithubHelp home page GithubHelp logo

Comments (6)

olsh avatar olsh commented on May 26, 2024 2

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.

lennartb- avatar lennartb- commented on May 26, 2024

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.

olsh avatar olsh commented on May 26, 2024

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.

olsh avatar olsh commented on May 26, 2024

Closed due to inactivity, feel free to re-open the issue.

from resharper-structured-logging.

markusschaber avatar markusschaber commented on May 26, 2024

@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.

olsh avatar olsh commented on May 26, 2024

Well, it works if you target C# 10
C# 10
image

C# 9
image

from resharper-structured-logging.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.