Log to output window when using T4

I'm using T4 for code generation. Its working well but one thing I'd like to do is some simple logging, I'm happy if this simply took the form of throwing messages into the Output window.

Does anyone know a way of doing this?

Alternatively, I know there exists Error() & Warning() methods for dumping stuff into the Error List pane, is there anything similar for dumping informational messages?

Answers


It's not exactly what you asked for but you can debug a T4 template from within visual studio, just right click "Debug T4 Template".

If you do this it's easy enough to keep the state you would be sending to the output window in a variable somewhere.


Based on Ondrej's answer, I wrote a handy function that I've been using to debug my templates:

private void WriteToOutput(string output)
{
  IServiceProvider hostServiceProvider = (IServiceProvider)Host;
  if (hostServiceProvider == null)
    throw new Exception("Host property returned unexpected value (null)");

  EnvDTE.DTE dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
  if (dte == null)
    throw new Exception("Unable to retrieve EnvDTE.DTE");

  var window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
  var outputWindow = (OutputWindow) window.Object;
  outputWindow.ActivePane.Activate();

  outputWindow.ActivePane.OutputString(output);
  outputWindow.ActivePane.OutputString("\n");
}

Need Your Help

Display loading/splash page in Flask

javascript html flask jinja2

I have a flask app that displays a single page of content, but it takes up to 20 seconds for the content to be generated. It seems to me one way to display a splash screen would be for the main URL