Proposal: String Interpolation providing the original string. #1949
Replies: 5 comments
-
#287 might help you? |
Beta Was this translation helpful? Give feedback.
-
I am afraid, it it might not. I undrestand the #287 basically more about getting better results in case of exception is thrown. The motivation of opening this issue was more or less to bring more convenience for that structured logging utilizing the string interpolation, if that string interpolation could be little "enhanced" in some next version of Compiler/ FormattableString/FormattableStringFactory implementation) |
Beta Was this translation helpful? Give feedback.
-
@rychlym I think the point was, consider your void Info(FormattableString formattableString, [CallerArgumentExpression("formattableString")] string argumentExpression); Then with #287, this code: log.Info($"{Greetings.Friendly:asGreetingString} Jack, I have {list.paramAmount} of parameters for you from my list ...."); would compile into: log.Info(
FormattableStringFactory.Create(
"{0:asGreetingString} Jack, I have {1} of parameters for you from my list ....",
Greetings.Friendly, list.paramAmount),
"$\"{Greetings.Friendly:asGreetingString} Jack, I have {list.paramAmount} of parameters for you from my list ....\""); This gives you all the information that you want: you take the |
Beta Was this translation helpful? Give feedback.
-
You might take a look at the StringTokenFormatter library on nuget. Not exactly what you want but close. |
Beta Was this translation helpful? Give feedback.
-
@svick thanks for clarifying it in the detail, now I understand, that #287 solves problem I tried to describe. Still there might be little place for consideration of native support inside "string interpolation" itself (I mean what if the FormattableString (or its supperior??) exposes aditional read-only Property similar to Format) implemented as though - perhaps can someone asses it whether it worths? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
First please excuse me, if already discussed such a topic, I just came at this forum and shorly seached and but haven't found that theme yet.)
I udderstand, that the string interpolation came as a kind of improvement to the String.Format concept.
This propossal I believe, could the move "string interpolation" little further (,actually I see it this time in just one particular area in structural logging as stated in Serilog, but someone else perhaps can have different ideas whith that.... (and yes, I know about the EF Core utilizing the FormattableString, but luckilly it infers the parameter names from the SQL)
As I mentionded I quite like using the Serilog logging library. Hopefully you know, the logging using serilog uses its special format - something like:
log.Info("{GreetingString:l} Jack, I have {ListParamAmout} of parameters for you fom my list ....", Greetings.Friendly, list.paramAmount);
I was thinking about to have ability in consumer's code write it easily with the spirit of the "string interpolation" as well.
log.Info("{Greetings.Friendly:asGreetingString} Jack, I have {list.paramAmount} of parameters for you from my list ....");
I have shortly looked around FormattableStringFactory, FormattableString, IFormattable
and all I have found e.g. FormattableString.Format gives the legacy format string (that with {0}, {1},.. placeholder.)
The proposal for some new version of the C# is, that the FormattablesSring provides also at least the original string or a kind of structure where it would be possible to get the C# (VB.NET) string contents inside of the curly brackets in that original string "literal" preceding with $.
I guess, (haven't looked to IL) the FormattableStringFactory is used by compiler, am I right?
So would it be hard to add that :) ? (I again guess the changes on the compiler side and ammendments in the FormattableStringFactory, FormattableString definitions))
p.s.
I have actually noticed the similar issue proposed by @ashmind (closed as well) during typing, so please let me try to have luck with this ....
Beta Was this translation helpful? Give feedback.
All reactions