title: How to log lazily with ASP.NET Core logging?
body: <p>Suppose a scenario like this:</p>
  
  <pre><code>[Route("api/test")]
  public class TestController
  {
      private readonly ILogger&lt;TestController&gt; logger;
  
      public TestController(ILogger&lt;TestController&gt; logger)
      {
          this.logger = logger;
      }
  
      [HttpPut]
      public void Put(Guid id, [FromBody]FooModel model)
      {
          logger.LogInformation($"Putting {id}");
          logger.LogTrace("Putting model {0}", Newtonsoft.Json.JsonConvert.SerializeObject(model));
          try
          {
              // Omitted: actual PUT operation.
          }
          catch (Exception ex)
          {
              logger.LogError("Exception {0}", ex);
          }
      }
  }
  
  public class FooModel 
  { 
      string Bar { get; set; } 
  }
  </code></pre>
  
  <p>In this scenario, the <code>LogInformation</code> call will trigger a <code>string.Format</code> call, and even worse, the <code>LogTrace</code> line will trigger <strong>a <code>SerializeObject</code> call, regardless of the <code>LogLevel</code></strong>. That seems rather wasteful.</p>
  
  <p>Is there a place in the Logging API that allows for a more lazy approach? The only workaround I can think of is overriding <code>ToString</code> on model to create a very verbose representation, and skip on using <code>JsonConvert.SerializeObject</code> as a tool.</p>