1
- using BaseProtocol ;
2
- using BaseProtocol . Protocol ;
3
- using Microsoft . Extensions . Logging ;
4
-
5
- /// <summary>
6
- /// Implements an ILogger that seamlessly switches from a fallback logger
7
- /// to BSP log messages as soon as the server initializes.
8
- /// </summary>
9
- internal sealed class BspLogMessageLogger : ILogger
10
- {
11
- private readonly string _categoryName ;
12
- private readonly ILogger _fallbackLogger ;
13
-
14
- public BspLogMessageLogger ( string categoryName , ILoggerFactory fallbackLoggerFactory )
15
- {
16
- _categoryName = categoryName ;
17
- _fallbackLogger = fallbackLoggerFactory . CreateLogger ( categoryName ) ;
18
- }
19
-
20
- public IDisposable BeginScope < TState > ( TState state ) where TState : notnull
21
- {
22
- throw new NotImplementedException ( ) ;
23
- }
24
-
25
- public bool IsEnabled ( LogLevel logLevel )
26
- {
27
- return true ;
28
- }
29
-
30
- public void Log < TState > ( LogLevel logLevel , EventId eventId , TState state , Exception ? exception , Func < TState , Exception ? , string > formatter )
31
- {
32
- var server = BuildServerHost . Instance ;
33
- if ( server == null )
34
- {
35
- // If the language server has not been initialized yet, log using the fallback logger.
36
- _fallbackLogger . Log ( logLevel , eventId , state , exception , formatter ) ;
37
- return ;
38
- }
39
-
40
- var message = formatter ( state , exception ) ;
41
-
42
- // HACK: work around https://github.com/dotnet/runtime/issues/67597: the formatter function we passed the exception to completely ignores the exception,
43
- // we'll add an exception message back in. If we didn't have a message, we'll just replace it with the exception text.
44
- if ( exception != null )
45
- {
46
- var exceptionString = exception . ToString ( ) ;
47
- if ( message == "[null]" ) // https://github.com/dotnet/runtime/blob/013ca673f6316dbbe71c7b327d7b8fa41cf8c992/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs#L19
48
- message = exceptionString ;
49
- else
50
- message += " " + exceptionString ;
51
- }
52
-
53
- if ( message != null && logLevel != LogLevel . None )
54
- {
55
- message = $ "[{ _categoryName } ] { message } ";
56
- var _ = server . GetRequiredBspService < IBaseProtocolClientManager > ( ) . SendNotificationAsync ( bsp4csharp . Protocol . Methods . BuildLogMessage , new LogMessageParams ( )
57
- {
58
- Message = message ,
59
- MessageType = logLevel switch
60
- {
61
- LogLevel . Trace => MessageType . Log ,
62
- LogLevel . Debug => MessageType . Log ,
63
- LogLevel . Information => MessageType . Info ,
64
- LogLevel . Warning => MessageType . Warning ,
65
- LogLevel . Error => MessageType . Error ,
66
- LogLevel . Critical => MessageType . Error ,
67
- _ => throw new InvalidOperationException ( $ "Unexpected logLevel argument { logLevel } ") ,
68
- }
69
- } , CancellationToken . None ) ;
70
- }
71
- }
72
- }
1
+ using BaseProtocol ;
2
+ using BaseProtocol . Protocol ;
3
+ using Microsoft . Extensions . Logging ;
4
+
5
+ /// <summary>
6
+ /// Implements an ILogger that seamlessly switches from a fallback logger
7
+ /// to BSP log messages as soon as the server initializes.
8
+ /// </summary>
9
+ internal sealed class BspLogMessageLogger : ILogger
10
+ {
11
+ private readonly string _categoryName ;
12
+ private readonly ILogger _fallbackLogger ;
13
+
14
+ public BspLogMessageLogger ( string categoryName , ILoggerFactory fallbackLoggerFactory )
15
+ {
16
+ _categoryName = categoryName ;
17
+ _fallbackLogger = fallbackLoggerFactory . CreateLogger ( categoryName ) ;
18
+ }
19
+
20
+ public IDisposable BeginScope < TState > ( TState state ) where TState : notnull
21
+ {
22
+ throw new NotImplementedException ( ) ;
23
+ }
24
+
25
+ public bool IsEnabled ( LogLevel logLevel )
26
+ {
27
+ return true ;
28
+ }
29
+
30
+ public void Log < TState > ( LogLevel logLevel , EventId eventId , TState state , Exception ? exception , Func < TState , Exception ? , string > formatter )
31
+ {
32
+ var server = BuildServerHost . Instance ;
33
+ if ( server == null )
34
+ {
35
+ // If the language server has not been initialized yet, log using the fallback logger.
36
+ _fallbackLogger . Log ( logLevel , eventId , state , exception , formatter ) ;
37
+ return ;
38
+ }
39
+
40
+ var message = formatter ( state , exception ) ;
41
+
42
+ // HACK: work around https://github.com/dotnet/runtime/issues/67597: the formatter function we passed the exception to completely ignores the exception,
43
+ // we'll add an exception message back in. If we didn't have a message, we'll just replace it with the exception text.
44
+ if ( exception != null )
45
+ {
46
+ var exceptionString = exception . ToString ( ) ;
47
+ if ( message == "[null]" ) // https://github.com/dotnet/runtime/blob/013ca673f6316dbbe71c7b327d7b8fa41cf8c992/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs#L19
48
+ message = exceptionString ;
49
+ else
50
+ message += " " + exceptionString ;
51
+ }
52
+
53
+ if ( message != null && logLevel != LogLevel . None )
54
+ {
55
+ message = $ "[{ _categoryName } ] { message } ";
56
+ var _ = server . GetRequiredBspService < IBaseProtocolClientManager > ( ) . SendNotificationAsync ( bsp4csharp . Protocol . Methods . BuildLogMessage , new LogMessageParams ( )
57
+ {
58
+ Message = message ,
59
+ MessageType = logLevel switch
60
+ {
61
+ LogLevel . Trace => MessageType . Log ,
62
+ LogLevel . Debug => MessageType . Log ,
63
+ LogLevel . Information => MessageType . Info ,
64
+ LogLevel . Warning => MessageType . Warning ,
65
+ LogLevel . Error => MessageType . Error ,
66
+ LogLevel . Critical => MessageType . Error ,
67
+ _ => throw new InvalidOperationException ( $ "Unexpected logLevel argument { logLevel } ") ,
68
+ }
69
+ } , CancellationToken . None ) ;
70
+ }
71
+ }
72
+ }
0 commit comments