You are provided with a program (ProgramMissingTracing.zip) which read a tracelevel and a name (really just a string) from the console - window
'Refactor' the program to make use of simple Trace.
After reading the name (linie 22) insert Trace.Assert("peter".Equals(name)) try the program by typing "peter" and something else.
extra: to the list of parameters you can add one or two string(s) - what happen then?
extra2: try the method Fail on the Trace class (e.g. Trace.Fail("peter"))
'Refactor' the program to make use of the stadard or normal use of Trace.
In first line add a ConsoleTraceListener to the Trace i.e. Trace.Listeners.Add(new ConsoleTraceListener())
When ever you have a Console.WriteLine change it into Trace.TraceInformation (or Trace.TraceError, Trace.TraceWarning) - what happen (yes when you run the program) ?
'Refactor' the program so your Trace have two TraceListeners.
Add another TraceListener to the Trace i.e. Trace.Listeners.Add(new TextWriterTraceListener("c:\\temp\\xxxx.log"))
What happen now when you run the program ? - look in the folder 'c:\temp\'
How many Listeners can you have added to the Trace?
extra: try to add a second consoleTraceListener
Make a new class e.g. 'MyTraceListener' which Inherit from TraceListener.
Implement the abstract methods (Write and WriteLine) e.g. by prefix the trace-message with "my trace:" + message
Add your TraceListener to the Trace
Rub the program - does it print out?
When adding a Filter you can decide 'if' the message should be traced.
You have only two prdefinde filters (EventTypeFilter - filters on trace levels and SourceFilter - filters on the name of a source object)
- So often you need to make it your self.
Make a new class e.g. 'MyFilter' which Inherit from TraceFilter.
Implement the abstract method (ShouldTrace) e.g. if the message (the parameter 'formatOrMessage') include "hi" then return true otherwise false.
Add an object of this filter to one of the Listener (e.g. the ConsoleTraceListener).
Run the program to trace a message with and without "hi".
extra: can you change filter during runtime?
When tracing you can trace at different level. The levels are from high to low (Error, Warning, Info)
You can use these levels in filters or directly in the Trace class
When you write through Trace you can use these levels to control the amount of trace you will get out.
You can Trace.WriteLineIf( -- condition --, message)
If you are using levels - use the Trace.TraceInformation etc. (see trace methods)
Then you set a filter use the EventTraceFilter to set the level to one of these values Verbose(all), information, warnings, errors or off e.g means warning all warnings and errors are traced but error only errors are traced.
On windows computers you have a system log file (Event log - open from control panel)
You have allso possibilities to trace your information into this event.log by using EventLogTraceLIsteners
BUT : Two remarks 1) You need to run as administrator when you create the log-'file' 2) you have to give the log file a Name
Try this and open the eventLog (look up your file under 'logfiles for programs ..' -> the name of your log-file)