Reputation: 385
I am working on a C# Windows Application. It has tab control implemented. My requirement is to trace events such as a method entry/exit, program load, etc and transfer these values to a textBox (named as traceTextBox) in one of the tabs.
I will be happy if anyone can guide me as to where to start from.
I want to trace only events: button click and method entry and exit.
I want to trace events in my applications only.
I want only trace and not debug events.
Please note: I do not want to log to a text file or the console. I want the output to be updated in a textbox.
Upvotes: 1
Views: 372
Reputation: 38590
System.Diagnostics.Trace.WriteLine()
is your friend. You can then create a subclass of System.Diagnostics.TraceListener
and register it by adding it to System.Diagnostics.Trace.TraceListeners
so that your code receives notification of these events.
Note that the trace events will only be compiled into the code when the TRACE
conditional compilation flag is set (usually by adding it to the 'Conditional Compilation Symbols' box in each relevant project's settings).
public sealed class MyTraceListener : TraceListener
{
public MyTraceListener() : base("MyTraceListener")
{
}
public override void Write(string message)
{
//TODO code here to add to your UI element
//NOTE make sure you hand off to the UI thread e.g.
// using InvokeRequired/Invoke
}
}
In your application startup:
...
Trace.Listeners.Add(new MyTraceListener());
...
Writing traces:
public void SomeMethod()
{
Trace.WriteLine("Entered SomeMethod()");
//...
Trace.WriteLine("Exited SomeMethod()");
}
Upvotes: 2