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

Implement support in the LSIF generator for logging to stderr #70067

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions src/Features/Lsif/Generator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.Writing;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Roslyn.Utilities;
using LspProtocol = Microsoft.VisualStudio.LanguageServer.Protocol;
Expand Down Expand Up @@ -53,18 +54,18 @@ internal sealed class Generator
};

private readonly ILsifJsonWriter _lsifJsonWriter;
private readonly TextWriter _logFile;
private readonly ILogger _logger;
private readonly IdFactory _idFactory = new IdFactory();

private Generator(ILsifJsonWriter lsifJsonWriter, TextWriter logFile)
private Generator(ILsifJsonWriter lsifJsonWriter, ILogger logger)
{
_lsifJsonWriter = lsifJsonWriter;
_logFile = logFile;
_logger = logger;
}

public static Generator CreateAndWriteCapabilitiesVertex(ILsifJsonWriter lsifJsonWriter, TextWriter logFile)
public static Generator CreateAndWriteCapabilitiesVertex(ILsifJsonWriter lsifJsonWriter, ILogger logger)
{
var generator = new Generator(lsifJsonWriter, logFile);
var generator = new Generator(lsifJsonWriter, logger);

// Pass the set of supported SemanticTokenTypes. Order must match the order used for serialization of
// semantic tokens array. This array is analogous to the equivalent array in
Expand Down Expand Up @@ -167,8 +168,7 @@ public async Task GenerateForProjectAsync(
var exception = tasks[i].Exception!.InnerExceptions.Single();
exceptions.Add(exception);

await _logFile.WriteLineAsync($"Exception while processing {documents[i].FilePath}:");
await _logFile.WriteLineAsync(exception.ToString());
_logger.LogError(exception, $"Exception while processing {documents[i].FilePath}");
}
}

Expand Down
58 changes: 58 additions & 0 deletions src/Features/Lsif/Generator/Logging/LsifFormatLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.Writing;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.Logging
{
internal sealed class LsifFormatLogger : ILogger
{
private readonly TextWriter _writer;
private readonly object _writerGate = new object();

public LsifFormatLogger(TextWriter writer)
{
_writer = writer;
}

public IDisposable BeginScope<TState>(TState state)
{
throw new NotImplementedException();
}

public bool IsEnabled(LogLevel logLevel)
{
return logLevel >= LogLevel.Information;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
var message = formatter(state, exception);

var severity = logLevel switch { LogLevel.Information => "Info", LogLevel.Warning => "Warning", LogLevel.Error => "Error", LogLevel.Critical => "Critical", _ => throw ExceptionUtilities.UnexpectedValue(logLevel) };

var command = new CommandWithParameters("log",
new LogCommandParameters(severity, message, exception?.Message, exception?.GetType().ToString(), exception?.StackTrace));
var serializedCommand = JsonConvert.SerializeObject(command, LineModeLsifJsonWriter.SerializerSettings);

lock (_writerGate)
{
_writer.Write(serializedCommand);
}
}

private sealed record CommandWithParameters(string Command, object Parameters);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a standard format from somewhere? Or are we creating our own logging protocol?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a new format for LSIF generators specifically; @gundermanc can comment more on that. There's a part I haven't implemented yet here where it also can contain data points like "number of documents processed" which we can also use to monitor system health.

private sealed record LogCommandParameters(
string Severity,
string Message,
string? ExceptionMessage,
string? ExceptionType,
string? CallStack);
}
}
46 changes: 46 additions & 0 deletions src/Features/Lsif/Generator/Logging/PlainTextLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using Microsoft.Extensions.Logging;

namespace Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.Logging
{
internal sealed class PlainTextLogger : ILogger
{
private readonly TextWriter _writer;
private readonly object _gate = new object();

public PlainTextLogger(TextWriter writer)
{
_writer = writer;
}

public IDisposable BeginScope<TState>(TState state)
{
throw new NotImplementedException();
}

public bool IsEnabled(LogLevel logLevel)
{
return true;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
// Not all formatters will actually include the exception even if we pass it through, so include it here.
var message = formatter(state, exception);
var exceptionString = exception?.ToString();

lock (_gate)
{
_writer.WriteLine(message);

if (exceptionString != null)
_writer.WriteLine(exceptionString);
}
}
}
}
Loading