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.

Add support for FormattableString

See original GitHub issue

I think it would be a nice addition to the arsenal of ZeroLog to support the somewhat unknown c# interpolated string feature called ForamttableString.

This would basically allow users to call ZeroLog with interpolated strings:

log.InfoInterpolated($"Tomorrow ({tomorrow}) will occur in {numberOfSecondsUntilTomorrow} seconds");

The theory behind this is that ZeroLog would get a FormattableString instance fed into the *Interpolated variants of the logging functions that would have to be added to ZeroLog and to the StringFormatter project.

The reason behind adding yet another overload for this purpose is that apprently the c# compiler has a feature/issue where having two functions that only differ by having a string/FormattableString parameter leads to the string variant being always selected by the compiler:

https://github.com/dotnet/roslyn/issues/46

http://blog.engdahls.dk/2016/08/how-to-overload-string-and.html

Any thoughts about this?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
ltrzesniewskicommented, Jun 18, 2018

Yes, it allocates the FormattableString, plus an object[] array to hold the values, and it needs to box the values when they’re value types.

See here:

    public void M()
    {
        Log($"Hello {42}, world! {true}");
    }
    
    public static void Log(FormattableString value)
    {
    }

Gets compiled to:

    public void M()
    {
        object[] obj = new object[2];
        obj[0] = 42;
        obj[1] = true;
        Log(FormattableStringFactory.Create("Hello {0}, world! {1}", obj));
    }

    public static void Log(FormattableString value)
    {
    }
0reactions
ltrzesniewskicommented, Apr 5, 2022

Ok, it’s maybe a tiny bit late, but interpolated string support is now available in v2, and does not allocate thanks to interpolated string handlers in C# 10. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# FormattableString concatenation for multiline interpolation
1 Answer 1 · Per the documentation: A FormattableString instance may result from an interpolated string in C# or Visual Basic. · Yes,...
Read more >
FormattableString.Invariant(FormattableString) Method
Returns a result string in which arguments are formatted by using the conventions of the invariant culture.
Read more >
Consider adding AppendFormattable to StringBuilder
I see a lot of code like this: new StringBuilder().Append$"Now = {DateTime.Now}"); (Well, not exactly like this, but you get my point) The ......
Read more >
FormattableString as Method Parameter - Damir's Corner
To resolve it, we need to add another implicit conversion to the RawString type, this time from the FormattableString type:
Read more >
FormattableString - somewhat abstract
If you need a culture-invariant string, there is a handy static method in the new `System.FormattableString` class called `Invariant()`. If you ...
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