I'm running a fairly large seed as a stress test, and I'm wondering if it's possible to show output to a console.
I'd like to show the remaining entries or percent complete, or anything really.
Is there a way to write to the console or package manager console during update-database?
Is there a way to write to the console or package manager console during update-database?
I will describe two options which are available for you out of the box:
1. Console.WriteLine
Solution:
Either if you're running your code from a Console application or from a Library of any other application type (e.g. asp.net), you can use the Console.WriteLine
function, but the main question is if there is a listener to the Console
output.
If nothing is listening to the Console, then take advantage of the Win32.AllocConsole()
Windows APIs function in order to open a listener:
using System.Runtime.InteropServices;
[DllImport("kernel32")]
static extern bool AllocConsole();
Usage:
AllocConsole();
//now you can call: Console.WriteLine();
Console.WriteLine("Total number of records left to seed:{0}", total);
Consider to wrap the above code with the following directive in order to avoid of painful surprises in the production environment:
#if DEBUG
#endif
Explanation:
AllocConsole initializes standard input, standard output, and standard error handles for the new console. The standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles to the console's screen buffer. To retrieve these handles, use the GetStdHandle function.
2. Debug.Print
Solution:
Use the Debug.Print
function on debug mode. Using it, you would be able to write whatever you want to the DEBUG output window of Visual Studio:
Debug -> Windows -> Output -> Show output from -> Debug
By the way, did you consider logging this information using NLog or LOG4NET to some text file, for instance?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With