2

I have a C# ASP .NET Core Web API application and an Worker Service application, but the logs from API and from Worker Service doesn't arrive in Azure Application Insights, can see them only in Console and are written to files.

Both scenario tested:

  • API deployed in Azure AppService + Worker Service deployed as Windows service in Azure in an Windows Server Virtual Machine
  • API and Worker Service ran in debug mode from local machine in Visual Studio

Versions:

  • .NET 5.0
  • Microsoft.ApplicationInsights.AspNetCore 2.18.0 (installed for the API)
  • Microsoft.ApplicationInsights.WorkerService 2.18.0 (installed for the Worker Service)
  • Serilog 2.10.0
  • Serilog.Sinks.ApplicationInsights 3.1.0 (or 2.6.4)

Tried without success even downgrading Serilog.Sinks.ApplicationInsights to version 2.6.4 as suggested in this issue.

Have both API and Worker service configured the same way: API - Program.cs

var options = CommandLineOptions.Configure(args);
await Host.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration(AppConfigurationServices.Configure)
    .ConfigureLogging(LoggerServices.Configure)
    .ConfigureServices(HostServices.Configure)
    .UseSerilog()
    .RunHostAsync(options);

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));

Worker Service - Program.cs

await Host.CreateDefaultBuilder(args)
      .ConfigureWebHostDefaults(webBuilder =>
      {
          webBuilder
              .ConfigureAppConfiguration(AppConfigurationServices.Configure)
              .ConfigureLogging(LoggingServices.Configure)
              .UseStartup<Startup>();
      })
      .UseSerilog()
      .Build()
      .RunAsync();

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));

I'm already supplying the Azure App Insights InstrumentationKey in appSettings.json two locations:

...
"ApplicationInsights": {
    "InstrumentationKey": "xxx-xyz"
  },
...
"Serilog": {
    "Using": [
      "Serilog",
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File",
      "Serilog.Sinks.ApplicationInsights"
    ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "File",
        "Args": {
          "path": "Logs/Services-.log",
          "rollingInterval": "Day"
        }
      },
      {
        "Name": "ApplicationInsights",
        "Args": {
          "instrumentationKey": "xxx-xyz",
          "restrictedToMinimumLevel": "Information",
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  }
...

When specifying InstrumentationKey only in ApplicationInsights and not under Serilog writer as well, iKey wasn't present and i got: Application Insights Telemetry (unconfigured) {"name":"AppTraces", "time": "123...", ....}

After specifying InstrumentationKey in both places (ApplicationInsights and under Serilog writer) you can see multiple records like bellow in the Debug Output window from Visual Studio. Even enabling Serilog self diagnostic doesn't produce any different output than this one from Debug Output window:

Application Insights Telemetry: {"name":"AppRequests", "time": "123...", "iKey": "xxx-xyz", ....}
Application Insights Telemetry: {"name":"AppMetrics", "time": "123...", "iKey": "xxx-xyz", ....}
Application Insights Telemetry: {"name":"AppTraces", "time": "123...", "iKey": "xxx-xyz", ....}

Opened as well a GitHub issue.

Florin Vîrdol
  • 395
  • 2
  • 10
  • 28
  • 1
    As you have already started a discussion on GitHub, posting the link here for reference to help other community members. You can refer to [Serilog logs aren't sent / written into Azure ApplicationInsights for .NET Core API and Worker Service](https://github.com/serilog/serilog-sinks-applicationinsights/issues/176) – Ecstasy Oct 07 '21 at 04:25
  • 1
    Thank you, @DeepDave-MT! I thought I added the GitHub link, but somehow forgot. – Florin Vîrdol Oct 07 '21 at 09:30
  • 1
    Could you pls take a look at my [this answer](https://stackoverflow.com/a/67834649/15581227)? – Tiny Wang Oct 11 '21 at 08:23

0 Answers0