diff --git a/src/agent/NetworkResurrector.Agent/Extensions/DataTypeExtensions.cs b/src/agent/NetworkResurrector.Agent/Extensions/DataTypeExtensions.cs new file mode 100644 index 0000000..20dbb21 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent/Extensions/DataTypeExtensions.cs @@ -0,0 +1,8 @@ +namespace NetworkResurrector.Agent.Extensions +{ + internal static class DataTypeExtensions + { + public static string Nullify(this string value) + => string.IsNullOrWhiteSpace(value) ? null : value; + } +} diff --git a/src/agent/NetworkResurrector.Agent/Extensions/LoggingExtensions.cs b/src/agent/NetworkResurrector.Agent/Extensions/LoggingExtensions.cs new file mode 100644 index 0000000..a1f51fc --- /dev/null +++ b/src/agent/NetworkResurrector.Agent/Extensions/LoggingExtensions.cs @@ -0,0 +1,41 @@ +using Microsoft.Extensions.Configuration; +using Serilog; +using Serilog.Configuration; +using Serilog.Sinks.MSSqlServer; +using System; + +namespace NetworkResurrector.Agent.Extensions +{ + internal static class LoggingExtensions + { + internal static LoggerSinkConfiguration ConfiguredDestinations(this LoggerSinkConfiguration writeTo, IConfiguration configuration) + { + var sqlServerEnabled = configuration.GetValue("Logs:SqlServer:Enabled"); + var sqlServerConnection = configuration.GetValue("Logs:SqlServer:Connection"); + var seqEnabled = configuration.GetValue("Logs:Seq:Enabled"); + var seqUrl = configuration.GetValue("Logs:Seq:Url"); + var seqApiKey = configuration.GetValue("Logs:Seq:ApiKey"); + + if (sqlServerEnabled && string.IsNullOrWhiteSpace(sqlServerConnection)) + throw new Exception("If SqlServer logging is enabled, the SqlServer connection must be configured."); + if (seqEnabled && string.IsNullOrWhiteSpace(seqUrl)) + throw new Exception("If Seq logging is enabled, the Seq URL must be configured."); + + if (sqlServerEnabled) + { + var columnOptions = new ColumnOptions(); + columnOptions.Store.Remove(StandardColumn.Properties); + columnOptions.Store.Remove(StandardColumn.MessageTemplate); + columnOptions.Store.Add(StandardColumn.LogEvent); + var mssqlSinkOptions = new MSSqlServerSinkOptions() { AutoCreateSqlTable = true, TableName = "__Logs_Agent" }; + + writeTo.MSSqlServer(sqlServerConnection, mssqlSinkOptions, columnOptions: columnOptions); + } + + if (seqEnabled) + writeTo.Seq(seqUrl, apiKey: seqApiKey.Nullify()); + + return writeTo; + } + } +} diff --git a/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj b/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj index 28f5391..389adb3 100644 --- a/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj +++ b/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj @@ -15,6 +15,7 @@ + diff --git a/src/agent/NetworkResurrector.Agent/Program.cs b/src/agent/NetworkResurrector.Agent/Program.cs index a071261..4b0f210 100644 --- a/src/agent/NetworkResurrector.Agent/Program.cs +++ b/src/agent/NetworkResurrector.Agent/Program.cs @@ -5,9 +5,6 @@ using Microsoft.Extensions.Hosting; using NetworkResurrector.Agent.Extensions; using NetworkResurrector.Agent.Extensions.Serilog; using Serilog; -using Serilog.Core; -using Serilog.Events; -using Serilog.Sinks.MSSqlServer; using System; namespace NetworkResurrector.Agent @@ -20,28 +17,12 @@ namespace NetworkResurrector.Agent builder.Host.UseSerilog((_, lc) => { - var connectionString = builder.Configuration.GetConnectionString("DatabaseConnection"); - var loggingLevelParam = builder.Configuration.GetValue("Logging:LogLevel:Default"); - - var loggingLevelOk = Enum.TryParse(loggingLevelParam, out LogEventLevel loggingLevel); - if (!loggingLevelOk) - throw new Exception($"Logging level '{loggingLevelParam}' is not valid."); - - var loggingLevelSwitch = new LoggingLevelSwitch(loggingLevel); - - var columnOptions = new ColumnOptions(); - columnOptions.Store.Remove(StandardColumn.Properties); - columnOptions.Store.Remove(StandardColumn.MessageTemplate); - columnOptions.Store.Add(StandardColumn.LogEvent); - var mssqlSinkOptions = new MSSqlServerSinkOptions() { AutoCreateSqlTable = true, TableName = "__Logs_Agent" }; - lc - .MinimumLevel.ControlledBy(loggingLevelSwitch) - .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .ReadFrom.Configuration(builder.Configuration) .Enrich.FromLogContext() .Enrich.With(new AgentCodeEventEnricher(builder.Configuration)) .WriteTo.Console() - .WriteTo.MSSqlServer(connectionString, mssqlSinkOptions, columnOptions: columnOptions); + .WriteTo.ConfiguredDestinations(builder.Configuration); }); builder.Services.ConfigureServices(builder.Configuration); diff --git a/src/agent/NetworkResurrector.Agent/appsettings.json b/src/agent/NetworkResurrector.Agent/appsettings.json index 9016051..daac0b0 100644 --- a/src/agent/NetworkResurrector.Agent/appsettings.json +++ b/src/agent/NetworkResurrector.Agent/appsettings.json @@ -6,11 +6,23 @@ "Host": { "UseWindowsService": true }, - "Logging": { - "LogLevel": { - "Default": "Debug", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Serilog": { + "MinimumLevel": { + "Default": "Information", + "Override": { + "Microsoft": "Information" + } + } + }, + "Logs": { + "SqlServer": { + "Enabled": false, + "Connection": "Server=#########;Database=#########;User Id=#########;Password=#########;MultipleActiveResultSets=true" + }, + "Seq": { + "Enabled": false, + "Url": "", + "ApiKey": "" } }, "AllowedHosts": "*", @@ -18,7 +30,7 @@ "Code": "DEV_AGENT" }, "IdentityServer": { - "BaseAddress": "https://lab.code-rove.com/identity-server-api/" + "BaseAddress": "http://:/api/" }, "Restrictions": { "EnforceActionOwner": true