Serilog sink to Azure Event Hub

I’m trying to add an additional serilog sink to Azure Event Hub. I’ve got it working using appsettings:

"Serilog": {
    "Using": [
      "Serilog.Sinks.AzureEventHub"
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "AzureEventHub",
        "Args": {
          "connectionString": "xxxxxxx",
          "eventHubName": "diagnostic-logs",
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
        }
      }
    ],
    "Properties": {
      "Application": "Portal",
      "Team": "xxxxxx",
      "Project": "xxxxxx"
    }
  }

but cannot get it working when setting it up through code.

public class Program
    {
        public static void Main(string[] args)
            => CreateHostBuilder(args)
                .Build()
                .Run();

        private static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureUmbracoDefaults()
                .ConfigureAppConfiguration()
                .UseSerilog((context, services, loggerConfiguration) =>
                {
                     var hostingEnv = services.GetRequiredService<IHostEnvironment>();
                     var loggingConfiguration = services.GetRequiredService<ILoggingConfiguration>();
                     var umbracoFileConfiguration = services.GetRequiredService<UmbracoFileConfiguration>();
                
                     loggerConfiguration = new LoggerConfiguration()
                         .MinimalConfiguration(hostingEnv, loggingConfiguration, umbracoFileConfiguration)
                         .ReadFrom.Configuration(context.Configuration)
                         .ReadFrom.Services(services)
                         .Enrich.FromLogContext()
                         .Enrich.With(services.GetService<TelemetryLogEventEnricher>())
                         .Enrich.WithProperty(Constants.Logging.EnvironmentName, context.HostingEnvironment.EnvironmentName)
                         .Enrich.WithProperty("TESTING", "TESTING")
                         .WriteTo.Console(theme: AnsiConsoleTheme.Code)
                         .WriteTo.AzureEventHub(
                             formatter: new JsonFormatter(),
                             connectionString: context.Configuration["AppSettings:AzureEventHub-ConnectionString"],
                             eventHubName: context.Configuration["AppSettings:AzureEventHub-Name"]
                         );
                })

                .UseSerilog((context, services, loggerConfiguration) =>
                {
                     var hostingEnv = services.GetRequiredService<IHostEnvironment>();
                     var loggingConfiguration = services.GetRequiredService<ILoggingConfiguration>();
                     var umbracoFileConfiguration = services.GetRequiredService<UmbracoFileConfiguration>();
                
                     loggerConfiguration
                         .Enrich.WithProperty("TESTING", "TESTING")
                         .WriteTo.Console(theme: AnsiConsoleTheme.Code)
                         .WriteTo.AzureEventHub(
                             formatter: new JsonFormatter(),
                             connectionString: context.Configuration["AppSettings:AzureEventHub-ConnectionString"],
                             eventHubName: context.Configuration["AppSettings:AzureEventHub-Name"]
                         );
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStaticWebAssets();
                    webBuilder.UseStartup<Startup>();
                });
        }
    }

I’ve added both versions of the UseSerilog method I’ve tried. But neither of them enrich the log with the custom property, nor send anything to Event Hub??

Could it be that umbraco core is already adding serilog, and so the appsettings approach is being read by that..
But in your coded example you are somehow clashing with the core serilog that’s already added..
There is a test case in the repo..
Umbraco-CMS/tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTestBase.cs at de64c537770649247a8b70c0c1c0dfcc3ce7e290 · umbraco/Umbraco-CMS

perhaps that’s of use?