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 ;
11+ using OmniSharp . Extensions . LanguageServer . Server ;
1512
16- #if DEBUG
17- using Serilog . Debugging ;
18- #endif
13+ // 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.
14+ using HostLogger = System . IObservable < ( int logLevel , string message ) > ;
1915
2016namespace Microsoft . PowerShell . EditorServices . Hosting
2117{
2218 /// <summary>
23- /// Factory class for hiding dependencies of Editor Services .
19+ /// Factory for creating the LSP server and debug server instances .
2420 /// </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>
3121 internal sealed class EditorServicesServerFactory : IDisposable
3222 {
23+ private readonly HostLogger _hostLogger ;
24+
3325 /// <summary>
34- /// Create a new Editor Services factory. This method will instantiate logging.
26+ /// Creates a loggerfactory for this instance
3527 /// </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 ;
28+ /// <param name="hostLogger">The hostLogger that will be provided to the language services for logging handoff</param>
29+ internal EditorServicesServerFactory ( HostLogger hostLogger ) => _hostLogger = hostLogger ;
8030
8131 /// <summary>
8232 /// Create the LSP server.
@@ -92,7 +42,7 @@ public static EditorServicesServerFactory Create(string logDirectoryPath, int mi
9242 public PsesLanguageServer CreateLanguageServer (
9343 Stream inputStream ,
9444 Stream outputStream ,
95- HostStartupInfo hostStartupInfo ) => new ( _loggerFactory , inputStream , outputStream , hostStartupInfo ) ;
45+ HostStartupInfo hostStartupInfo ) => new ( _hostLogger , inputStream , outputStream , hostStartupInfo ) ;
9646
9747 /// <summary>
9848 /// Create the debug server given a language server instance.
@@ -110,7 +60,7 @@ public PsesDebugServer CreateDebugServerWithLanguageServer(
11060 PsesLanguageServer languageServer )
11161 {
11262 return new PsesDebugServer (
113- _loggerFactory ,
63+ _hostLogger ,
11464 inputStream ,
11565 outputStream ,
11666 languageServer . LanguageServer . Services ) ;
@@ -132,7 +82,7 @@ public PsesDebugServer RecreateDebugServer(
13282 PsesDebugServer debugServer )
13383 {
13484 return new PsesDebugServer (
135- _loggerFactory ,
85+ _hostLogger ,
13686 inputStream ,
13787 outputStream ,
13888 debugServer . ServiceProvider ) ;
@@ -153,7 +103,7 @@ public PsesDebugServer CreateDebugServerForTempSession(
153103 ServiceProvider serviceProvider = new ServiceCollection ( )
154104 . AddLogging ( builder => builder
155105 . ClearProviders ( )
156- . AddSerilog ( )
106+ . AddLanguageProtocolLogging ( )
157107 . SetMinimumLevel ( LogLevel . Trace ) ) // TODO: Why randomly set to trace?
158108 . AddSingleton < ILanguageServerFacade > ( _ => null )
159109 // TODO: Why add these for a debug server?!
@@ -171,25 +121,14 @@ public PsesDebugServer CreateDebugServerForTempSession(
171121 serviceProvider . GetService < ExtensionService > ( ) ;
172122
173123 return new PsesDebugServer (
174- _loggerFactory ,
124+ _hostLogger ,
175125 inputStream ,
176126 outputStream ,
177127 serviceProvider ,
178128 isTemp : true ) ;
179129 }
180130
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- }
131+ // TODO: Clean up host logger? Shouldn't matter since we start a new process after shutdown.
132+ public void Dispose ( ) { }
194133 }
195134}
0 commit comments