Ami*_*nen 5 .net vb.net msbuild visual-studio-2010
我正在尝试将现有的命令行工具转换为 msbuild 自定义任务。该工具使用 System.Console 类记录消息和错误。
我已将一个继承自 Microsoft.Build.Utilities.Task 的类添加到该工具的程序集中,并调用该工具的主函数,并且自定义工具工作正常 - 但没有显示消息/错误(在 Visual Studio 输出窗口上)。
我想避免更改原始工具的代码(否则我可以将每个“Console.Error.WriteLine”更改为“Log.LogError”)。我想在调用工具的 main 函数之前通过调用 Console.SetOut 和 SetError 来更改控制台的 stdout 和 stderr 流。为此,我需要实现一个继承自 TextWriter 的类。
所以我的问题是:
谢谢。
Public Class TaskLogger
Inherits TextWriter
Public Enum TaskLoggerType
out
err
End Enum
Private m_logger As TaskLoggingHelper
Private m_logger_type As TaskLoggerType
Sub New(ByRef logger As TaskLoggingHelper, ByVal type As TaskLoggerType)
m_logger = logger
m_logger_type = type
End Sub
Public Overrides ReadOnly Property Encoding As System.Text.Encoding
Get
Return System.Text.Encoding.Default
End Get
End Property
Public Overrides Sub WriteLine(ByVal value As String)
Select Case m_logger_type
Case TaskLoggerType.out
m_logger.LogMessage(value)
Case TaskLoggerType.err
m_logger.LogError(value)
End Select
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
...
Public Overrides Function Execute() As Boolean
Dim oldOut As TextWriter = Console.Out
Dim oldErr As TextWriter = Console.Error
Dim newOut As TextWriter = New TaskLogger(Log, TaskLogger.TaskLoggerType.out)
Dim newErr As TextWriter = New TaskLogger(Log, TaskLogger.TaskLoggerType.err)
Console.SetOut(newOut)
Console.SetError(newErr)
Dim result As Boolean = Run(...)
Console.SetOut(oldOut)
Console.SetOut(oldErr)
Return result
End Function
Run Code Online (Sandbox Code Playgroud)