Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
i have a application which takes the .csproj file and builds it using the Msbuild Engine that .NET provides.
http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.engine.buildprojectfile.aspx
have the code from here.
what i want now is that whatever build process that is logged in the log file needs to be shown in the text box on the fly when the building is in process.(ie the text box should behave like the console window)
(when we build using the MSVisual Studio command prompt then on the console build messages are shown)
can some one help or throw some info on this

Thanks & Regards
Sandeep
Posted
Updated 12-Oct-10 2:59am
v2

1 solution

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.

C#
public class UiControlLogger : Logger {

   public UiControlLogger(ListBox logContainer) {/*..*/}
   //or whatever control you may need to show
   //log content in your UI;
   //just remember this object

   public override void Initialize(IEventSource eventSource) {/*...*/}

   //...
}


For a starting point in its implementation, use this Microsoft codelet.

In this codelet, locate this method:

C#
private void WriteLine(string line, BuildEventArgs e)
    //control-based implementation...
}


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:

C#
Engine engine = new Engine();
engine.BinPath = myFrameworkPath;

//Do you still need logging to file?
//You may or may not, because you can always implement "Save Log As..."
//with text file generated out of your UI control content.
//If you still need the file, do this:
FileLogger fileLogger = new FileLogger();
fileLogger.Parameters = logFileName;
engine.RegisterLogger(fileLogger);

//now register your specialized UI logger:
engine.RegisterLogger(new UiControlLogger(myLogContainer));

//call the engine's Build method you want, for example:
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.
 
Share this answer
 
v15
Comments
Espen Harlinn 26-Feb-11 10:53am    
Amazing effort - my 5
Sergey Alexandrovich Kryukov 26-Feb-11 20:20pm    
Thank you very much, Espen.
This is about custom logger. I think this small not the most difficult aspect or something else caused OP to give up whole project. Indeed, it looks a bit too ambitions, something like yes another Visual Studio, I would assume...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900