Skip to content

Commit 0721cbf

Browse files
committed
Make loggerfactory react on loglevel changes.
1 parent 0da7b96 commit 0721cbf

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

sample/SampleServer/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static void Main(string[] args)
1919

2020
static async Task MainAsync(string[] args)
2121
{
22-
Debugger.Launch();
22+
//Debugger.Launch();
2323
//while (!System.Diagnostics.Debugger.IsAttached)
2424
//{
2525
// await Task.Delay(100);
@@ -38,8 +38,8 @@ static async Task MainAsync(string[] args)
3838
.WithOutput(Console.OpenStandardOutput())
3939
.ConfigureLogging(x => x
4040
.AddSerilog()
41-
.AddLanguageServer(LogLevel.Critical)
42-
.SetMinimumLevel(LogLevel.Trace))
41+
.AddLanguageServer(LogLevel.Error)
42+
.SetMinimumLevel(LogLevel.Error))
4343
.WithHandler<TextDocumentHandler>()
4444
.WithHandler<DidChangeWatchedFilesHandler>()
4545
.WithHandler<FoldingRangeHandler>()

src/Server/LanguageServer.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using ISerializer = OmniSharp.Extensions.LanguageServer.Protocol.Serialization.ISerializer;
2828
using System.Reactive.Disposables;
2929
using Microsoft.Extensions.Options;
30+
using OmniSharp.Extensions.LanguageServer.Server.Logging;
3031

3132
namespace OmniSharp.Extensions.LanguageServer.Server
3233
{
@@ -132,6 +133,7 @@ internal LanguageServer(
132133
var outputHandler = new OutputHandler(output, serializer);
133134

134135
services.AddLogging(builder => loggingBuilderAction(builder));
136+
services.AddSingleton<IOptionsMonitor<LoggerFilterOptions>, LanguageServerLoggerFilterOptions>();
135137

136138
_reciever = reciever;
137139
_serializer = serializer;
@@ -347,10 +349,18 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
347349
{
348350
var loggerSettings = _serviceProvider.GetService<LanguageServerLoggerSettings>();
349351

350-
if (loggerSettings?.MinimumLogLevel >= LogLevel.Information)
352+
if (loggerSettings?.MinimumLogLevel <= LogLevel.Information)
351353
{
352354
loggerSettings.MinimumLogLevel = LogLevel.Trace;
353355
}
356+
357+
var optionsMonitor = _serviceProvider.GetService<IOptionsMonitor<LoggerFilterOptions>>() as LanguageServerLoggerFilterOptions;
358+
359+
if (optionsMonitor?.CurrentValue.MinLevel <= LogLevel.Information)
360+
{
361+
optionsMonitor.CurrentValue.MinLevel = LogLevel.Trace;
362+
optionsMonitor.Set(optionsMonitor.CurrentValue);
363+
}
354364
}
355365

356366
_clientVersion = request.Capabilities?.GetClientVersion() ?? ClientVersion.Lsp2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Options;
5+
6+
namespace OmniSharp.Extensions.LanguageServer.Server.Logging
7+
{
8+
internal class LanguageServerLoggerFilterOptions : IOptionsMonitor<LoggerFilterOptions>, IDisposable
9+
{
10+
private readonly List<IDisposable> _registrations = new List<IDisposable>();
11+
private event Action<LoggerFilterOptions, string> _onChange;
12+
13+
public LanguageServerLoggerFilterOptions(IOptions<LoggerFilterOptions> options)
14+
{
15+
CurrentValue = options.Value;
16+
}
17+
18+
public LoggerFilterOptions CurrentValue { get; private set; }
19+
20+
public LoggerFilterOptions Get(string _) => CurrentValue;
21+
22+
public IDisposable OnChange(Action<LoggerFilterOptions, string> listener)
23+
{
24+
var disposable = new ChangeTrackerDisposable(this, listener);
25+
_onChange += disposable.OnChange;
26+
return disposable;
27+
}
28+
29+
public void Dispose()
30+
{
31+
foreach (var registration in _registrations)
32+
{
33+
registration.Dispose();
34+
}
35+
36+
_registrations.Clear();
37+
}
38+
39+
internal void Set(LoggerFilterOptions options)
40+
{
41+
CurrentValue = options;
42+
_onChange?.Invoke(options, Options.DefaultName);
43+
}
44+
45+
private class ChangeTrackerDisposable : IDisposable
46+
{
47+
private readonly Action<LoggerFilterOptions, string> _listener;
48+
private readonly LanguageServerLoggerFilterOptions _monitor;
49+
50+
public ChangeTrackerDisposable(LanguageServerLoggerFilterOptions monitor, Action<LoggerFilterOptions, string> listener)
51+
{
52+
_listener = listener;
53+
_monitor = monitor;
54+
}
55+
56+
public void OnChange(LoggerFilterOptions options, string name) => _listener.Invoke(options, name);
57+
58+
public void Dispose() => _monitor._onChange -= OnChange;
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)