1

I'm trying to configure Serilog to work with a .NET 6 Razor page web application. I've tried following the aspnetcore directions here and applicationinsights here. Nothing I try seems to work. I see my enricher being hit but nothing is written to application insights.

The program.cs files looks like this:

using AppInsightsSerilogCustomDimensions;
using Microsoft.ApplicationInsights.Extensibility;
using Serilog;

Log.Logger = new LoggerConfiguration()
    .Enrich.With<SystemInfoEnricher>()
    .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces)
    .CreateBootstrapLogger();

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Configuration(context.Configuration)
    .ReadFrom.Services(services)
    .Enrich.With<SystemInfoEnricher>()
    .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces));

// Error: No service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' has been registered.
//builder.Host.UseSerilog((context, services, configuration) => configuration
//    .ReadFrom.Configuration(context.Configuration)
//    .ReadFrom.Services(services)
//    .Enrich.With<SystemInfoEnricher>()
//    .WriteTo.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces));


var app = builder.Build();


// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

I've configured my appsettings.json file according to the Microsoft docs here, but threw in an InstrumentationKey as well just in case that would help.

{
  "ApplicationInsights": {
    "InstrumentationKey": "--I put the key here--",
    "ConnectionString": "---I put a connection string here---"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Information"
    }
  },
  "AllowedHosts": "*"
}

I've been banging my head against this for 3 hours now and it just doesn't work. I've been able to configure application insight WITHOUT Serilog in the application and it logged to the Application Insights resource, but I just can't get it to work WITH Serilog.

I suspect it has something to do with the instrumentation key not being read since I see error No service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' when I uncomment the code in program.cs.

Any help would be appreciated.

David Yates
  • 1,935
  • 2
  • 22
  • 38
  • See following : https://stackoverflow.com/questions/42126800/app-wont-start-after-removing-application-insights – jdweng Aug 05 '22 at 02:29
  • @jdweng My issue isn't that Application Insights doesn't work by itself or that I can't run the web site. Both work. The problem is trying to get Serilog wired up so that it works with Application Insights. Also, this issue is also caused by the different way that .NET Core 6 sites startup now as opposed to a couple of years ago. In other words, this is a .NET 6.0 issue with Serilog configuration. – David Yates Aug 05 '22 at 15:22
  • Did you "Look for lines referencing Application Insights". It may be older version of Net didn't care about the lines while Net Core needs them removed. – jdweng Aug 06 '22 at 08:02

1 Answers1

0
  • Your Instrumentation key is not configured properly.

Refer the below workaround

  • Make sure you have below Nuget packages installed are updated
Serilog.Settings.Configuration
Serilog.AspNetCore
Serilog.Sinks.ApplicationInsights
Microsoft.ApplicationInsights.AspNetCore

Program.cs

  public static void Main(string[] args)
    {      
        CreateHostBuilder(args).Build().Run();       
    }
  public static IHostBuilder CreateHostBuilder(string[] args) =>
         Host.CreateDefaultBuilder(args)                   
           .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                    .ReadFrom.Configuration(hostingContext.Configuration)
                  .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces)
             );   

Output : enter image description here

References taken from :

Serilog.AspNetCore

Harshitha
  • 3,784
  • 2
  • 4
  • 9