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??