views:

657

answers:

5

Hi,

I know how to write trace statements that I can view in a webforms environment, but how do I do this in a windows forms app?

I am inside of a static method, and I want to display the sql query that it is generating.

I don't have access to messagebox.show, what are my options?

A: 

You could use a global logging object:

enum LogLevel
{
    Info,
    Warning,
    Error
}

delegate void OnLog (string msg, LogLevel level);

interface ILogger
{
    void Log(string msg, LogLevel level);
    event OnLog;
}

Then extend ILogger with a class that you acquire using a public static method in the Program class.

And in your main form, attach yourself to the OnLog event and use it to print messages on the for itself. Then all you have to do is call the Log method in your static method with the SQL query.

:)

nlaq
A: 

Do you need to show it in your form? If not, you could just Trace.WriteLine() out the query and use DebugView to see it. I guess I need more info if that doesn't help.

itsmatt
+1  A: 

The System.Diagnostics.Trace class will write to trace listeners.

The default listener writes to the output window when debugging. You can specify other listeners in the application configuration file which can redirect trace output to a file, to the event log etc.

Alternatively use a logging framework such as Log4Net.

Joe
+3  A: 

The simplest way is to use either System.Diagnostics.Debug.WriteLine or System.Diagnostics.Trace.WriteLine. If you have a debugger attached, the messages will show up in the output window, otherwise run DebugView to see the messages (you'll need to play with the filtering some to exclude the noise).

Scott Dorman
+1  A: 

You can use Log4Net.

Log4Net is completely Xml configuration driven and provides a very high degree of extensibility (Just implement new Appenders, Filters or Layouts).

Daok
Yes.. absolutely: http://www.tigraine.at/2008/10/13/log4net-logging-made-easy/
Tigraine