Skip to content

Commit

Permalink
Align more logging settings
Browse files Browse the repository at this point in the history
  • Loading branch information
bording committed Mar 20, 2024
1 parent 5831ad6 commit 4a08825
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 154 deletions.
67 changes: 21 additions & 46 deletions src/ServiceControl.Audit/Infrastructure/Settings/LoggingSettings.cs
Expand Up @@ -2,26 +2,22 @@ namespace ServiceControl.Audit.Infrastructure.Settings
{
using System;
using System.IO;
using System.Reflection;
using Configuration;
using NLog;
using NLog.Common;

public class LoggingSettings
public class LoggingSettings(LogLevel defaultLevel = null, string logPath = null)
{
public LoggingSettings(LogLevel defaultLevel = null, string logPath = null)
{
LoggingLevel = InitializeLevel("LogLevel", defaultLevel ?? LogLevel.Info);
LogPath = SettingsReader.Read(Settings.SettingsRootNamespace, "LogPath", Environment.ExpandEnvironmentVariables(logPath ?? DefaultLogLocation()));
}

public LogLevel LoggingLevel { get; }
public LogLevel LoggingLevel { get; } = InitializeLogLevel(defaultLevel);

public string LogPath { get; }
public string LogPath { get; } = SettingsReader.Read(Settings.SettingsRootNamespace, "LogPath", Environment.ExpandEnvironmentVariables(logPath ?? DefaultLogLocation()));

LogLevel InitializeLevel(string key, LogLevel defaultLevel)
static LogLevel InitializeLogLevel(LogLevel defaultLevel)
{
var levelText = SettingsReader.Read<string>(Settings.SettingsRootNamespace, key);
defaultLevel ??= LogLevel.Info;

var levelText = SettingsReader.Read<string>(Settings.SettingsRootNamespace, logLevelKey);

if (string.IsNullOrWhiteSpace(levelText))
{
return defaultLevel;
Expand All @@ -33,47 +29,26 @@ LogLevel InitializeLevel(string key, LogLevel defaultLevel)
}
catch
{
InternalLogger.Warn($"Failed to parse {key} setting. Defaulting to {defaultLevel.Name}.");
InternalLogger.Warn($"Failed to parse {logLevelKey} setting. Defaulting to {defaultLevel.Name}.");
return defaultLevel;
}
}

// SC installer always populates LogPath in app.config on installation/change/upgrade so this will only be used when
// debugging or if the entry is removed manually. In those circumstances default to the folder containing the exe
static string DefaultLogLocation()
{
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
return Path.Combine(Path.GetDirectoryName(assemblyLocation), ".logs");
}
static string DefaultLogLocation() => Path.Combine(AppContext.BaseDirectory, ".logs");

public Microsoft.Extensions.Logging.LogLevel ToHostLogLevel()
public Microsoft.Extensions.Logging.LogLevel ToHostLogLevel() => LoggingLevel switch
{
if (LoggingLevel == LogLevel.Debug)
{
return Microsoft.Extensions.Logging.LogLevel.Debug;
}
if (LoggingLevel == LogLevel.Error)
{
return Microsoft.Extensions.Logging.LogLevel.Error;
}
if (LoggingLevel == LogLevel.Fatal)
{
return Microsoft.Extensions.Logging.LogLevel.Critical;
}
if (LoggingLevel == LogLevel.Warn)
{
return Microsoft.Extensions.Logging.LogLevel.Warning;
}
if (LoggingLevel == LogLevel.Info)
{
return Microsoft.Extensions.Logging.LogLevel.Information;
}
if (LoggingLevel == LogLevel.Trace)
{
return Microsoft.Extensions.Logging.LogLevel.Trace;
}

return Microsoft.Extensions.Logging.LogLevel.None;
}
_ when LoggingLevel == LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
_ when LoggingLevel == LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
_ when LoggingLevel == LogLevel.Info => Microsoft.Extensions.Logging.LogLevel.Information,
_ when LoggingLevel == LogLevel.Warn => Microsoft.Extensions.Logging.LogLevel.Warning,
_ when LoggingLevel == LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
_ when LoggingLevel == LogLevel.Fatal => Microsoft.Extensions.Logging.LogLevel.Critical,
_ => Microsoft.Extensions.Logging.LogLevel.None
};

const string logLevelKey = "LogLevel";
}
}
Expand Up @@ -33,9 +33,8 @@ public static class HostApplicationBuilderExtensions
hostBuilder.Services.AddWindowsService();

hostBuilder.Logging.ClearProviders();
//HINT: configuration used by NLog comes from MonitorLog.cs
hostBuilder.Logging.AddNLog();
hostBuilder.Logging.SetMinimumLevel(ToHostLogLevel(settings.LogLevel));
hostBuilder.Logging.SetMinimumLevel(settings.ToHostLogLevel());

var services = hostBuilder.Services;
services.AddSingleton(settings);
Expand Down Expand Up @@ -133,39 +132,4 @@ static EndpointInputQueue ToQueueId(EndpointToQueueMapping endpointInputQueueDto

static RawMessage.Entry ToEntry(QueueLengthEntry entryDto) =>
new RawMessage.Entry { DateTicks = entryDto.DateTicks, Value = entryDto.Value };

public static LogLevel ToHostLogLevel(NLog.LogLevel logLevel)
{
if (logLevel == NLog.LogLevel.Debug)
{
return LogLevel.Debug;
}

if (logLevel == NLog.LogLevel.Error)
{
return LogLevel.Error;
}

if (logLevel == NLog.LogLevel.Fatal)
{
return LogLevel.Critical;
}

if (logLevel == NLog.LogLevel.Warn)
{
return LogLevel.Warning;
}

if (logLevel == NLog.LogLevel.Info)
{
return LogLevel.Information;
}

if (logLevel == NLog.LogLevel.Trace)
{
return LogLevel.Trace;
}

return LogLevel.None;
}
}
Expand Up @@ -2,9 +2,7 @@
{
using System;
using System.IO;
using Configuration;
using NLog;
using NLog.Common;
using NLog.Config;
using NLog.Extensions.Logging;
using NLog.Layouts;
Expand Down Expand Up @@ -62,23 +60,6 @@ public static void Configure(Settings settings)
logger.InfoFormat("Logging to {0} with LogLevel '{1}'", fileTarget.FileName.Render(logEventInfo), settings.LogLevel.Name);
}

public static LogLevel InitializeLevel()
{
var level = LogLevel.Info;
try
{
level = LogLevel.FromString(SettingsReader.Read(Settings.SettingsRootNamespace, LogLevelKey, LogLevel.Info.Name));
}
catch
{
InternalLogger.Warn($"Failed to parse {LogLevelKey} setting. Defaulting to Warn.");
}

return level;
}

const long megaByte = 1024 * 1024;

const string LogLevelKey = "LogLevel";
}
}
42 changes: 36 additions & 6 deletions src/ServiceControl.Monitoring/Settings.cs
Expand Up @@ -4,10 +4,10 @@ namespace ServiceControl.Monitoring
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Configuration;
using NLog;
using NLog.Common;
using Transports;

public class Settings
Expand All @@ -19,7 +19,7 @@ public Settings()
TransportType = SettingsReader.Read<string>(SettingsRootNamespace, "TransportType");

ConnectionString = GetConnectionString();
LogLevel = LoggingConfigurator.InitializeLevel();
LogLevel = InitializeLogLevel();
LogPath = SettingsReader.Read(SettingsRootNamespace, "LogPath", DefaultLogLocation());
ErrorQueue = SettingsReader.Read(SettingsRootNamespace, "ErrorQueue", "error");
HttpHostName = SettingsReader.Read<string>(SettingsRootNamespace, "HttpHostname");
Expand Down Expand Up @@ -52,13 +52,42 @@ public string EndpointName
public int MaximumConcurrencyLevel { get; set; }
public string LicenseFileText { get; set; }

static LogLevel InitializeLogLevel()
{
var defaultLevel = LogLevel.Info;

var levelText = SettingsReader.Read<string>(SettingsRootNamespace, logLevelKey);

if (string.IsNullOrWhiteSpace(levelText))
{
return defaultLevel;
}

try
{
return LogLevel.FromString(levelText);
}
catch
{
InternalLogger.Warn($"Failed to parse {logLevelKey} setting. Defaulting to {defaultLevel.Name}.");
return defaultLevel;
}
}

// SC installer always populates LogPath in app.config on installation/change/upgrade so this will only be used when
// debugging or if the entry is removed manually. In those circumstances default to the folder containing the exe
static string DefaultLogLocation()
static string DefaultLogLocation() => Path.Combine(AppContext.BaseDirectory, ".logs");

public Microsoft.Extensions.Logging.LogLevel ToHostLogLevel() => LogLevel switch
{
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
return Path.Combine(Path.GetDirectoryName(assemblyLocation), ".logs");
}
_ when LogLevel == LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
_ when LogLevel == LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
_ when LogLevel == LogLevel.Info => Microsoft.Extensions.Logging.LogLevel.Information,
_ when LogLevel == LogLevel.Warn => Microsoft.Extensions.Logging.LogLevel.Warning,
_ when LogLevel == LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
_ when LogLevel == LogLevel.Fatal => Microsoft.Extensions.Logging.LogLevel.Critical,
_ => Microsoft.Extensions.Logging.LogLevel.None
};

void TryLoadLicenseFromConfig() => LicenseFileText = SettingsReader.Read<string>(SettingsRootNamespace, "LicenseText");

Expand Down Expand Up @@ -94,6 +123,7 @@ static string GetConnectionString()

internal Func<string, Dictionary<string, string>, byte[], Func<Task>, Task> OnMessage { get; set; } = (messageId, headers, body, next) => next();

const string logLevelKey = "LogLevel";
public const string DEFAULT_ENDPOINT_NAME = "Particular.Monitoring";
public static readonly SettingsRootNamespace SettingsRootNamespace = new("Monitoring");
}
Expand Down
67 changes: 21 additions & 46 deletions src/ServiceControl/Infrastructure/Settings/LoggingSettings.cs
Expand Up @@ -2,26 +2,22 @@ namespace ServiceBus.Management.Infrastructure.Settings
{
using System;
using System.IO;
using System.Reflection;
using NLog;
using NLog.Common;
using ServiceControl.Configuration;

public class LoggingSettings
public class LoggingSettings(LogLevel defaultLevel = null, string logPath = null)
{
public LoggingSettings(LogLevel defaultLevel = null, string logPath = null)
{
LoggingLevel = InitializeLevel("LogLevel", defaultLevel ?? LogLevel.Info);
LogPath = SettingsReader.Read(Settings.SettingsRootNamespace, "LogPath", Environment.ExpandEnvironmentVariables(logPath ?? DefaultLogLocation()));
}

public LogLevel LoggingLevel { get; }
public LogLevel LoggingLevel { get; } = InitializeLogLevel(defaultLevel);

public string LogPath { get; }
public string LogPath { get; } = SettingsReader.Read(Settings.SettingsRootNamespace, "LogPath", Environment.ExpandEnvironmentVariables(logPath ?? DefaultLogLocation()));

LogLevel InitializeLevel(string key, LogLevel defaultLevel)
static LogLevel InitializeLogLevel(LogLevel defaultLevel)
{
var levelText = SettingsReader.Read<string>(Settings.SettingsRootNamespace, key);
defaultLevel ??= LogLevel.Info;

var levelText = SettingsReader.Read<string>(Settings.SettingsRootNamespace, logLevelKey);

if (string.IsNullOrWhiteSpace(levelText))
{
return defaultLevel;
Expand All @@ -33,47 +29,26 @@ LogLevel InitializeLevel(string key, LogLevel defaultLevel)
}
catch
{
InternalLogger.Warn($"Failed to parse {key} setting. Defaulting to {defaultLevel.Name}.");
InternalLogger.Warn($"Failed to parse {logLevelKey} setting. Defaulting to {defaultLevel.Name}.");
return defaultLevel;
}
}

// SC installer always populates LogPath in app.config on installation/change/upgrade so this will only be used when
// debugging or if the entry is removed manually. In those circumstances default to the folder containing the exe
static string DefaultLogLocation()
{
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
return Path.Combine(Path.GetDirectoryName(assemblyLocation), ".logs");
}
static string DefaultLogLocation() => Path.Combine(AppContext.BaseDirectory, ".logs");

public Microsoft.Extensions.Logging.LogLevel ToHostLogLevel()
public Microsoft.Extensions.Logging.LogLevel ToHostLogLevel() => LoggingLevel switch
{
if (LoggingLevel == LogLevel.Debug)
{
return Microsoft.Extensions.Logging.LogLevel.Debug;
}
if (LoggingLevel == LogLevel.Error)
{
return Microsoft.Extensions.Logging.LogLevel.Error;
}
if (LoggingLevel == LogLevel.Fatal)
{
return Microsoft.Extensions.Logging.LogLevel.Critical;
}
if (LoggingLevel == LogLevel.Warn)
{
return Microsoft.Extensions.Logging.LogLevel.Warning;
}
if (LoggingLevel == LogLevel.Info)
{
return Microsoft.Extensions.Logging.LogLevel.Information;
}
if (LoggingLevel == LogLevel.Trace)
{
return Microsoft.Extensions.Logging.LogLevel.Trace;
}

return Microsoft.Extensions.Logging.LogLevel.None;
}
_ when LoggingLevel == LogLevel.Trace => Microsoft.Extensions.Logging.LogLevel.Trace,
_ when LoggingLevel == LogLevel.Debug => Microsoft.Extensions.Logging.LogLevel.Debug,
_ when LoggingLevel == LogLevel.Info => Microsoft.Extensions.Logging.LogLevel.Information,
_ when LoggingLevel == LogLevel.Warn => Microsoft.Extensions.Logging.LogLevel.Warning,
_ when LoggingLevel == LogLevel.Error => Microsoft.Extensions.Logging.LogLevel.Error,
_ when LoggingLevel == LogLevel.Fatal => Microsoft.Extensions.Logging.LogLevel.Critical,
_ => Microsoft.Extensions.Logging.LogLevel.None
};

const string logLevelKey = "LogLevel";
}
}

0 comments on commit 4a08825

Please sign in to comment.