Skip to content

Commit 76d9bb7

Browse files
Logging events to diagnose stop sequence (#4823)
* Log when application is done to determine if process was killed * Logging when Raven.Embedded.EmbeddedServer started/completed as this operation can take a while and can take longer than the host allows * Improve logging and code comments --------- Co-authored-by: Mauro Servienti <[email protected]>
1 parent 0e39e56 commit 76d9bb7

File tree

4 files changed

+81
-52
lines changed

4 files changed

+81
-52
lines changed

src/ServiceControl.Audit/Program.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,40 @@
77
using ServiceControl.Configuration;
88
using ServiceControl.Infrastructure;
99

10-
AppDomain.CurrentDomain.UnhandledException += (s, e) => LogManager.GetLogger(typeof(Program)).Error("Unhandled exception was caught.", e.ExceptionObject as Exception);
10+
try
11+
{
12+
AppDomain.CurrentDomain.UnhandledException += (s, e) => LogManager.GetLogger(typeof(Program)).Error("Unhandled exception was caught.", e.ExceptionObject as Exception);
1113

12-
// Hack: See https://github.com/Particular/ServiceControl/issues/4392
13-
var exitCode = await IntegratedSetup.Run();
14+
// Hack: See https://github.com/Particular/ServiceControl/issues/4392
15+
var exitCode = await IntegratedSetup.Run();
1416

15-
if (exitCode != 0)
16-
{
17-
return exitCode;
18-
}
17+
if (exitCode != 0)
18+
{
19+
return exitCode;
20+
}
1921

20-
ExeConfiguration.PopulateAppSettings(Assembly.GetExecutingAssembly());
22+
ExeConfiguration.PopulateAppSettings(Assembly.GetExecutingAssembly());
2123

22-
var arguments = new HostArguments(args);
24+
var arguments = new HostArguments(args);
2325

24-
if (arguments.Help)
25-
{
26-
arguments.PrintUsage();
27-
return 0;
28-
}
26+
if (arguments.Help)
27+
{
28+
arguments.PrintUsage();
29+
return 0;
30+
}
2931

30-
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
31-
LoggingConfigurator.ConfigureLogging(loggingSettings);
32+
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
33+
LoggingConfigurator.ConfigureLogging(loggingSettings);
3234

33-
var settings = new Settings(loggingSettings: loggingSettings);
35+
var settings = new Settings(loggingSettings: loggingSettings);
3436

35-
await new CommandRunner(arguments.Command).Execute(arguments, settings);
37+
await new CommandRunner(arguments.Command).Execute(arguments, settings);
3638

37-
return 0;
39+
return 0;
40+
}
41+
finally
42+
{
43+
// The following log statement is meant to leave a trail in the logs to determine if the process was killed
44+
NLog.LogManager.GetCurrentClassLogger().Info("Shutdown complete");
45+
NLog.LogManager.Shutdown();
46+
}

src/ServiceControl.Monitoring/Program.cs

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

8-
AppDomain.CurrentDomain.UnhandledException += (s, e) => LogManager.GetLogger(typeof(Program)).Error("Unhandled exception was caught.", e.ExceptionObject as Exception);
8+
try
9+
{
10+
AppDomain.CurrentDomain.UnhandledException += (s, e) => LogManager.GetLogger(typeof(Program)).Error("Unhandled exception was caught.", e.ExceptionObject as Exception);
911

10-
// Hack: See https://github.com/Particular/ServiceControl/issues/4392
11-
var exitCode = await IntegratedSetup.Run();
12+
// Hack: See https://github.com/Particular/ServiceControl/issues/4392
13+
var exitCode = await IntegratedSetup.Run();
1214

13-
if (exitCode != 0)
14-
{
15-
return exitCode;
16-
}
15+
if (exitCode != 0)
16+
{
17+
return exitCode;
18+
}
1719

18-
ExeConfiguration.PopulateAppSettings(Assembly.GetExecutingAssembly());
20+
ExeConfiguration.PopulateAppSettings(Assembly.GetExecutingAssembly());
1921

20-
var arguments = new HostArguments(args);
22+
var arguments = new HostArguments(args);
2123

22-
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
23-
LoggingConfigurator.ConfigureLogging(loggingSettings);
24+
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
25+
LoggingConfigurator.ConfigureLogging(loggingSettings);
2426

25-
var settings = new Settings(loggingSettings: loggingSettings);
27+
var settings = new Settings(loggingSettings: loggingSettings);
2628

27-
await new CommandRunner(arguments.Command).Execute(arguments, settings);
29+
await new CommandRunner(arguments.Command).Execute(arguments, settings);
2830

29-
return 0;
31+
return 0;
32+
}
33+
finally
34+
{
35+
// The following log statement is meant to leave a trail in the logs to determine if the process was killed
36+
NLog.LogManager.GetCurrentClassLogger().Info("Shutdown complete");
37+
NLog.LogManager.Shutdown();
38+
}

src/ServiceControl.RavenDB/EmbeddedDatabase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ public void Dispose()
176176
EmbeddedServer.Instance.ServerProcessExited -= OnServerProcessExited;
177177

178178
shutdownTokenSource.Cancel();
179+
Logger.Debug("Disposing RavenDB server");
179180
EmbeddedServer.Instance.Dispose();
181+
Logger.Debug("Dispose RavenDB server");
180182
shutdownTokenSource.Dispose();
181183
applicationStoppingRegistration.Dispose();
182184

src/ServiceControl/Program.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,40 @@
77
using ServiceControl.Hosting.Commands;
88
using ServiceControl.Infrastructure;
99

10-
AppDomain.CurrentDomain.UnhandledException += (s, e) => LogManager.GetLogger(typeof(Program)).Error("Unhandled exception was caught.", e.ExceptionObject as Exception);
10+
try
11+
{
12+
AppDomain.CurrentDomain.UnhandledException += (s, e) => LogManager.GetLogger(typeof(Program)).Error("Unhandled exception was caught.", e.ExceptionObject as Exception);
1113

12-
// Hack: See https://github.com/Particular/ServiceControl/issues/4392
13-
var exitCode = await IntegratedSetup.Run();
14+
// Hack: See https://github.com/Particular/ServiceControl/issues/4392
15+
var exitCode = await IntegratedSetup.Run();
1416

15-
if (exitCode != 0)
16-
{
17-
return exitCode;
18-
}
17+
if (exitCode != 0)
18+
{
19+
return exitCode;
20+
}
1921

20-
ExeConfiguration.PopulateAppSettings(Assembly.GetExecutingAssembly());
22+
ExeConfiguration.PopulateAppSettings(Assembly.GetExecutingAssembly());
2123

22-
var arguments = new HostArguments(args);
24+
var arguments = new HostArguments(args);
2325

24-
if (arguments.Help)
25-
{
26-
arguments.PrintUsage();
27-
return 0;
28-
}
26+
if (arguments.Help)
27+
{
28+
arguments.PrintUsage();
29+
return 0;
30+
}
2931

30-
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
31-
LoggingConfigurator.ConfigureLogging(loggingSettings);
32+
var loggingSettings = new LoggingSettings(Settings.SettingsRootNamespace);
33+
LoggingConfigurator.ConfigureLogging(loggingSettings);
3234

33-
var settings = new Settings(loggingSettings: loggingSettings);
35+
var settings = new Settings(loggingSettings: loggingSettings);
3436

35-
await new CommandRunner(arguments.Command).Execute(arguments, settings);
37+
await new CommandRunner(arguments.Command).Execute(arguments, settings);
3638

37-
return 0;
39+
return 0;
40+
}
41+
finally
42+
{
43+
// The following log statement is meant to leave a trail in the logs to determine if the process was killed
44+
NLog.LogManager.GetCurrentClassLogger().Info("Shutdown complete");
45+
NLog.LogManager.Shutdown();
46+
}

0 commit comments

Comments
 (0)