Skip to content

Commit

Permalink
Improvements to logging during startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
tintoy committed Nov 16, 2018
1 parent 89c5b9d commit 3ff6897
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.2.46.{build}
version: 0.2.47.{build}
image: Visual Studio 2017
build_script:
- ps: >-
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## v0.2.47

* Improvements to logging during startup (tintoy/msbuild-project-tools-vscode#42).

## v0.2.46

* Log configured package sources when initialising a project document (tintoy/msbuild-project-tools-vscode#44).
Expand Down
7 changes: 6 additions & 1 deletion src/LanguageServer.Engine/Logging/LanguageServerSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public LanguageServerLoggingSink(ILanguageServer languageServer, LoggingLevelSwi
}
}

/// <summary>
/// Can log entries be sent to the language server?
/// </summary>
bool CanLog => _languageServer.Server != null && !_hasServerShutDown;

/// <summary>
/// Emit a log event.
/// </summary>
Expand All @@ -74,7 +79,7 @@ public LanguageServerLoggingSink(ILanguageServer languageServer, LoggingLevelSwi
/// </param>
public void Emit(LogEvent logEvent)
{
if (_hasServerShutDown)
if (!CanLog)
return;

if (logEvent.Level < _levelSwitch.MinimumLevel)
Expand Down
2 changes: 1 addition & 1 deletion src/LanguageServer/LoggingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static ILogger CreateLogger(IComponentContext componentContext)
ILogger logger = loggerConfiguration.CreateLogger();
Log.Logger = logger;

Log.Verbose("Logger initialised.");
logger.Verbose("Logger initialised.");

return logger;
}
Expand Down
22 changes: 17 additions & 5 deletions src/LanguageServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ static void Main()
{
Console.WriteLine(unexpectedError);
}
finally
{
Log.CloseAndFlush();
}
}

/// <summary>
Expand All @@ -61,16 +65,26 @@ static async Task AsyncMain()
using (Terminator terminator = new Terminator())
using (IContainer container = BuildContainer())
{
// Force initialisation of logging.
ILogger log = container.Resolve<ILogger>().ForContext(typeof(Program));

log.Debug("Creating language server...");

var server = container.Resolve<LSP.Server.LanguageServer>();

log.Debug("Waiting for client to initialise language server...");

await server.Initialize();

log.Debug("Language server initialised by client.");

await server.WasShutDown;

Log.Information("Server is shutting down...");
log.Debug("Language server is shutting down...");

await server.WaitForExit;

Log.Information("Server has shut down. Preparing to terminate server process...");
log.Debug("Server has shut down. Preparing to terminate server process...");

// AF: Temporary fix for tintoy/msbuild-project-tools-vscode#36
//
Expand All @@ -79,10 +93,8 @@ static async Task AsyncMain()
TimeSpan.FromSeconds(3)
);

// TODO: Terminate immediately if the client process has terminated unexpectedly.
log.Debug("Server process is ready to terminate.");
}

Log.Information("Server process is ready to terminate.");
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/LanguageServer/Terminator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Terminator()
/// </param>
public async void TerminateAfter(TimeSpan timeout)
{
Log.Verbose("Language server process will be forcibly terminated in {Timeout} seconds.", timeout.TotalSeconds);
Log.Debug("Language server process will be forcibly terminated in {Timeout} seconds.", timeout.TotalSeconds);

CancellationToken cancellationToken = _cancellation.Token;
_cancellation.CancelAfter(timeout);
Expand All @@ -59,12 +59,13 @@ public async void TerminateAfter(TimeSpan timeout)
{
await Task.Delay(timeout, cancellationToken);

Log.Warning("Timed out after waiting {Timeout} seconds; the language server process will now be forcibly terminated within {TerminationDelay} second(s).",
Log.Debug("Timed out after waiting {Timeout} seconds; the language server process will now be forcibly terminated within {TerminationDelay} second(s).",
timeout.TotalSeconds,
TerminationDelay.TotalSeconds
);

// Last-ditch effort to flush pending log entries.
(Log as IDisposable)?.Dispose();
Serilog.Log.CloseAndFlush();
await Task.Delay(
TimeSpan.FromSeconds(1)
Expand Down

0 comments on commit 3ff6897

Please sign in to comment.