Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to show output during update-database?

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?

like image 864
Charles W Avatar asked Mar 05 '14 19:03

Charles W


1 Answers

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?

like image 123
Yair Nevet Avatar answered Sep 30 '22 13:09

Yair Nevet