22// Licensed under the MIT License.
33
44using System ;
5- using System . Diagnostics ;
65using System . IO ;
76using Microsoft . Extensions . DependencyInjection ;
87using Microsoft . Extensions . Logging ;
9- using Microsoft . PowerShell . EditorServices . Logging ;
108using Microsoft . PowerShell . EditorServices . Server ;
11- using Serilog ;
12- using Serilog . Events ;
139using OmniSharp . Extensions . LanguageServer . Protocol . Server ;
1410using Microsoft . PowerShell . EditorServices . Services . Extension ;
1511
16- #if DEBUG
17- using Serilog . Debugging ;
18- #endif
12+ // The HostLogger type isn't directly referenced from this assembly, however it uses a common IObservable interface and this alias helps make it more clear the purpose. We can use Microsoft.Extensions.Logging from this point because the ALC should be loaded, but we need to only expose the IObservable to the Hosting assembly so it doesn't try to load MEL before the ALC is ready.
13+ using HostLogger = System . IObservable < ( int logLevel , string message ) > ;
1914
2015namespace Microsoft . PowerShell . EditorServices . Hosting
2116{
2217 /// <summary>
23- /// Factory class for hiding dependencies of Editor Services .
18+ /// Factory for creating the LSP server and debug server instances .
2419 /// </summary>
25- /// <remarks>
26- /// Dependency injection and logging are wrapped by factory methods on this class so that the
27- /// host assembly can construct the LSP and debug servers without directly depending on <see
28- /// cref="Microsoft.Extensions.Logging"/> and <see
29- /// cref="Microsoft.Extensions.DependencyInjection"/>.
30- /// </remarks>
3120 internal sealed class EditorServicesServerFactory : IDisposable
3221 {
22+ private readonly HostLogger _hostLogger ;
23+
3324 /// <summary>
34- /// Create a new Editor Services factory. This method will instantiate logging.
25+ /// Creates a loggerfactory for this instance
3526 /// </summary>
36- /// <remarks>
37- /// <para>
38- /// This can only be called once because it sets global state (the logger) and that call is
39- /// in <see cref="Hosting.EditorServicesRunner" />.
40- /// </para>
41- /// <para>
42- /// TODO: Why is this a static function wrapping a constructor instead of just a
43- /// constructor? In the end it returns an instance (albeit a "singleton").
44- /// </para>
45- /// </remarks>
46- /// <param name="logDirectoryPath">The path of the log file to use.</param>
47- /// <param name="minimumLogLevel">The minimum log level to use.</param>
48- /// <param name="hostLogger">The host logger?</param>
49- public static EditorServicesServerFactory Create ( string logDirectoryPath , int minimumLogLevel , IObservable < ( int logLevel , string message ) > hostLogger )
50- {
51- // NOTE: Ignore the suggestion to use Environment.ProcessId as it doesn't work for
52- // .NET 4.6.2 (for Windows PowerShell), and this won't be caught in CI.
53- int currentPID = Process . GetCurrentProcess ( ) . Id ;
54- string logPath = Path . Combine ( logDirectoryPath , $ "PowerShellEditorServices-{ currentPID } .log") ;
55- Log . Logger = new LoggerConfiguration ( )
56- . Enrich . FromLogContext ( )
57- . WriteTo . Async ( config => config . File ( logPath ) )
58- . MinimumLevel . Is ( ( LogEventLevel ) minimumLogLevel )
59- . CreateLogger ( ) ;
60-
61- #if DEBUG
62- SelfLog . Enable ( msg => Debug . WriteLine ( msg ) ) ;
63- #endif
64-
65- LoggerFactory loggerFactory = new ( ) ;
66- loggerFactory . AddSerilog ( ) ;
67-
68- // Hook up logging from the host so that its recorded in the log file
69- hostLogger . Subscribe ( new HostLoggerAdapter ( loggerFactory ) ) ;
70-
71- return new EditorServicesServerFactory ( loggerFactory ) ;
72- }
73-
74- // TODO: Can we somehow refactor this member so the language and debug servers can be
75- // instantiated using their constructors instead of tying them to this factory with `Create`
76- // methods?
77- private readonly ILoggerFactory _loggerFactory ;
78-
79- private EditorServicesServerFactory ( ILoggerFactory loggerFactory ) => _loggerFactory = loggerFactory ;
27+ /// <param name="hostLogger">The hostLogger that will be provided to the language services for logging handoff</param>
28+ internal EditorServicesServerFactory ( HostLogger hostLogger ) => _hostLogger = hostLogger ;
8029
8130 /// <summary>
8231 /// Create the LSP server.
@@ -92,7 +41,7 @@ public static EditorServicesServerFactory Create(string logDirectoryPath, int mi
9241 public PsesLanguageServer CreateLanguageServer (
9342 Stream inputStream ,
9443 Stream outputStream ,
95- HostStartupInfo hostStartupInfo ) => new ( _loggerFactory , inputStream , outputStream , hostStartupInfo ) ;
44+ HostStartupInfo hostStartupInfo ) => new ( _hostLogger , inputStream , outputStream , hostStartupInfo ) ;
9645
9746 /// <summary>
9847 /// Create the debug server given a language server instance.
@@ -110,7 +59,7 @@ public PsesDebugServer CreateDebugServerWithLanguageServer(
11059 PsesLanguageServer languageServer )
11160 {
11261 return new PsesDebugServer (
113- _loggerFactory ,
62+ _hostLogger ,
11463 inputStream ,
11564 outputStream ,
11665 languageServer . LanguageServer . Services ) ;
@@ -132,7 +81,7 @@ public PsesDebugServer RecreateDebugServer(
13281 PsesDebugServer debugServer )
13382 {
13483 return new PsesDebugServer (
135- _loggerFactory ,
84+ _hostLogger ,
13685 inputStream ,
13786 outputStream ,
13887 debugServer . ServiceProvider ) ;
@@ -153,7 +102,6 @@ public PsesDebugServer CreateDebugServerForTempSession(
153102 ServiceProvider serviceProvider = new ServiceCollection ( )
154103 . AddLogging ( builder => builder
155104 . ClearProviders ( )
156- . AddSerilog ( )
157105 . SetMinimumLevel ( LogLevel . Trace ) ) // TODO: Why randomly set to trace?
158106 . AddSingleton < ILanguageServerFacade > ( _ => null )
159107 // TODO: Why add these for a debug server?!
@@ -171,25 +119,14 @@ public PsesDebugServer CreateDebugServerForTempSession(
171119 serviceProvider . GetService < ExtensionService > ( ) ;
172120
173121 return new PsesDebugServer (
174- _loggerFactory ,
122+ _hostLogger ,
175123 inputStream ,
176124 outputStream ,
177125 serviceProvider ,
178126 isTemp : true ) ;
179127 }
180128
181- /// <summary>
182- /// TODO: This class probably should not be <see cref="IDisposable"/> as the primary
183- /// intention of that interface is to provide cleanup of unmanaged resources, which the
184- /// logger certainly is not. Nor is this class used with a <see langword="using"/>. Instead,
185- /// this class should call <see cref="Log.CloseAndFlush()"/> in a finalizer. This
186- /// could potentially even be done with <see
187- /// cref="SerilogLoggerFactoryExtensions.AddSerilog"</> by passing <c>dispose=true</c>.
188- /// </summary>
189- public void Dispose ( )
190- {
191- Log . CloseAndFlush ( ) ;
192- _loggerFactory . Dispose ( ) ;
193- }
129+ // TODO: Clean up host logger? Shouldn't matter since we start a new process after shutdown.
130+ public void Dispose ( ) { }
194131 }
195132}
0 commit comments