1

I have the following line of code in which I'm trying to use string interpolation, but I'm having an issue with my variable {time}:

logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

After string interpolation (see below), I get this error: Time does not exist in the current context.

logger.LogInformation($"Worker running at: {time}", DateTimeOffset.Now);
LHM
  • 721
  • 12
  • 31
JulieRice
  • 11
  • 2
  • 7
    Change `logger.LogInformation($"Worker running at: {time}", DateTimeOffset.Now);` to `logger.LogInformation($"Worker running at: {DateTimeOffset.Now}");` – Izzy Jan 05 '21 at 15:42
  • 5
    How about just `logger.LogInformation("Worker running at: {0}", DateTimeOffset.Now);`? You don't _always_ need to use string interpolation. – D Stanley Jan 05 '21 at 15:43
  • 2
    You're calling [LoggerExtensions.LogInformation](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loggerextensions.loginformation?view=dotnet-plat-ext-5.0). This follows the principles of [semantic logging](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0#log-message-template), where you're expected to provide a log message template containing placeholders `"{like} {this}"`, which you then provide the arguments for... – canton7 Jan 05 '21 at 15:45
  • 1
    ... This means that the logging infrastructure has a link between your "time" variable and the value of `DateTimeOffset.Now` which it can use when recording to a database or other logging sink, and allows you to easily search your log entries based on the values of particular placholders, rather than doing basic text searching – canton7 Jan 05 '21 at 15:46
  • 1
    @canton is right, for more information see their links in comments and the linked duplicate on top of your question. – CodeCaster Jan 05 '21 at 15:49
  • @Olivier then try reading it and its answers and convince me. – CodeCaster Jan 05 '21 at 15:50
  • @CodeCaster Trying to understand what I don't understand even reading the duplicate, one idea comes to me: is `{time}` a valid argument to string formatting like `{0}` ?????????? –  Jan 05 '21 at 15:59
  • This string isn't being passed to `string.Format` -- it's being passed to Microsoft's structured logging infrastructure. `string.Format` doesn't know how to deal with `{time}`, but the logging infrastructure does. In practice, placeholders match up with arguments left-to-right, rather than an index being provided in the placeholder, but see the Microsoft documentation for more details – canton7 Jan 05 '21 at 16:01
  • 1
    @Olivier it is, if the method that accepts those parameters (in this case `LogInformation (this ILogger logger, string message, params object[] args)`) does not accept a "format string" as we know since the birth of .NET ("foo {0} bar"), but, as canton illustrates, a so-called "message template format". The logging framework extracts the `{name}` parts, and fills them in order by the `object[] args`, but not only that, it saves the name with the replacement. So the log will be something like `"Worker running at: [time:2021-01-05 17:01]"`, where the user can query on `time` and whatnot. – CodeCaster Jan 05 '21 at 16:01
  • 1
    @CodeCaster I don't understand at all. But... First I can't find information about `string.Format` acceping `{time}` as a replacement argument... Second: if it is true, it is **une satanerie** ! –  Jan 05 '21 at 16:04
  • 1
    @Olivier I was surprised by the format the first time I encountered it as well, it goes against everything we knew up till then, but you'll get used to it. Again, it's **not** `String.Format()` that eats such strings, it's only methods that know "message templates" that allow this. `Console.WriteLine()` and `String.Format()` do not accept message templates, we're "stuck" with either format strings (`("foo {0} bar", DateTime.Now)`) or string interpolation (`$"foo {DateTime.Now} bar"`) there. (https://www.youtube.com/watch?v=vhC3tG-ids0) – CodeCaster Jan 05 '21 at 16:06
  • 1
    @OlivierRogier You sound like you would benefit from reading about [structured logging](https://nblumhardt.com/2016/06/structured-logging-concepts-in-net-series-1/). Particularly the "Message templates and properties – structured logging concepts in .NET" article. That's focused around a library called Serilog, but the concepts will be similar to Microsoft's logging framework. – mason Jan 05 '21 at 16:33

0 Answers0