This is not too hard to implement.
First, implement custom logger. Use helper class
Logger
as a base; it will implement
ILogger
interface for you.
public class UiControlLogger : Logger {
public UiControlLogger(ListBox logContainer) {}
public override void Initialize(IEventSource eventSource) {}
}
For a starting point in its implementation, use
this Microsoft codelet.
In this codelet, locate this method:
private void WriteLine(string line, BuildEventArgs e)
}
Modify implementation of this method: write line in your control's content instead of stream, review whole logger implementation and modify accordingly.
Now you have a specialized custom logger. Use it in your build method using Build Engine:
Engine engine = new Engine();
engine.BinPath = myFrameworkPath;
FileLogger fileLogger = new FileLogger();
fileLogger.Parameters = logFileName;
engine.RegisterLogger(fileLogger);
engine.RegisterLogger(new UiControlLogger(myLogContainer));
bool success = engine.BuildProjectFile(myProject);
engine.UnregisterAllLoggers();
Optionally, you may want to implement just one universal logger to cover all your logging needs at once, using parameters, representing data of all your data sinks (where you re-direct your text strings via WriteLine implementation), all optional.
A note on the UI control: I don't know what control do you want to use for showing the log. I would not recommend text box, neither plain nor RTF (especially with
System.Windows.Forms
where the text box are not even designed for Append). Better use list box or more advanced item/node based control, such as
TreeView
(where you can classify lines by types (warning, error, etc.), source files, etc., placing the items under different parent nodes. With WPF, you have a lot more flexible options for your design.
Ultimately, you can implement much more advance features similar to that of Visual Studio. Look at the Microsoft codelet explaining logger implementation again. It implemets separate event handles for errors, warnings, etc:
eventSource_ErrorRaised
,
eventSource_WarningRaised
, etc. You can discriminate event handle behavior depending on even types and parameters via event arguments: you can display them in different color, re-direct strings to different UI controls, attach different context menus (implementing features like "Go to source"), etc.
(I'm just curious: are you creating yet another development studio? Some open-source projects are already available... :) )
Ok, will it help you? Please report if you try and have any problems and need more help.
Thank you for attention.