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

Remove MS.Ext.Logging and Abstractions from most of our repo #10199

Merged
merged 10 commits into from
Apr 2, 2024

Conversation

davidwengier
Copy link
Contributor

@davidwengier davidwengier commented Apr 2, 2024

We don't get much from using MS.Ext.Logging for our logging, and we pay for it with two three (?) DLL loads in VS. This fixes that by implementing the logging bits outselves, and removing the abstraction for options monitoring which we weren't really using.

I also removed the notion of "scopes" from the logging because we never actually implemented them, so this also closes #8232. We don't actually do any structured logging so we're really not missing much.

The only place that does still use MS.Ext.Logging is our one DLL that is referenced by Roslyn in VS Code, since they pass us an ILogger, so we don't really have a choice about that bit :)

I still need to do a little more testing of the options bit, because I'm seeing some oddities, but I think the issue is really one with the unified settings experience and/or our options updating code. Okay yeah, this is happening in VS main too. Logged #10200 but will make sure this doesn't get any worse :)

ie, no BeginScope, no EventIds. Message formatter could go in future too.
This is mostly mechanical fixing of namespace, plus some test mock changes
Copies from MS.Ext.Logging :) In future we should use a proper interpolated string handler
@davidwengier davidwengier requested a review from a team as a code owner April 2, 2024 06:05
@davidwengier davidwengier changed the title Remove MS.Ext.Logging from most of our repo Remove MS.Ext.Logging and Abstractions from most of our repo Apr 2, 2024
public bool IsEnabled(LogLevel logLevel)
{
return true;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
public void Log<TState>(LogLevel logLevel, TState state, Exception exception, Func<TState, Exception, string> formatter)
Copy link
Member

Choose a reason for hiding this comment

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

Can state be removed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is how the message gets passed currently. Will fix in a follow up PR.

Comment on lines +65 to +99
using var _ = StringBuilderPool.GetPooledObject(out var vsb);
var scanIndex = 0;
var endIndex = format.Length;

var count = 0;

while (scanIndex < endIndex)
{
var openBraceIndex = FindBraceIndex(format, '{', scanIndex, endIndex);
if (scanIndex == 0 && openBraceIndex == endIndex)
{
// No holes found.
return format;
}

var closeBraceIndex = FindBraceIndex(format, '}', openBraceIndex, endIndex);

if (closeBraceIndex == endIndex)
{
vsb.Append(format, scanIndex, endIndex - scanIndex);
scanIndex = endIndex;
}
else
{
// Format item syntax : { index[,alignment][ :formatString] }.
var formatDelimiterIndex = format.AsSpan(openBraceIndex, closeBraceIndex - openBraceIndex).IndexOfAny(',', ':');
formatDelimiterIndex = formatDelimiterIndex < 0 ? closeBraceIndex : formatDelimiterIndex + openBraceIndex;

vsb.Append(format, scanIndex, openBraceIndex - scanIndex + 1);
vsb.Append(count++);
vsb.Append(format, formatDelimiterIndex, closeBraceIndex - formatDelimiterIndex + 1);

scanIndex = closeBraceIndex + 1;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this? Do we want to keep using the MS.Ext.Logging string formatting? Or, would it be better to just use AppendFormat and add use interpolated strings (with InterpolatedStringHandler methods)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will follow up with an interpolated string handler in another PR.

}
}

return string.Format(vsb.ToString(), args);
Copy link
Member

Choose a reason for hiding this comment

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

It feels strange to call string.Format here, which will just use another StringBuilder to call AppendFormat.


internal interface ILogger
{
void Log<TState>(LogLevel logLevel, TState state, Exception? exception, Func<TState, Exception?, string> formatter);
Copy link
Member

Choose a reason for hiding this comment

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

Do we need all of the extreme flexibility this signature provides?

{
foreach (var logger in _loggers)
{
if (logger.IsEnabled(logLevel))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming this was always the case, but some of our loggers also check IsEnabled before logging. Should we remove that check there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do something about this in a follow up PR. With an interpolated string handler, hopefully the Log call can be avoided entirely in the right circumstances

Copy link
Contributor

@ryzngard ryzngard left a comment

Choose a reason for hiding this comment

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

Good work! Quickest to the draw :shipit:

@davidwengier
Copy link
Contributor Author

The answer to a bunch of the feedback is "I deliberately left the implementation unchanged to reduce the churn, and make the review easier", so I can either follow up in this PR, or another. I've always thought the log method has a weird signature :)

@davidwengier
Copy link
Contributor Author

Will do the renames in this PR, since it already touches so many files, and will follow up with a nicer API and use of an interpolated string handler in a future PR.

@davidwengier davidwengier merged commit 8c34fdd into dotnet:main Apr 2, 2024
12 checks passed
@davidwengier davidwengier deleted the RemoveMSExtLogging branch April 2, 2024 23:00
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Apr 2, 2024
@Cosifne Cosifne modified the milestones: Next, 17.11 P2 May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do something useful with Logging Scopes
4 participants