From b20026290d37a88296afb278037c04bc175f7442 Mon Sep 17 00:00:00 2001 From: leewrigh Date: Fri, 11 Oct 2024 12:10:30 -0700 Subject: [PATCH] Add splunk logging --- backend/JAMService/JAMServiceConfiguration.cs | 8 ++++ backend/JAMService/Program.cs | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/backend/JAMService/JAMServiceConfiguration.cs b/backend/JAMService/JAMServiceConfiguration.cs index b34d83c2..035933ba 100644 --- a/backend/JAMService/JAMServiceConfiguration.cs +++ b/backend/JAMService/JAMServiceConfiguration.cs @@ -9,6 +9,14 @@ public class JAMServiceConfiguration public ConnectionStringConfiguration DatabaseConnectionInfo { get; set; } = new(); public KeycloakAdminConfiguration KeycloakConfiguration { get; set; } = new(); public JustinClientAuthentication JustinApplicationRolesClient { get; set; } = new JustinClientAuthentication(); + public SplunkConfiguration SplunkConfig { get; set; } = new SplunkConfiguration(); + + public class SplunkConfiguration + { + public string Host { get; set; } = string.Empty; + public string CollectorToken { get; set; } = string.Empty; + } + public class KafkaClusterConfiguration { diff --git a/backend/JAMService/Program.cs b/backend/JAMService/Program.cs index a63d8bff..dca5df4a 100644 --- a/backend/JAMService/Program.cs +++ b/backend/JAMService/Program.cs @@ -1,3 +1,4 @@ +using System.Reflection; using JAMService; using JAMService.Data; using JAMService.Infrastructure; @@ -5,6 +6,9 @@ using JAMService.Infrastructure.Kafka; using Microsoft.EntityFrameworkCore; using Prometheus; +using Serilog; +using Serilog.Events; +using Serilog.Sinks.SystemConsole.Themes; var builder = WebApplication.CreateBuilder(args); @@ -40,6 +44,43 @@ builder.Services.AddHealthChecks(); +var name = Assembly.GetExecutingAssembly().GetName(); +var outputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"; + +var loggerConfiguration = new LoggerConfiguration() + .MinimumLevel.Information() + .Filter.ByExcluding("RequestPath like '/health%'") + .Filter.ByExcluding("RequestPath like '/metrics%'") + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information) + .MinimumLevel.Override("System", LogEventLevel.Warning) + .Enrich.FromLogContext() + .Enrich.WithMachineName() + .Enrich.WithProperty("Assembly", $"{name.Name}") + .Enrich.WithProperty("Version", $"{name.Version}") + .WriteTo.Console( + outputTemplate: outputTemplate, + theme: AnsiConsoleTheme.Code); + + +if (!string.IsNullOrEmpty(config.SplunkConfig.Host)) +{ + loggerConfiguration.WriteTo.EventCollector(config.SplunkConfig.Host, config.SplunkConfig.CollectorToken); +} + +Log.Logger = loggerConfiguration.CreateLogger(); + +if (string.IsNullOrEmpty(config.SplunkConfig.Host)) +{ + Log.Warning("*** Splunk Host is not configured - check Splunk environment *** "); +} + +else +{ + Log.Information($"*** Splunk logging to {config.SplunkConfig.Host} ***"); +} + + var app = builder.Build();