title: How to setup Serilog with Azure Functions v4 correctly?
body: <p>I want to use Serilog in an Azure Function v4 (.net 6) (the logs should be sent to Datadog). For this I have installed the following nuget packages:</p>
  <pre><code>&lt;PackageReference Include=&quot;Serilog&quot; Version=&quot;2.10.0&quot; /&gt;
  &lt;PackageReference Include=&quot;Serilog.Extensions.Logging&quot; Version=&quot;3.1.0&quot; /&gt;
  &lt;PackageReference Include=&quot;Serilog.Formatting.Compact&quot; Version=&quot;1.1.0&quot; /&gt;
  &lt;PackageReference Include=&quot;Serilog.Sinks.Console&quot; Version=&quot;4.0.1&quot; /&gt;
  &lt;PackageReference Include=&quot;Serilog.Sinks.Datadog.Logs&quot; Version=&quot;0.3.5&quot; /&gt;
  </code></pre>
  <p>Below is the configuration in the Startup.cs class:</p>
  <pre><code>public override void Configure(IFunctionsHostBuilder builder)
  {
    builder.Services.AddHttpClient();
    
    //... adding services etc.
  
    Log.Logger = new LoggerConfiguration()
      .MinimumLevel.Override(&quot;Microsoft&quot;, LogEventLevel.Warning)
      .MinimumLevel.Override(&quot;Worker&quot;, LogEventLevel.Warning)
      .MinimumLevel.Override(&quot;Host&quot;, LogEventLevel.Warning)
      .MinimumLevel.Override(&quot;System&quot;, LogEventLevel.Error)
      .MinimumLevel.Override(&quot;Function&quot;, LogEventLevel.Error)
      .MinimumLevel.Override(&quot;Azure.Storage.Blobs&quot;, LogEventLevel.Error)
      .MinimumLevel.Override(&quot;Azure.Core&quot;, LogEventLevel.Error)
      .Enrich.WithProperty(&quot;Application&quot;, &quot;Comatic.KrediScan.AzureFunctions&quot;)
      .Enrich.FromLogContext()
      .WriteTo.DatadogLogs(&quot;XXXXXXXXXXX&quot;, configuration: new DatadogConfiguration() { Url = &quot;https://http-intake.logs.datadoghq.eu&quot; }, logLevel:   LogEventLevel.Debug)
      .WriteTo.Console()
      .CreateLogger();
  
    builder.Services.AddSingleton&lt;ILoggerProvider&gt;(sp =&gt; new SerilogLoggerProvider(Log.Logger, true));
  
    builder.Services.AddLogging(lb =&gt;
    {
      //lb.ClearProviders(); //--&gt; if used nothing works...
      lb.AddSerilog(Log.Logger, true);
    });
  </code></pre>
  <p>Basically logging works, but all log statements are written twice (with a few milliseconds difference, Datadog and Console).</p>
  <p><a href="https://i.stack.imgur.com/wPssT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/wPssT.png" alt="enter image description here" /></a></p>
  <p>Obviously I am doing something fundamentally wrong with the configuration. I don't use appsettings.json, the configuration of Serilog takes place exclusively in the code. I have scoured the entire internet and read just about every article on Serilog and Azure Functions. On Stackoverflow I also read virtually every question about it and tried all the answers. Unfortunately, so far without success.</p>
  <p>SO-Questions for example:
  <a href="https://stackoverflow.com/questions/49744852/use-serilog-with-azure-log-stream">Use Serilog with Azure Log Stream</a><br />
  <a href="https://stackoverflow.com/questions/67321356/how-do-i-use-serilog-with-azure-webjobs">How do I use Serilog with Azure WebJobs?</a><br />
  <a href="https://stackoverflow.com/questions/64025879/serilog-enricher-dependency-injection-with-azure-functions">Serilog enricher Dependency Injection with Azure Functions</a><br />
  <a href="https://github.com/hgmauri/sample-azure-functions/blob/main/src/Sample.AzureFunctions.DotNet31/Startup.cs" rel="noreferrer">https://github.com/hgmauri/sample-azure-functions/blob/main/src/Sample.AzureFunctions.DotNet31/Startup.cs</a></p>
  <p>Is there any example for setting up Serilog with Azure Functions v4 / .net 6?</p>
  <p>Thanks a lot for the help!<br />
  Michael Hachen</p>