我修改了一个 C# 插件,它是 Visual Studio 2.0 的 Python 工具的一部分,并希望查看代码中 Debug.WriteLine 和 Trace.WriteLine 语句的输出。请注意,该插件用于 Visual Studio 2013 本身,修改 python 进程的调试....
我希望我可以按照 pminaev on the PTVS discussion forum here 的建议在 devenv.exe.config
中添加一个跟踪监听器(将另一个 VS 附加到 VS 本身似乎很笨拙,所以我希望避免这种情况)。
我认为适用于其他 .NET 应用程序的正确配置似乎不适用于 VS2013 本身。 我的努力记录如下。
如果有人设法将插件调试/跟踪输出重定向到 Visual Studio 控制台(或其他任何比另一个附加的 VS 实例更方便的地方),非常感谢提示......
我的 Google-fu 出现了几个示例,基于这些示例,我尝试添加...
<system.diagnostics>
<trace autoflush="true" />
<listeners>
<add name="myConsoleTraceListener" type="System.Diagnostics.ConsoleTraceListener" />
</listeners>
</trace>
</system.diagnostics>
...和...
<system.diagnostics>
<source name="System.ServiceModel.MessageLogging" switchValue="Verbose, ActivityTracing">
<listeners>
<add name="myTraceListener" />
</listeners>
</source>
<sharedListeners>
<add name="myTraceListener" type="System.Diagnostics.ConsoleTraceListener" />
</sharedListeners>
</system.diagnostics>
之后启动 PTVS,没有任何用处。使用任一配置,我都会弹出一个窗口:
The `Python Tools Package' package id not load correctly.
The problem may have been caused by a configuration change or by
the installation of another extension. You can get more information by
examining the file
'C:\Users\XXX\AppData\Roaming\Microsoft\VisualStudio\12.0\Acti
vityLog.xml'.
ActivityLog 对我来说意义不大:
<entry>
<record>41</record>
<time>2014/05/12 07:52:47.851</time>
<type>Error</type>
<source>VisualStudio</source>
<description>CreateInstance failed for package [Python Tools Package]</description>
<guid>{6DBD7C1E-1F1B-496D-AC7C-C55DAE66C783}</guid>
<hr>80131604</hr>
<errorinfo>Exception has been thrown by the target of an invocation.</errorinfo>
</entry>
<entry>
<record>42</record>
<time>2014/05/12 07:52:47.851</time>
<type>Error</type>
<source>VisualStudio</source>
<description>End package load [Python Tools Package]</description>
<guid>{6DBD7C1E-1F1B-496D-AC7C-C55DAE66C783}</guid>
<hr>80004005 - E_FAIL</hr>
<errorinfo>Exception has been thrown by the target of an invocation.</errorinfo>
</entry>
当我尝试附加到远程 python 进程时,我得到另一个弹出窗口:
Unable to connect to 'secret@server'. Operation not supported.
Unknown error: 0x80131902.
Google-foo 建议这是加载 .NET 版本的一些失败......?
最佳答案
我想为我写的插件做类似的事情: pMixins 。我使用 log4net 而不是 System.Diagnostics,但想将日志消息路由到 Visual Studio 输出窗口。
我从来没有得到基于配置的工作方法,最终以编程方式进行接线。此外,我必须创建一个自定义日志编写器来写入 Visual Studio 输出窗口。
在我的包文件( https://github.com/ppittle/pMixins/blob/master/pMixins.VSPackage/pMixinsVisualStudioCodeGenerateInitializer.cs )中:
protected override void Initialize()
{
//Create a Visual Studio Writer
_visualStudioWriter = new VisualStudioWriter(dte, this);
//Initialize Logging
Log4NetInitializer.Initialize(_visualStudioWriter, this);
}
VisualStudioWriter(摘要)( https://github.com/ppittle/pMixins/blob/master/pMixins.VSPackage/Infrastructure/VisualStudioWriter.cs ):
public class VisualStudioWriter : IVisualStudioWriter
{
private EnvDTE.OutputWindowPane _outputWindowPane;
public VisualStudioWriter(DTE dte, System.IServiceProvider serviceProvider)
{
_outputWindowPane = LoadOutputWindowPane(dte);
}
private EnvDTE.OutputWindowPane LoadOutputWindowPane(DTE dte)
{
const string windowName = "pMixins Code Generator";
EnvDTE.OutputWindowPane pane = null;
EnvDTE.Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
if (window != null)
{
EnvDTE.OutputWindow output = window.Object as EnvDTE.OutputWindow;
if (output != null)
{
pane = output.ActivePane;
if (pane == null || pane.Name != windowName)
{
for (int ix = output.OutputWindowPanes.Count; ix > 0; ix--)
{
pane = output.OutputWindowPanes.Item(ix);
if (pane.Name == windowName)
break;
}
if (pane == null || pane.Name != windowName)
pane = output.OutputWindowPanes.Add(windowName);
if (pane != null)
pane.Activate();
}
}
}
return pane;
}
public void OutputString(string s)
{
_outputWindowPane.OutputString(s);
}
}
Log4NetInitializer(摘要)https://github.com/ppittle/pMixins/blob/master/CopaceticSoftware.CodeGenerator.StarterKit/Logging/Log4NetInitializer.cs
public static class Log4NetInitializer
{
public static void Initialize(IVisualStudioWriter visualStudioWriter,
IServiceProvider serviceProvider)
{
//http://stackoverflow.com/questions/650694/changing-the-log-level-programmaticaly-in-log4net
var outputWindowAppender =
new VisualStudioOutputWindowAppender(visualStudioWriter)
{
Layout =
new PatternLayout(@"%date{HH:mm:ss,fff} %thread% %-5level [%logger{2}] %message%newline"),
Threshold =
#if DEBUG
Level.Debug
#else
Level.Info
#endif
};
log4net.Config.BasicConfigurator.Configure(
outputWindowAppender);
}
}
最后,VisualStudioOutputWindowAppender ( https://github.com/ppittle/pMixins/blob/master/CopaceticSoftware.CodeGenerator.StarterKit/Logging/VisualStudioOutputWindowAppender.cs )
public class VisualStudioOutputWindowAppender : AppenderSkeleton
{
public IVisualStudioWriter OutputWindow { get; set; }
public VisualStudioOutputWindowAppender(IVisualStudioWriter outputWindow)
{
OutputWindow = outputWindow;
Layout = new PatternLayout("%-5level %logger - %message%newline");
}
protected override void Append(LoggingEvent loggingEvent)
{
if (null == OutputWindow)
return;
if (null == loggingEvent)
return;
OutputWindow.OutputString(RenderLoggingEvent(loggingEvent));
}
}
希望有帮助
关于c# - Debug|Trace.WriteLine 来自 Visual Studio 中的 C# 插件 - 通过 ConsoleTraceListener 显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23603914/