How to write in Visual Studio output window in My custom tool?

I am writing a custom tool, and currently I am doing what I want as much as possible. I would like to be able to write in Visual Studio if something goes wrong. (Incorrectly formatted code or something else).

Are there any standards for this? Right now I can mostly make the tool fail, and Visual Studio warns that this is the case. I would like to get a category in the output window with any resulting messages that I want to send. I could also live with a more descriptive task / warning in the error list window.

+25
visual-studio vsx
Jul 07 '09 at 19:36
source share
5 answers

Output window

To write to the "General" output window in Visual Studio, you need to do the following:

IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow; Guid generalPaneGuid = VSConstants.GUID_OutWindowGeneralPane; // PS There also the GUID_OutWindowDebugPane available. IVsOutputWindowPane generalPane; outWindow.GetPane( ref generalPaneGuid , out generalPane ); generalPane.OutputString( "Hello World!" ); generalPane.Activate(); // Brings this pane into view 

If you want to write to a custom window, this is what you need to do:

 IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow; // Use eg Tools -> Create GUID to make a stable, but unique GUID for your pane. // Also, in a real project, this should probably be a static constant, and not a local variable Guid customGuid = new Guid("0F44E2D1-F5FA-4d2d-AB30-22BE8ECD9789"); string customTitle = "Custom Window Title"; outWindow.CreatePane( ref customGuid, customTitle, 1, 1 ); IVsOutputWindowPane customPane; outWindow.GetPane( ref customGuid, out customPane); customPane.OutputString( "Hello, Custom World!" ); customPane.Activate(); // Brings this pane into view 

Details on IVsOutputWindow and IVsOutputWindowPane can be found on MSDN.

Error list

To add items to the error list, IVsSingleFileGenerator has a call to the void Generate(...) method, which has a parameter of type IVsGeneratorProgress . This interface has a void GeneratorError() method that allows you to report errors and warnings to the Visual Studio error list.

 public class MyCodeGenerator : IVsSingleFileGenerator { ... public void Generate( string inputFilePath, string inputFileContents, string defaultNamespace, out IntPtr outputFileContents, out int output, IVsGeneratorProgress generateProgress ) { ... generateProgress.GeneratorError( false, 0, "An error occured", 2, 4); ... } ... } 

Details of GeneratorError () can be found on MSDN.

+42
Dec 05 '09 at 15:53
source share

There is another way to use Marshal.GetActiveObject to capture the current DTE2 instance.

The first link is EnvDTE and envdte80. This currently works in VisualStudio 2012, I have not tried others yet.

 using System; using System.Runtime.InteropServices; using EnvDTE; using EnvDTE80; internal class VsOutputLogger { private static Lazy<Action<string>> _Logger = new Lazy<Action<string>>( () => GetWindow().OutputString ); private static Action<string> Logger { get { return _Logger.Value; } } public static void SetLogger( Action<string> logger ) { _Logger = new Lazy<Action<string>>( () => logger ); } public static void Write( string format, params object[] args) { var message = string.Format( format, args ); Write( message ); } public static void Write( string message ) { Logger( message + Environment.NewLine ); } private static OutputWindowPane GetWindow() { var dte = (DTE2) Marshal.GetActiveObject( "VisualStudio.DTE" ); return dte.ToolWindows.OutputWindow.ActivePane; } } 
+7
Jun 10 2018-12-12T00:
source share

If you want something to appear in the output window, it must come from stdout. To do this, your application must be connected as a "console" application. Set the / SUBSYSTEM: CONSOLE flag on the project properties page, in Linker / System, set the SubSystem property to CONSOLE.

As soon as you see the output in the window, if you include the text “Error:”, it will appear as an error or if you set “Warning:”, it will appear as a warning. If the error text starts with the path / file name followed by the line number in brackets, the IDE recognizes it as a “clickable” error and automatically moves along the fault line.

+4
Jul 07 '09 at 21:46
source share

You can use the Debug and / or Trace classes. There is information here: http://msdn.microsoft.com/en-us/library/bs4c1wda(VS.71).aspx

Good luck.

0
Jul 07 '09 at 19:42
source share

use System.Diagnostics.Debugger.Message

-2
Apr 09 2018-12-12T00:
source share



All Articles