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

7
  • 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, stackoverflow.com/questions/52200484/… Commented Nov 14, 2022 at 7: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" Commented Nov 17, 2022 at 3:04
  • Hi @AllanXu, what do you mean for go through .netcore logging source code and verify it? Commented Nov 17, 2022 at 3:06
  • @Rena I mean I need to analyze the .netcore logging source code to find an accurate answer to the question in the title Commented Nov 17, 2022 at 3: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. Commented Nov 17, 2022 at 3:15

1 Answer 1

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}";
Sign up to request clarification or add additional context in comments.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.