Repository navigation
push improved MSBuildLogger to a separate project in case we want to spin it off - #49751
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR extracts an improved MSBuild logging component into a separate project to potentially spin it off as a standalone package. The main enhancement is the ability to use properties from Microsoft.Extensions.Logging.ILogger messages as inputs to MSBuild's logging parameters (code, file, line, etc.), while preserving state information for binlog and other MSBuild loggers.
- Moves MSBuild logging functionality from
Microsoft.NET.Build.Containersto a newMicrosoft.Extensions.Logging.MSBuildproject - Enhances the logger to support extracting MSBuild-specific parameters from logging state/scope
- Updates namespace and visibility from internal to public for the extracted components
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/Microsoft.Extensions.Logging.MSBuild/Microsoft.Extensions.Logging.MSBuild.csproj |
New project file defining the extracted MSBuild logging library |
src/Microsoft.Extensions.Logging.MSBuild/MSBuildLoggerProvider.cs |
Updated provider with scope support and public visibility |
src/Microsoft.Extensions.Logging.MSBuild/MSBuildLogger.cs |
Enhanced logger with parameter extraction from state/scope |
src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.cs |
Updated namespace import to use new project |
src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateImageIndex.cs |
Updated namespace import to use new project |
src/Containers/Microsoft.NET.Build.Containers/Microsoft.NET.Build.Containers.csproj |
Added project reference to new logging library |
src/Containers/Microsoft.NET.Build.Containers/Logging/MSBuildLogger.cs |
Removed old implementation |
Comments suppressed due to low confidence (1)
| continue; | ||
| default: | ||
| var wrappedKey = "{" + kvp.Key + "}"; | ||
| if (originalFormat.Contains(wrappedKey)) |
There was a problem hiding this comment.
Potential null reference exception when originalFormat is null. The variable is declared as 'null!' but can remain null if no {OriginalFormat} key is found, causing a null reference when calling Contains().
| if (originalFormat.Contains(wrappedKey)) | |
| if (originalFormat != null && originalFormat.Contains(wrappedKey)) |
There was a problem hiding this comment.
originalFormat is also null if "{OriginalFormat}" is a key in the list but the foreach (var kvp in stateItems) loop has not reached it yet. There is no documented requirement that "{OriginalFormat}" be in the first element of the list.
| continue; | ||
| default: | ||
| var wrappedKey = "{" + kvp.Key + "}"; | ||
| if (originalFormat.Contains(wrappedKey)) |
There was a problem hiding this comment.
Potential null reference exception when originalFormat is null. This is the same issue as line 140 - originalFormat can be null when no {OriginalFormat} key is found in the state items.
| if (originalFormat.Contains(wrappedKey)) | |
| if (originalFormat != null && originalFormat.Contains(wrappedKey)) |
fae2e39 to
395a0e5
Compare
| var wrappedKey = "{" + kvp.Key + "}"; | ||
| if (originalFormat.Contains(wrappedKey)) | ||
| { | ||
| // If the key is part of the format string of the original format, we don't need to append it again. | ||
| continue; | ||
| } |
There was a problem hiding this comment.
If the message template includes formatting like {CreationDate:O}, this won't match.
There was a problem hiding this comment.
Totally agree - I should possibly look at interpreting this via CompositeFormat.
There was a problem hiding this comment.
The CompositeFormat.Parse method does not support named parameter placeholders like log message templates have. The CompositeFormat class does not have public members for examining the structure of the format string. I don't think CompositeFormat is useful here.
There was a problem hiding this comment.
I was hoping it would be slightly more capable. In the past I've used MessageTemplates for this kind of thing, but there would be source-build concerns with using an external library. Might have to punt for v1 of this - at least if it stays in the SDK repo directly.
| continue; | ||
| default: | ||
| var wrappedKey = "{" + kvp.Key + "}"; | ||
| if (originalFormat.Contains(wrappedKey)) |
There was a problem hiding this comment.
originalFormat is also null if "{OriginalFormat}" is a key in the list but the foreach (var kvp in stateItems) loop has not reached it yet. There is no documented requirement that "{OriginalFormat}" be in the first element of the list.
|
How do you create the state objects that are passed to ILogger.Log<TState> and then analysed by this logger? Methods like LoggerExtensions.LogInformation and LoggerMessage.Define parse the log message template to learn the names of attributes, so if you use those and want to give e.g. "File" to MSBuild, then you have to include a "{File}" placeholder in the template. But then the formatted log message also includes the file name, and MSBuild adds the file name again, so the user sees it twice. Is source-generated logging different in this respect? |
The state objects are created via |
Oh so if you don't want to include an attribute in the formatted log message, then you place it in the state of the logger scope, not in the state of the log entry.
That one doesn't seem to have any of the attributes that MSBuildLogger would recognise. |
Correct - I didn't really make use of the MSBuild-specific attributes in that PR. They just kind of emerged from the fact that I could now walk state/scope in a structured way and do something with the attributes I found. We have this problem in the SDK codebase more generally when we log in our Tasks where we have to call special "LogXFromFormat" methods because those know how to extract specific kinds of data from the shapes of our localized strings - "wouldn't it be nice if that was done in a more 'structured' way" was basically the genesis of this entire PR. |
53678d6 to
7fe2868
Compare
nagilson
left a comment
There was a problem hiding this comment.
Ideally we would have review from MS Build as well, but the changes LGTM
|
Thank you @KalleOlaviNiemitalo for providing such great feedback. |
| return message; | ||
| } | ||
|
|
||
| private static void ProcessScope(object? scope, ref MSBuildMessageParameters message, ref string? originalFormat, List<KeyValuePair<string, object?>>? unprocessedKeyValues) |
There was a problem hiding this comment.
unprocessedKeyValues should be passed by ref here because it may be null on entry and ProcessScope can then allocate a list for it but the caller never gets that list.
This is part of reducing the diff in #49556.
During that PR, I implemented a number of improvements to the simple Microsoft.Extensions.Logging.Abstractions.ILogger wrapper we had that ties that ILogger to MSBuild's ILogger interfaces.
The main feature is being able to use state/props from MEL.ILogger messages as inputs to MSbuild's various parameters on the
LogXoverloads - things like code, file, line, etc can now all be set by props. State is also persisted and attached to messages so ambient context is available in the binlog and other MSBuild Loggers even if not directly used in the Message.