title: How to properly set up Azure Functions logging, live metrics, and app insights with dependency injection
body: <p>About a month ago, I noticed that some of the monitoring functionality in the old Azure Functions portal interface stopped working. I wrote more details about the issues on the <a href="https://github.com/Azure/azure-functions-host/issues/6060" rel="noreferrer">Azure Functions Host GitHub</a> but my particular questions are as of yet unanswered. </p>
  
  <p>Now it seems the Azure Functions portal interface default to the new "management experience" that looks more similar to the rest of Azure, and with that, it's even more apparent that something is wrong in the way we use logging and tracing. </p>
  
  <p>My question is: <strong>Does anybody have any code samples as to how to set up Azure Function logging, live metrics, and app insights tracing so that it:</strong></p>
  
  <ol>
  <li><strong>Works with dependency injection</strong></li>
  <li><strong>Works with the new "management experience" interface</strong></li>
  </ol>
  
  <p>Currently, in order to see what a particular Azure Function is doing, I have to go to the old Azure interface and study the log stream. The Functions do work, and they spit out information in the log stream, but only in the old interface, and not much else in terms of monitoring seems to work. Using the old interface: </p>
  
  <ul>
  <li>The invocation logs, the one you get when you press the "Monitor" link under "Functions(Read Only) > [function] > Monitor, shows no invocations at all even though the functions are definitely being called according to the logs.</li>
  <li>The Live app metrics link results in the default "Not available: your app is offline or using an older SDK" with some animated demo charts.</li>
  </ul>
  
  <p>These worked fine a month ago. Now, not so much. </p>
  
  <p>Using the new interface:</p>
  
  <ul>
  <li>Monitoring > Log stream shows nothing except the word "Connected!", regardless of verbosity.</li>
  <li>Monitoring > Log Stream > Open in Live Metrics again just yields the default "Not available: your app is offline or using an older SDK".</li>
  </ul>
  
  <p>Going to a specific function in the new interface by using Functions > Functions > [click on a function]: </p>
  
  <ul>
  <li>Developer > Code + Test > Test-button > Run, the Logs window pops up, just says "Connected!" and nothing else, again regardless of verbosity.</li>
  <li>Monitor > Invocations, there are no invocation traces registered here, even though the function is obviously being called according to the old interface log stream.</li>
  <li>Monitor > Logs, again, just says "Connected!", regardless of verbosity.</li>
  </ul>
  
  <p>I don't understand why it suddenly stopped working a month back, and why so many things don't seem to work with the new interface. Our Functions' NuGet packages are all up to date. </p>
  
  <p>In terms of logging, the logger is dependency injected so that we can use it in multiple classes and not just in the default Functions.cs class:</p>
  
  <pre><code>using Microsoft.Extensions.Logging;
  
  public class EventForwarder
  {
      private readonly ILogger&lt;EventForwarder&gt; log;
  </code></pre>
  
  <p>And we log through the use of extension methods, nothing fancy really:  </p>
  
  <pre><code>using Microsoft.Extensions.Logging;
  
  public static class LoggerExtensions
  {
      public static void Info(this ILogger log, string msg) =&gt; log.LogInformation(msg);
  </code></pre>
  
  <p>The app insights tracer is also dependency injected using a workaround suggested <a href="https://github.com/Azure/azure-functions-host/issues/5353#issuecomment-590147499" rel="noreferrer">here</a>, i.e. our  Startup.cs looks lite this: </p>
  
  <pre><code>using Microsoft.ApplicationInsights.Extensibility;
  using Microsoft.Azure.Functions.Extensions.DependencyInjection;
  using Microsoft.Extensions.DependencyInjection;
  
  [assembly: FunctionsStartup(typeof(EventForwarder.Startup))]
  namespace EventForwarder
  {
      public class Startup : FunctionsStartup
      {
          public override void Configure(IFunctionsHostBuilder builder)
          {
              // https://github.com/Azure/azure-functions-host/issues/5353
              builder.Services.AddSingleton(sp =&gt;
              {
                  var key = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                  return string.IsNullOrWhiteSpace(key) ? new TelemetryConfiguration() : new TelemetryConfiguration(key);
              });
  </code></pre>
  
  <p>We're performing traces of Http retries, among other things, like so: </p>
  
  <pre><code>public class HttpRetryPolicyService
  {
      private readonly ILogger&lt;HttpRetryPolicyService&gt; log;
      private readonly TelemetryClient insights;
  
      public HttpRetryPolicyService(ILogger&lt;HttpRetryPolicyService&gt; log,
          TelemetryConfiguration insightsConfig)
      {
          this.log = log;
          insights = new TelemetryClient(insightsConfig);
      }
  ...
  private void LogRetry(DelegateResult&lt;HttpResponseMessage&gt; message, TimeSpan delay, int attempt, Context context)
  {
      if (message.Exception != null)
      {
          log.Warn($"Exception details: {message.Exception}");
          insights.Track(message.Exception);
  </code></pre>
  
  <p>And we're using extension methods to trace, like so:</p>
  
  <pre><code>using Microsoft.ApplicationInsights;
  
  namespace EventForwarder.Static
  {
      public static class TelemetryExtensions
      {
          public static void Track(this TelemetryClient insights, string eventName)
          {
              insights.TrackEvent(eventName);
              insights.Flush();
          }
  </code></pre>
  
  <p>What am I missing?</p>
  
  <p><em>Edit #1: Btw, adding Application Insights as a Service Dependency in the Publish dialog unfortunately does <strong>not</strong> solve these issues.</em> </p>
  
  <p><em>Edit #2: Also, preemtively, our Functions host.json files all look like this:</em></p>
  
  <pre><code>{
      "version": "2.0",
      "healthMonitor": {
          "enabled": true,
          "healthCheckInterval": "00:00:10",
          "healthCheckWindow": "00:02:00",
          "healthCheckThreshold": 6,
          "counterThreshold": 0.80
      },
      "logging": {
          "fileLoggingMode": "always",
          "applicationInsights": {
              "enableLiveMetrics": true,
              "samplingSettings": {
                  "isEnabled": true
              }
          },
          "logLevel": {
              "EventForwarder": "Information"
          }
      }
  }
  </code></pre>