简体   繁体   中英

Cannot log into application insights from Azure IoT Edge module

I've a sample AzureIotEdgeApp with one IotEdgeModule (C#). In that one, I've a single file (program.cs) and I'm writing some logging information to application insights. But it never writes. I also have a launchSettings.json where I've an environment variable APPINSIGHTS_INSTRUMENTATIONKEY.

I've the same code in a net core sample console app (2.2 and 3.1) and it just works fine.

What am I missing?

static void Main(string[] args)
{
  var serviceCollection = new ServiceCollection();
  ConfigureServices(serviceCollection);

  var serviceProvider = serviceCollection.BuildServiceProvider();
  var logger = serviceProvider.GetService<ILogger<Program>>();

  for (var i=1; i<=2; i++)
  {
     logger.LogInformation("Testing an information message");
     logger.LogError("Testing an error message");
  }

  Console.ReadKey();
}

private static void ConfigureServices(IServiceCollection services)
{
  Log.Logger = new LoggerConfiguration() // Serilog
         .Enrich.FromLogContext()
         .WriteTo.Console()
         .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces)
         .CreateLogger();

  services.AddLogging(configure => configure.AddSerilog(Log.Logger)).AddScoped<TelemetryClient>();
}

I've the following packages added.

Microsoft.ApplicationInsights - 2.12.0 Microsoft.Extensions.DependencyInjection - 3.1.0 Serilog - 2.0 Serilog.Extensions.Logging - 3.0.1 Serilog.Sinks.ApplicationInsights - 3.0.4

应用程序洞察键应该添加到 deployment.json 文件中,而不是像普通的 net core 控制台应用程序那样添加到 launchSettings.json 中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM