This article reproduced: http://blog.csdn.net/oyi319/article/details/5753311
2.WinForm Program and Console window console
If you debug a SharpDevelop source program, you will see that it appears in debug mode with a console window to display the log information. Maybe I'm using a different approach, but you can try it out and write our own debug log code.
The first issue to solve is how to display the console window in debug mode. I am sure that this is a WinForm project and has not changed its output type. We need to use some API functions at the entry point of the project to display the console:
They are allocconsole and freeconsole.
- [DllImport ("kernel32.dll")]
- Public static extern Boolean allocconsole ();
- [DllImport ("kernel32.dll")]
- Public static extern Boolean freeconsole ();
We then make it determine the debug compile tag at the beginning of main (), call the AllocConsole method to display the console, and then, at the end of Main (), judge the debug compile tag and call the Freeconsole method to close the console. In this way, we can use Console.Write and other methods to display debug information in this console window.
To achieve better results, we write a shell class that encapsulates the Console.WriteLine method and outputs the personalization information. I do this, according to the output to the console text in the first few words to be judged as "warning", "error", "note" When the output with yellow, red, green text, the other output information output console default gray text, in order to differentiate the effect, but also before each message with the output of the time of the information.
This is the Shell class:
- <summary>
- Interacting with the console
- </summary>
- Static class Shell
- {
- // <summary>
- /// Output Information
- // </summary>
- /// <param name= "format" ></param>
- /// <param name= "args" ></param>
- public static void WriteLine (string format, params object[] args)
- {
- WriteLine (string. Format (format, args));
- }
- // <summary>
- /// Output Information
- // </summary>
- /// <param name= "Output" ></param>
- public static void WriteLine (string output)
- {
- Console.foregroundcolor = Getconsolecolor (output);
- Console.WriteLine (@"[{0}]{1}", Datetimeoffset.now, Output);
- }
- // <summary>
- /// Select console text color based on output text
- // </summary>
- /// <param name= "Output" ></param>
- // <returns></returns>
- private static Consolecolor getconsolecolor (string output)
- {
- if (output. StartsWith ("Warning")) return consolecolor.yellow;
- if (output. StartsWith ("error")) return consolecolor.red;
- if (output. StartsWith ("note")) return consolecolor.green;
- return consolecolor.gray;
- }
- }
Then the program entry function main code is as follows:
- <summary>
- The main entry point for the application.
- </summary>
- [STAThread]
- static void Main ()
- {
- #if DEBUG
- AllocConsole ();
- Shell.writeline ("NOTE: Start program ...");
- Shell.writeline ("/twritten by Oyi319");
- Shell.writeline ("/tblog:http://blog.csdn.com/oyi319");
- Shell.writeline ("{0}:{1}", " Warning", " this is a warning message. ");
- Shell.writeline ("{0}:{1}", " error", " this is an error message!") ");
- Shell.writeline ("{0}:{1}", "note", "This is a required attention message. ");
- Shell.writeline ("");
- #endif
- Application.enablevisualstyles ();
- Application.setcompatibletextrenderingdefault (false);
- Application.Run (new Form1 ());
- #if DEBUG
- Shell.writeline ("Note: 2 seconds to close ...");
- Thread.Sleep (2000);
- Freeconsole ();
- #endif
- }
Now this console window will only be displayed in debug mode, and will not appear at release compile time. Is this the debug method you want?
WinForm Program start Console window