15

I have an Azure Function that uses Serilog to write to AppInsights with Serilog AppInsights sink v3.1.

The code in Startup.cs looks like this

Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(config)
            .CreateLogger();

and appsettings.json

"Serilog": {
    "Using": [
      "Serilog.Sinks.ApplicationInsights"
    ],
    "WriteTo": [
      {
        "Name": "ApplicationInsights",
        "Args": {
          "instrumentationKey": "...",
          "restrictedToMinimumLevel": "Verbose",
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
      }
    }
  ],
  ...

It can happily write application logs into AppInsights.

The latest Github documentation mentioned the deprecation of telemetry configuration active and future removal support of Instrumentation Key therefore I would like to upgrade the library to version 4.0.

However, when I upgrade to the sink to v4.0, I get this exception:

enter image description here

Please help.

Alex Monkey
  • 1,427
  • 1
  • 14
  • 16
  • What is the dotnet version you're using in the project? –  Jun 23 '22 at 01:06
  • Net 6.0, Azure function version v4 – Alex Monkey Jun 23 '22 at 01:49
  • Could you try with this code and see if it resolves the issue: Log.Logger = new LoggerConfiguration() .WriteTo.ApplicationInsights( serviceProvider.GetRequiredService(), TelemetryConverter.Traces) .CreateLogger(); –  Jun 23 '22 at 03:32
  • Now it's showing a different exception. ------------ System.InvalidOperationException: 'No service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' has been registered.' ------------ Besides, is there a declarative way rather than using .WriteTo() method. – Alex Monkey Jun 23 '22 at 05:49

1 Answers1

37

So I just spent a good chunk of time debugging this, because I also ran into the issue. I'll admit I should have look at the git log for the app insights sink, because the offending commit is right here

https://github.com/serilog-contrib/serilog-sinks-applicationinsights/commit/8e4e26a8fdfa12da6ed15afcc94889e5f399ff97#diff-bc9f0e00aaa0aef88484faa764a62f13d26f07bf6c2b6df21cd8d893aa47c2e0

They adjusted their namespaces, which causes the Serilog Configuration binding not to be able to find the type Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter anymore. Instead the namespace is Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter as shown in the commit I linked.

Hope this helps

  • 3
    Thank you so much @DarrienSingleton, the shortened namespace really helps. In addition, I had to change the **Args** sections with connection string instead of instrumentationKey. `"Args": { "connectionString": "...", "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights" }` – Alex Monkey Jul 03 '22 at 05:29
  • 2
    Would have been nice if they mentioned that in the breaking changes. – DigiBanks99 Aug 16 '22 at 08:02