0

.netcore logger supports a text templating feature that is different than string interpolation.

For example, the following call:

_logger.LogInformation("About page visited at {DT}",DateTime.UtcNow.ToLongTimeString());

... replaces {DT} with a date.

Is this templating capability a special feature in the .netcore logging or a new C# feature?

If it is only implemented in .netcore logging, can I re-use it in my code string processing operation?

Update 1

enter image description here

Allan Xu
  • 7,998
  • 11
  • 51
  • 122
  • Sorry for my previous answer, which was a bit misleading. I dug a bit more and found that this is only for .net Logging. So the answer to your question is probably no - it's not reusable. Here are some explanations, https://stackoverflow.com/questions/52200484/why-logging-doesnt-use-string-interpolation – Charles Han Nov 14 '22 at 07:03
  • @Rena, I need to go through .netcore logging source code and verify it. Unless it uses an internal class, I expect some reusability in "Microsoft.Extensions.Logging" – Allan Xu Nov 17 '22 at 03:04
  • Hi @AllanXu, what do you mean for `go through .netcore logging source code and verify it`? – Rena Nov 17 '22 at 03:06
  • @Rena I mean I need to analyze the .netcore logging source code to find an accurate answer to the question in the title – Allan Xu Nov 17 '22 at 03:13
  • @Rena, please consider the question in the title "Is the text templating feature in .netcore logging available for use in our code?" . The laser focus a verified answer should be what is asked in the question. – Allan Xu Nov 17 '22 at 03:15
  • @Rena, please consider I am not asking why .netocre is not using C# string interpolation. The ask is how to re-use it. – Allan Xu Nov 17 '22 at 03:16
  • Hi @AllanXu, if you mean you want to apply text template like what logging did to simple string, I may say no. – Rena Nov 17 '22 at 04:22

1 Answers1

1

You cannot use the string with text template like what logging do.

You could use String.Format like below as a alternative way:

string s = string. Format("About page visited at {0}", DateTime.UtcNow.ToLongTimeString());

Reference: get started with the String.Format method

Or you can use in this way:

var time = DateTime.UtcNow.ToLongTimeString();
string s = $"About page visited at {time}";
Rena
  • 30,832
  • 6
  • 37
  • 72
  • 1
    @AllanXu without more detailed explaination why you need the specific way that logging handles this (it needs to know not only the value to be replaced, but the "name" that this placeholder has), this is the correct answer. – Christian.K Nov 15 '22 at 05:11
  • 1
    @Christian.K, I respectfully disagree. String.Format is not the answer. While I have a reason in my application, it shouldn't matter. The question title is quite straightforward. The answer could be "No you cannot" -OR- "Yes, you can use the Nuget Package and call this class method" . The answer above is like "I don't know but you can use something else" I would not consider that as the correct answer as it does not clear the question. – Allan Xu Nov 17 '22 at 02:59
  • @AllanXu Of course you don't _have_ to accept the above as an answer. More detail in your question on your reasons would be nice though, because it helps people finding solutions to your - as it seems - _actual_ problem. Sometimes people ask XY-questions, you know. YMMV of course. – Christian.K Nov 17 '22 at 04:11