Writing trace information in a windows form app

后端 未结 5 752
小鲜肉
小鲜肉 2021-02-11 08:39

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 t

相关标签:
5条回答
  • 2021-02-11 09:10

    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.

    0 讨论(0)
  • 2021-02-11 09:10

    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.

    :)

    0 讨论(0)
  • 2021-02-11 09:12

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

    0 讨论(0)
  • 2021-02-11 09:19

    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.

    0 讨论(0)
  • 2021-02-11 09:34

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

    0 讨论(0)
提交回复
热议问题