Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow logging level to be changed in rzls #11228

Merged
merged 8 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

<RestrictedInternalsVisibleTo Include="Microsoft.VisualStudio.LanguageServices.Razor" Namespace="Microsoft.AspNetCore.Razor.LanguageServer.Hosting" Key="$(RazorKey)" />
<RestrictedInternalsVisibleTo Include="rzls" Namespace="Microsoft.AspNetCore.Razor.LanguageServer.Hosting" Key="$(RazorKey)" />
<RestrictedInternalsVisibleTo Include="rzls" Namespace="Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts" Key="$(RazorKey)" />
ryzngard marked this conversation as resolved.
Show resolved Hide resolved
ryzngard marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/Razor/src/rzls/Endpoints/RazorUpdateLogLevelParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Text.Json.Serialization;
using Microsoft.CodeAnalysis.Razor.Logging;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Endpoints;

/// <summary>
/// Request parameters for updating the log level in the server dynamically.
/// </summary>
/// <param name="LogLevelValue">the string value of the <see cref="LogLevel"/> enum</param>
internal record class UpdateLogLevelParams([property: JsonPropertyName("logLevel")] string LogLevelValue);
23 changes: 23 additions & 0 deletions src/Razor/src/rzls/Endpoints/UpdateLogLevelEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
using Microsoft.CodeAnalysis.Razor.Logging;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Endpoints;

internal class UpdateLogLevelEndpoint(LogLevelProvider logLevelProvider) : IRazorNotificationHandler<UpdateLogLevelParams>
ryzngard marked this conversation as resolved.
Show resolved Hide resolved
ryzngard marked this conversation as resolved.
Show resolved Hide resolved
{
public bool MutatesSolutionState => false;

public Task HandleNotificationAsync(UpdateLogLevelParams request, RazorRequestContext requestContext, CancellationToken cancellationToken)
{
var logLevel = Enum.Parse<LogLevel>(request.LogLevelValue);
logLevelProvider.SetLogLevel(logLevel);

return Task.CompletedTask;
}
}
18 changes: 18 additions & 0 deletions src/Razor/src/rzls/LogLevelProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Threading;
using Microsoft.CodeAnalysis.Razor.Logging;

namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal class LogLevelProvider(LogLevel logLevel)
{
private int _logLevel = (int)logLevel;

internal LogLevel GetLogLevel()
=> (LogLevel)_logLevel;

internal void SetLogLevel(LogLevel logLevel)
=> Interlocked.Exchange(ref _logLevel, (int)logLevel);
ryzngard marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 2 additions & 5 deletions src/Razor/src/rzls/LoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@

namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal class LoggerProvider(LogLevel logLevel, IClientConnection clientConnection) : ILoggerProvider
internal class LoggerProvider(LogLevelProvider logLevelProvider, IClientConnection clientConnection) : ILoggerProvider
{
private readonly LogLevel _logLevel = logLevel;
private readonly IClientConnection _clientConnection = clientConnection;

public ILogger CreateLogger(string categoryName)
{
return new LspLogger(categoryName, _logLevel, _clientConnection);
return new LspLogger(categoryName, logLevelProvider, clientConnection);
}
}
6 changes: 3 additions & 3 deletions src/Razor/src/rzls/LspLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer;
/// <summary>
/// ILogger implementation that logs via the window/logMessage LSP method
/// </summary>
internal class LspLogger(string categoryName, LogLevel logLevel, IClientConnection clientConnection) : ILogger
internal class LspLogger(string categoryName, LogLevelProvider logLevelProvider, IClientConnection clientConnection) : ILogger
{
private readonly LogLevel _logLevel = logLevel;
private LogLevel LogLevel => logLevelProvider.GetLogLevel();
private readonly string _categoryName = categoryName;
private readonly IClientConnection _clientConnection = clientConnection;

public bool IsEnabled(LogLevel logLevel)
{
return logLevel.IsAtLeast(_logLevel);
return logLevel.IsAtLeast(LogLevel);
}

public void Log(LogLevel logLevel, string message, Exception? exception)
Expand Down
3 changes: 2 additions & 1 deletion src/Razor/src/rzls/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public static async Task Main(string[] args)

// Now we have a server, and hence a connection, we have somewhere to log
var clientConnection = host.GetRequiredService<IClientConnection>();
var loggerProvider = new LoggerProvider(logLevel, clientConnection);
var logLevelProvider = new LogLevelProvider(logLevel);
var loggerProvider = new LoggerProvider(logLevelProvider, clientConnection);
loggerFactory.AddLoggerProvider(loggerProvider);

loggerFactory.GetOrCreateLogger("RZLS").LogInformation($"Razor Language Server started successfully.");
Expand Down