Writing trace information in a windows form app

两盒软妹~` 提交于 2019-12-13 11:37:36

问题


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?


回答1:


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).




回答2:


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).




回答3:


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.




回答4:


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.




回答5:


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.

:)



来源:https://stackoverflow.com/questions/213451/writing-trace-information-in-a-windows-form-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!