Skip to content

Commit ec9c21f

Browse files
committed
fix logging not being available during logging setup
1 parent 7edf38f commit ec9c21f

File tree

4 files changed

+57
-21
lines changed

4 files changed

+57
-21
lines changed

src/ServiceControl.Audit/Program.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
using ServiceControl.Configuration;
88
using ServiceControl.Infrastructure;
99

10-
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
11-
LoggingConfigurator.ConfigureLogging(loggingSettings);
12-
var logger = LoggerUtil.CreateStaticLogger(typeof(Program));
10+
ILogger logger = null;
1311

1412
try
1513
{
14+
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
15+
LoggingConfigurator.ConfigureLogging(loggingSettings);
16+
logger = LoggerUtil.CreateStaticLogger(typeof(Program));
17+
1618
AppDomain.CurrentDomain.UnhandledException += (s, e) => logger.LogError(e.ExceptionObject as Exception, "Unhandled exception was caught.");
1719

1820
// Hack: See https://github.com/Particular/ServiceControl/issues/4392
@@ -41,12 +43,20 @@
4143
}
4244
catch (Exception ex)
4345
{
44-
logger.LogCritical(ex, "Unrecoverable error");
46+
if (logger != null)
47+
{
48+
logger.LogCritical(ex, "Unrecoverable error");
49+
}
50+
else
51+
{
52+
LoggingConfigurator.ConfigureNLog("bootstrap.${shortdate}.txt", "./", NLog.LogLevel.Fatal);
53+
NLog.LogManager.GetCurrentClassLogger().Fatal(ex, "Unrecoverable error");
54+
}
4555
throw;
4656
}
4757
finally
4858
{
4959
// The following log statement is meant to leave a trail in the logs to determine if the process was killed
50-
logger.LogInformation("Shutdown complete");
60+
logger?.LogInformation("Shutdown complete");
5161
NLog.LogManager.Shutdown();
5262
}

src/ServiceControl.Infrastructure/LoggingConfigurator.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public static void ConfigureLogging(LoggingSettings loggingSettings)
2424
return;
2525
}
2626

27+
var logLevel = loggingSettings.LogLevel.ToNLogLevel();
28+
var loggingTo = ConfigureNLog("logfile.${shortdate}.txt", loggingSettings.LogPath, loggingSettings.LogLevel.ToNLogLevel());
29+
30+
//using LogManager here rather than LoggerUtil.CreateStaticLogger since this is exclusive to NLog
31+
var logger = LogManager.GetLogger("LoggingConfiguration");
32+
logger.InfoFormat("Logging to {0} with LogLevel '{1}'", loggingTo, logLevel.Name);
33+
}
34+
35+
public static string ConfigureNLog(string logFileName, string logPath, LogLevel logLevel)
36+
{
2737
//configure NLog
2838
var nlogConfig = new LoggingConfiguration();
2939
var simpleLayout = new SimpleLayout("${longdate}|${processtime}|${threadid}|${level}|${logger}|${message}${onexception:|${exception:format=tostring}}");
@@ -32,8 +42,8 @@ public static void ConfigureLogging(LoggingSettings loggingSettings)
3242
{
3343
Name = "file",
3444
ArchiveEvery = FileArchivePeriod.Day,
35-
FileName = Path.Combine(loggingSettings.LogPath, "logfile.${shortdate}.txt"),
36-
ArchiveFileName = Path.Combine(loggingSettings.LogPath, "logfile.{#}.txt"),
45+
FileName = Path.Combine(logPath, logFileName),
46+
ArchiveFileName = Path.Combine(logPath, "logfile.{#}.txt"),
3747
ArchiveNumbering = ArchiveNumberingMode.DateAndSequence,
3848
Layout = simpleLayout,
3949
MaxArchiveFiles = 14,
@@ -64,7 +74,6 @@ public static void ConfigureLogging(LoggingSettings loggingSettings)
6474
nlogConfig.LoggingRules.Add(aspNetCoreRule);
6575
nlogConfig.LoggingRules.Add(httpClientRule);
6676

67-
var logLevel = loggingSettings.LogLevel.ToNLogLevel();
6877
nlogConfig.LoggingRules.Add(new LoggingRule("*", logLevel, consoleTarget));
6978

7079
if (!AppEnvironment.RunningInContainer)
@@ -74,11 +83,8 @@ public static void ConfigureLogging(LoggingSettings loggingSettings)
7483

7584
NLog.LogManager.Configuration = nlogConfig;
7685

77-
//using LogManager here rather than LoggerUtil.CreateStaticLogger since this is exclusive to NLog
78-
var logger = LogManager.GetLogger("LoggingConfiguration");
7986
var logEventInfo = new LogEventInfo { TimeStamp = DateTime.UtcNow };
80-
var loggingTo = AppEnvironment.RunningInContainer ? "console" : fileTarget.FileName.Render(logEventInfo);
81-
logger.InfoFormat("Logging to {0} with LogLevel '{1}'", loggingTo, logLevel.Name);
87+
return AppEnvironment.RunningInContainer ? "console" : fileTarget.FileName.Render(logEventInfo);
8288
}
8389

8490
static LogLevel ToNLogLevel(this Microsoft.Extensions.Logging.LogLevel level)

src/ServiceControl.Monitoring/Program.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
using ServiceControl.Infrastructure;
66
using ServiceControl.Monitoring;
77

8-
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
9-
LoggingConfigurator.ConfigureLogging(loggingSettings);
10-
var logger = LoggerUtil.CreateStaticLogger<Program>();
8+
ILogger logger = null;
119

1210
try
1311
{
12+
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
13+
LoggingConfigurator.ConfigureLogging(loggingSettings);
14+
logger = LoggerUtil.CreateStaticLogger(typeof(Program));
15+
1416
AppDomain.CurrentDomain.UnhandledException += (s, e) => logger.LogError(e.ExceptionObject as Exception, "Unhandled exception was caught.");
1517

1618
// Hack: See https://github.com/Particular/ServiceControl/issues/4392
@@ -33,7 +35,15 @@
3335
}
3436
catch (Exception ex)
3537
{
36-
logger.LogCritical(ex, "Unrecoverable error");
38+
if (logger != null)
39+
{
40+
logger.LogCritical(ex, "Unrecoverable error");
41+
}
42+
else
43+
{
44+
LoggingConfigurator.ConfigureNLog("bootstrap.${shortdate}.txt", "./", NLog.LogLevel.Fatal);
45+
NLog.LogManager.GetCurrentClassLogger().Fatal(ex, "Unrecoverable error");
46+
}
3747
throw;
3848
}
3949
finally

src/ServiceControl/Program.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
using ServiceControl.Hosting.Commands;
88
using ServiceControl.Infrastructure;
99

10-
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
11-
LoggingConfigurator.ConfigureLogging(loggingSettings);
12-
var logger = LoggerUtil.CreateStaticLogger(typeof(Program));
10+
ILogger logger = null;
1311

1412
try
1513
{
14+
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
15+
LoggingConfigurator.ConfigureLogging(loggingSettings);
16+
logger = LoggerUtil.CreateStaticLogger(typeof(Program));
17+
1618
AppDomain.CurrentDomain.UnhandledException += (s, e) => logger.LogError(e.ExceptionObject as Exception, "Unhandled exception was caught");
1719

1820
// Hack: See https://github.com/Particular/ServiceControl/issues/4392
@@ -41,12 +43,20 @@
4143
}
4244
catch (Exception ex)
4345
{
44-
logger.LogCritical(ex, "Unrecoverable error");
46+
if (logger != null)
47+
{
48+
logger.LogCritical(ex, "Unrecoverable error");
49+
}
50+
else
51+
{
52+
LoggingConfigurator.ConfigureNLog("bootstrap.${shortdate}.txt", "./", NLog.LogLevel.Fatal);
53+
NLog.LogManager.GetCurrentClassLogger().Fatal(ex, "Unrecoverable error");
54+
}
4555
throw;
4656
}
4757
finally
4858
{
4959
// The following log statement is meant to leave a trail in the logs to determine if the process was killed
50-
logger.LogInformation("Shutdown complete");
60+
logger?.LogInformation("Shutdown complete");
5161
NLog.LogManager.Shutdown();
5262
}

0 commit comments

Comments
 (0)