Alex Gordon
Alex Gordon

Reputation: 60841

logging not showing up in my app insights instance

I have a .NET Core 3.1 project in Visual Studio that previously only used Serilog for logging. I am trying to add logging to an Azure Application Insights instance.Please note that although this is deployed to azure as a webjob, I'm actually running it locally in this instance. Below are the relevant parts of my project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\ccc.BusinessManager\ccc.BusinessManager.csproj" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
  </ItemGroup>
</Project>

heres the app.config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ApplicationInsights:ConnectionString" value="InstrumentationKey=xxxxxxxxxx-95bxxxx15;IngestionEndpoint=https://centralus-3.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/;ApplicationId=xxxxxxxxb92db790dfb" />
  </appSettings>
</configuration>

here is the program.cs

// Truncated for brevity, here's the relevant part for Application Insights setup:
private static void RegisterServices()
{
    var services = new ServiceCollection();

    // Read Application Insights Connection String from config
    string appInsightsConnectionString = ConfigurationManager.AppSettings["ApplicationInsights:ConnectionString"];

    // Configure Application Insights
    services.AddApplicationInsightsTelemetryWorkerService(options =>
    {
        options.ConnectionString = appInsightsConnectionString;
    });

    var loggerService = services
        .AddLogging(cfg =>
        {
            cfg.AddConsole();
            cfg.AddApplicationInsights();
        })
        .Configure<LoggerFilterOptions>(cfg => cfg.MinLevel = LogLevel.Debug)
        .BuildServiceProvider();

    _serviceProvider = services.BuildServiceProvider(true);
}

I am not seeing logs in my Application Insights instance.

How do I get my logging to flow into app insights?

Upvotes: 0

Views: 78

Answers (2)

Ruikai Feng
Ruikai Feng

Reputation: 11896

You called BuildServiceProvider(); twice,assigned the provider with no logging configuration(services.BuildServiceProvider(true);) to _serviceProvider,try modify

var loggerService = services
        .AddLogging(cfg =>
        {
            cfg.AddConsole();
            cfg.AddApplicationInsights();
        })
        .Configure<LoggerFilterOptions>(cfg => cfg.MinLevel = LogLevel.Debug)
        .BuildServiceProvider();

    _serviceProvider = services.BuildServiceProvider(true);

to

_serviceProvider=services
            .AddLogging(cfg =>
            {
                cfg.AddConsole();
                cfg.AddApplicationInsights();
            })
            .Configure<LoggerFilterOptions>(cfg => cfg.MinLevel = LogLevel.Debug)
            .BuildServiceProvider();

Upvotes: 1

RithwikBojja
RithwikBojja

Reputation: 11383

ogging not showing up in my app insights instance and How do I get my logging to flow into app insights?

To log logs to App insights from program.cs in .net core 3.1, use below code:

csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
    <PackageReference Include="Serilog" Version="4.2.0" />
    <PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
  </ItemGroup>

</Project>

program.cs:

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Hosting;
    using Serilog.Sinks.ApplicationInsights.TelemetryConverters;
    using Serilog;

and

 CreateHostBuilder(args).Build().Run();
 var test_ri_app = "RithwikApp";
 var app_insights_cntst = "InstrumentationKey=10c3echotuab4-a821-app69c;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=f0864rithwikcdda-appf95c";
    
Log.Logger = new LoggerConfiguration()
                    .Enrich.FromLogContext()
                    .Enrich.WithProperty("ApplicationName", test_ri_app)
                    .WriteTo.ApplicationInsights(app_insights_cntst, new TraceTelemetryConverter())
                    .CreateLogger();
 Log.Information("Hello Rithwik, Logging from .net core 3.1");

Output:

enter image description here

Upvotes: 0

Related Questions