-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support in the LSIF generator for logging to stderr
LSIF tools can implement an optional logging format which is written to stderr; this implements that format.
- Loading branch information
1 parent
475fe65
commit 4bc3379
Showing
4 changed files
with
106 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }; | ||
|
||
// If we have an exception, we'll use the exception message, otherwise the logged message. It doesn't appear we can log both at once. | ||
var command = new CommandWithParameters("log", | ||
new LogCommandParameters(severity, exception?.Message ?? 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); | ||
private sealed record LogCommandParameters( | ||
string Severity, | ||
string Message, | ||
string? Exception, | ||
string? CallStack); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
' 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. | ||
|
||
Imports System.IO | ||
Imports Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.Logging | ||
Imports Microsoft.Extensions.Logging | ||
|
||
Namespace Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.UnitTests | ||
Public Class LsifFormatLoggerTests | ||
<Theory> | ||
<InlineData(LogLevel.Information, "Info")> | ||
<InlineData(LogLevel.Warning, "Warning")> | ||
<InlineData(LogLevel.Error, "Error")> | ||
<InlineData(LogLevel.Critical, "Critical")> | ||
Public Sub TestSimpleMessage(logLevel As LogLevel, expectedSeverity As String) | ||
Dim writer = New StringWriter | ||
Dim logger = New LsifFormatLogger(writer) | ||
|
||
logger.Log(logLevel, "Test message") | ||
|
||
Assert.Equal("{""command"":""log"",""parameters"":{""severity"":""" + expectedSeverity + """,""message"":""Test message""}}", writer.ToString()) | ||
End Sub | ||
|
||
<Fact> | ||
Public Sub TestException() | ||
Dim writer = New StringWriter | ||
Dim logger = New LsifFormatLogger(writer) | ||
|
||
logger.LogError(New Exception("Exception!"), "An exception was thrown") | ||
|
||
Assert.Equal("{""command"":""log"",""parameters"":{""severity"":""Error"",""message"":""Exception!"",""exception"":""System.Exception""}}", writer.ToString()) | ||
End Sub | ||
End Class | ||
End Namespace |