title: Serilog in Azure Functions
body: <p>Each method in Azure Functions can have a <code>Microsoft.Extensions.Logging.ILogger</code> injected into it for logging. Using <code>WebJobsStartup</code> with a startup class you can change the logging to use Serilog using the following syntax:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[assembly: WebJobsStartup(typeof(Startup))]
  namespace MyFuncApp {
      public class Startup : IWebJobsStartup
      {
          public void Configure(IWebJobsBuilder builder)
          {
              builder.Services.AddLogging(
                  lb =&gt; lb.ClearProviders()
                      .AddSerilog(
                          new LoggerConfiguration()
                              .Enrich.FromLogContext()
                              .WriteTo.Console()
                              .WriteTo.File(@"C:\Temp\MyFuncApp.log")
                              .CreateLogger(),
                          true));
          }
      }
  }
  </code></pre>
  
  <p>I can also add other objects to the DI and inject them either into the methods or into the constructor for the class containing the methods using i.e. <code>builder.Services.AddSingleton&lt;IMyInterface, MyImplementation&gt;();</code></p>
  
  <p>However, I would very much like to be able to inject the <code>Microsoft.Extensions.Logging.ILogger</code> in the same way, but if I try to use the <code>ILogger</code> in the constructor I get the following error during method invokation (as that's when the class is created):</p>
  
  <blockquote>
    <p>Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'MyFuncApp.MyFunctions'.</p>
  </blockquote>
  
  <p>So, is there any way of injecting the <code>ILogger</code> into a class constructor like this?</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MyFunctions
  {
      private IMyInterface _myImpl;
      private ILogger _log;
  
      public MyFunctions(
          IMyInterface myImplememtation, // This works
          ILogger log) // This does not
      {
          _myImpl = myImplementation;
          _log = log;
          _log.LogInformation("Class constructed");
      }
  
      public async Task&lt;IActionResult&gt; Function1([HttpTrigger() ... ) {
          _log.LogInformation("Function1 invoked");
      }
  }
  </code></pre>