Skip to content

Commit

Permalink
Add splunk logging
Browse files Browse the repository at this point in the history
  • Loading branch information
leewrigh committed Oct 11, 2024
1 parent 3e77116 commit b200262
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions backend/JAMService/JAMServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
41 changes: 41 additions & 0 deletions backend/JAMService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Reflection;
using JAMService;
using JAMService.Data;
using JAMService.Infrastructure;
using JAMService.Infrastructure.Clients.KeycloakAdminClient;
using JAMService.Infrastructure.Kafka;
using Microsoft.EntityFrameworkCore;
using Prometheus;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit b200262

Please sign in to comment.