dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.2k stars 1.35k forks source link

Can't attach custom build logger to Visual Studio for MSBuild #4077

Open samsnowy opened 5 years ago

samsnowy commented 5 years ago

We've created a custom build logger and attached it to the build with the "-logger" switch and it worked great for command line builds. (Docs were awesome: https://docs.microsoft.com/en-us/visualstudio/msbuild/build-loggers?view=vs-2017)

However, we're can't find a way to attach the custom logger for Visual Studio builds which also uses MSBuild. The only method mentioned online is a hack that overrides the Build target: https://stackoverflow.com/questions/28733875/can-a-msbuild-custom-logger-be-specified-within-a-project-file/28806181#28806181

I was wondering if there is any changes in VS 2017 or MSBuild that allows custom build loggers to be attached to VS builds. If there isn't any, are there any plans to enable them for VS in the future?

rebnridgway commented 5 years ago

I have this problem too. I created a ticket in the Visual Studio tracker but haven't header much: https://developercommunity.visualstudio.com/content/problem/478625/i-cant-use-a-custom-msbuild-logger-from-visual-stu.html

rainersigwald commented 5 years ago

There's still no way to do this built into VS. The Project System Tools extension allows you to capture binary logs of builds in VS, which can then be replayed throuh your custom logger (but doesn't allow hooking a custom logger directly in AFAIK).

One problem with trying to do this is that there are many subsystems of VS that can trigger a build: a project is owned by a "project system" which may be independently implemented, or use a library to abstract away some details. In order to intercept all calls to MSBuild, you'd have to plug into every project system (C#/VB/F#, the older C# implementation csproj.dll, C++, etc.). That's not easy, so I don't forsee this coming as a general VS feature.

rebnridgway commented 5 years ago

I have discovered through experimentation that MSBuild opens the MSBuild.rsp file (but not the Directory.Build.rsp file) when it starts a build from Visual Studio but doesn't seem to take any arguments from there. Could that mechanism be used for this, assuming it can be made to work?

rainersigwald commented 5 years ago

No, I wouldn't expect that to work. I'm surprised we're even touching the file; that seems like a bug to me.

rebnridgway commented 5 years ago

This would be an incredibly useful feature for me, even if it was only available for C++ projects. It is 95% working, would it really be so much work to make it usable from within VS?

I'd be happy with being able to register a logger for VS builds using a VS extension. If MSBuild could take loggers from ProjectCollection.GlobalProjectCollection.Loggers, that would solve the problem. @samsnowy What is your use-case? Which project system would you need integration with?

samsnowy commented 5 years ago

@rebnridgway, we want to get some telemetry from local developers who uses VS as their build system instead of command line. Stuff like most commonly built project/target, build time, etc.

One way forward where I am experimenting with is the same way that project system tools is doing: Basically I'm creating a custom VS extension with a class that implements IVsBuildLoggerProvider. Then we export this interface and the VS build will pick this up as a logger.

Edit: We only care about C# projects, which probably made our use case easier.

edgarrs commented 5 years ago

@samsnowy , have your tried creating logger implementing IBuildLoggerProviderAsync? Take a look at this extension: https://github.com/dotnet/project-system-tools

mpm-os commented 3 years ago

Any update on this issue? This seems important to gather telemetry on build issues. I'm interested in getting data for, local build failures, time to build, etc.

rebnridgway commented 3 years ago

As far as I can tell Microsoft have no interest in adding this feature. I hacked around it by creating an MSBuild target that runs as early as possible and calls a custom task to attach a custom logger, it's not perfect because you miss the opening milliseconds and first few lines of output but it was good enough for my purposes. I'm just praying Microsoft don't 'fix' my workaround :)

mpm-os commented 3 years ago

Can you elaborate on how you attached the custom logger? How to you configure the custom logger? I can't seem to have access to the buildengine api to register it