diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/LoggerAdapter.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/LoggerAdapter.cs index e6a366c9ff..ee6c031160 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/LoggerAdapter.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/LoggerAdapter.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using System.Diagnostics; using System.Linq; using Microsoft.AspNetCore.Razor.PooledObjects; using Microsoft.AspNetCore.Razor.Telemetry; @@ -17,11 +18,13 @@ public class LoggerAdapter : IRazorLogger { private readonly IEnumerable _loggers; private readonly ITelemetryReporter? _telemetryReporter; + private readonly TraceSource? _traceSource; - public LoggerAdapter(IEnumerable loggers, ITelemetryReporter? telemetryReporter) + public LoggerAdapter(IEnumerable loggers, ITelemetryReporter? telemetryReporter, TraceSource? traceSource = null) { _loggers = loggers; _telemetryReporter = telemetryReporter; + _traceSource = traceSource; } public IDisposable BeginScope(TState state) @@ -52,9 +55,26 @@ public class LoggerAdapter : IRazorLogger } } + public void LogStartContext(string message, params object[] @params) + { + if (_traceSource != null) + { + // This should provide for a better activity name when looking at traces, rather than the + // random number we normally get. + _traceSource.TraceEvent(TraceEventType.Start, id: 0, message); + } + + LogInformation("Start: {message}", message); + } + public void LogEndContext(string message, params object[] @params) { - LogInformation("Exiting: {}", message); + LogInformation("Stop: {message}", message); + + if (_traceSource != null) + { + _traceSource.TraceEvent(TraceEventType.Stop, id: 0, message); + } } public void LogError(string message, params object[] @params) @@ -118,11 +138,6 @@ public class LoggerAdapter : IRazorLogger } } - public void LogStartContext(string message, params object[] @params) - { - LogInformation("Entering: {}", message); - } - public void LogWarning(string message, params object[] @params) { foreach (var logger in _loggers) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorLanguageServerWrapper.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorLanguageServerWrapper.cs index 3081425b55..6e0197715b 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorLanguageServerWrapper.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorLanguageServerWrapper.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See License.txt in the project root for license information. using System; +using System.Diagnostics; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.LanguageServer.Common.Extensions; @@ -41,10 +42,17 @@ internal sealed class RazorLanguageServerWrapper : IDisposable ProjectSnapshotManagerDispatcher? projectSnapshotManagerDispatcher = null, Action? configure = null, LanguageServerFeatureOptions? featureOptions = null, - RazorLSPOptions? razorLSPOptions = null) + RazorLSPOptions? razorLSPOptions = null, + TraceSource? traceSource = null) { var jsonRpc = CreateJsonRpc(input, output); + // This ensures each request is a separate activity in loghub + jsonRpc.ActivityTracingStrategy = new CorrelationManagerTracingStrategy + { + TraceSource = traceSource + }; + var server = new RazorLanguageServer( jsonRpc, razorLogger, diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorRequestContextFactory.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorRequestContextFactory.cs index c62d9b98df..0a0fe24177 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorRequestContextFactory.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorRequestContextFactory.cs @@ -49,7 +49,6 @@ internal class RazorRequestContextFactory : IRequestContextFactory(); - loggerAdapter.LogDebug("Entering method {methodName}.", queueItem.MethodName); var requestContext = new RazorRequestContext(documentContext, loggerAdapter, _lspServices); diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/RazorLanguageServerClient.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/RazorLanguageServerClient.cs index 127e121d36..d589c5e185 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/RazorLanguageServerClient.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/RazorLanguageServerClient.cs @@ -179,10 +179,10 @@ internal class RazorLanguageServerClient : ILanguageClient, ILanguageClientCusto // Initialize Logging Infrastructure _loggerProvider = (LogHubLoggerProvider)await _logHubLoggerProviderFactory.GetOrCreateAsync(LogFileIdentifier, token).ConfigureAwait(false); - + var traceSource = _loggerProvider.GetTraceSource(); var logHubLogger = _loggerProvider.CreateLogger("Razor"); var loggers = _outputWindowLogger == null ? new ILogger[] { logHubLogger } : new ILogger[] { logHubLogger, _outputWindowLogger }; - var razorLogger = new LoggerAdapter(loggers, _telemetryReporter); + var razorLogger = new LoggerAdapter(loggers, _telemetryReporter, traceSource); var lspOptions = RazorLSPOptions.From(_clientSettingsManager.GetClientSettings()); _server = RazorLanguageServerWrapper.Create( serverStream, @@ -192,7 +192,8 @@ internal class RazorLanguageServerClient : ILanguageClient, ILanguageClientCusto _projectSnapshotManagerDispatcher, ConfigureLanguageServer, _languageServerFeatureOptions, - lspOptions); + lspOptions, + traceSource); // This must not happen on an RPC endpoint due to UIThread concerns, so ActivateAsync was chosen. await EnsureContainedLanguageServersInitializedAsync();