Beruflich Dokumente
Kultur Dokumente
70-536 Chapitre 10
Plan
Instrumentation
Mohamed Romdhani
INSAT, Octobre 2007 2
M. Romdhani, Octobre 2007
To create an event log, the .NET Framework provides the EventLog class. The WriteEntry method looks rather simple.
To use it, the Source property needs to be specified and a message needs However, there are 10 overloads for it. As is the case with many overloaded
to be written, as shown in the following code, which requires the constructors, the minimal construction can be used and then you can set all
necessary properties. Or you can specify all the information you need in the
System.Diagnostics namespace: constructor.
public static void CreateEventLog() {
The following example shows how to use an overload to add an event ID
EventLog DemoLog = new EventLog("Chap10Demo");
public static void CreateEventLog() {
DemoLog.Source = "Chap10Demo"; EventLog DemoLog = new EventLog("Security");
DemoLog.WriteEntry("CreateEventLog called",EventLogEntryType.Information); DemoLog.Source = "Chap10Demo";
} DemoLog.WriteEntry("CreateEventLog called",EventLogEntryType.Information, 100);
} //100 is the EventID
After you create an EventLog object and specify its source (which, by the way, can all
be done in one of the overloaded constructors), information about the object should be In addition to reading custom logs, the EventLog object also gives developers the ability to read
visible from the Windows Event Viewer. from and write to built-in event logs. The built-in logs are the Application, Security, and System
logs. Even though you might have a specified log for your application, you might want to take
advantage of the built-in logs.
Deleting an event log is equally simple. You may want, for example, to delete the log Use the following code to write to the Security log (Remember that you must have permission to write) :
that you just created as part of this exercise. To remove the demonstration log, use
the Delete method of EventLog in code like the following: public static void WriteToSecurityEventLog() {
Public static void DeleteEventLog() { EventLog DemoLog = new EventLog("Security");
EventLog.Delete("Chap10Demo"); DemoLog.Source = "DemoApp";
} DemoLog.WriteEntry("A Sql Injection Attack just occurred from IP Address 100.000.00.0",
EventLogEntryType.Warning);
}
1
Chapitre 1- Introduction aux technologies J2EE
Writing Output
The Debugger class, which enables communication with a debugger When you use the Log method, it directs the output to whatever
application, is fairly straightforward. listener objects are attached to the debugger.
Methods Debugger Class Name To use the Log method, all you need to do is call it and indicate a level,
Break : Signals a break to the debugger. This is an excellent tool for category, and message.
conditionally stopping the execution of the application.
Trace.Listeners.Clear();
IsAttached : Indicates whether the debugger is attached to a process
already. DefaultTraceListener MyListener = new DefaultTraceListener();
IsLogging : Indicates whether the Debugger is currently logging. Trace.Listeners.Add(MyListener);
Debugger.Log(2, "Test", "This is a test");
Launch : Launches a debugger, and attaches it to a process.
Console.ReadLine();
Log : Posts a message to the current debugger.
Exemple:
All that we've done is cleared any existing Listeners from the Listeners collection,
String MyMessage = ReturnMessage(); added a DefaultTraceListener object, and then sent some output to it. There are
if (MyMessage == null) { multiple overloads for the DefaultTraceListener, and we could just as easily have
Debugger.Break(); directed the output to a text file, for instance. If the listener is implemented
correctly, you should see the text "This is a test" in the Output window.
}
This use of the Break method could
yield results similar to those shown
in figure
NOTE : For the Debugger or Debug
class to function, the build must be
performed in Debug mode.
2
Chapitre 1- Introduction aux technologies J2EE
3
Chapitre 1- Introduction aux technologies J2EE
Lesson 2 Summary
The Debug and Trace classes are two of the primary tools developers can use to
examine the execution of their applications while they run.
The Write, WriteLine, WriteIf, and WriteLineIf methods all can be used to send
output to attached listener objects.
The Assert method of the Debug and Trace classes is one mechanism to test a
condition of your code and receive feedback based on that evaluation.
Lesson 3 : Monitoring Performance
Listener objects are used as receptacles for Debug and Trace information.
The TraceSource class allows the developer to specify information about the
source of the trace information
The TraceSwitch allows the developer to manipulate virtually every aspect of the
TraceSource class.
4
Chapitre 1- Introduction aux technologies J2EE
5
Chapitre 1- Introduction aux technologies J2EE
Each of the previous items were parts that, when used together, help Many times there is a need to start external applications or processes from
provide a tremendous amount of information about within your application.
PerformanceCounter objects. There are two ways to start a process:
To illustrate what can be done with a PerformanceCounter, Here is a 1. Use without command-line arguments.
common scenario: examining how many applications are running on an 2. Specify command-line arguments.
ASP.NET process. There is a predefined category named "ASP.NET"
and a predefined counter named "Applications Running". Start Processes without Command-Line Arguments
StringBuilder Builder = new StringBuilder(); In many instances, you'll want to set many more properties than are provided in
Builder.Append("Counter Name:" + DemoCounter.CounterName + "\r\n"); the constructors. To accomplish this, the ProcessStartInfo class must be used.
Builder.Append("Counter Type:" + DemoCounter.CounterType.ToString() + "\r\n"); ProcessStartInfo Info = new ProcessStartInfo();
Builder.Append("Category Name:" + DemoCounter.CategoryName + "\r\n"); Info.FileName = this.tbProcessName.Text;
Builder.Append("Instance Name:" + DemoCounter.InstanceName+ "\r\n"); Process.Start(Info);
Builder.Append("Machine Name:" + DemoCounter.MachineName + "\r\n");
MessageBox.Show(Builder.ToString()); Start Processes with Command-Line Arguments
Specifying command-line arguments to start a process is similar to starting a process
without them.
ProcessStartInfo Info = new ProcessStartInfo();
Info.FileName = this.tbProcessName.Text;
Info.Arguments = "EACH OF THESE WORDS IS AN ARGUMENT " ;
Process.Start(Info);
Each time a method is called, a StackFrame is added and pushed onto The Start method of the Process class allows applications to be started
the stack. Each subsequent method is pushed onto the stack to be programmatically. The GetProcesses method of the Process class
executed in last-in, first-out order. When there are no more methods to returns information about all running processes on a machine.
be called, each method is executed and popped (removed) from the
stack.
To pass values into the constructor of the Main method, command-line
arguments can be specified.
Every time an exception is thrown by the .NET runtime, the Exception
object's current StackTrace property is set to the current StackTrace.
try {
The StackTrace object provides detailed information about the state of
execution for a given application. StackFrame objects make up the
Int32 x = 1;
StackTrace object. However, they are intended to support the .NET
x--; Framework and should not be used directly.
Int32 i = 10 / x;
} catch (DivideByZeroException Problem) {
PerformanceCounter objects are mechanisms that allow specific
Debug.Assert(false, Problem.StackTrace); measurement of an application's resource utilization.
}
After the Exception object is caught, the code intentionally causes a Debug
assertion to fail so that the StackTrace can be viewed.
6
Chapitre 1- Introduction aux technologies J2EE
The ManagementEventWatcher class allows for subscription information about WMI is a component of the Windows operating system that provides
events that occur within the WMI context. To use it, perform the following
steps: monitoring functionality that spans virtually every machine resource.
1. Create a new ManagementEventWatcher object.
2. Associate an EventQuery object with it. The EventQuery class is used within the .NET Framework to represent
3. Call the WaitForNextEvent method. a WMI query.
4. Stop the notifications.
The Win32_Service object can be used to query information about
Here is an example: Windows services.
public static void QueryServices() {
EventQuery DemoQuery = new EventQuery();
DemoQuery.QueryString = "SELECT * FROM __InstanceCreationEvent WITHIN 2 The ManagementQuery base class is used as a foundation for all
WHERE TargetInstance isa \"Win32_Service\" management query objects.
AND TargetInstance.State = 'Paused'";
The ManagementObjectSearcher is used to query system resources
ManagementEventWatcher DemoWatcher = through WMI.
new ManagementEventWatcher(DemoQuery);
7
Chapitre 1- Introduction aux technologies J2EE
Visual Studio 2005 provides many new attributes that will allow you to take
Correct Answer: 3 finely grained control over the debugger. Examples of these new attributes
1. Incorrect: Network Adapters can be discovered using WMI. include DebuggerVisualizerAttribute and DebuggerProxyType.
2. Incorrect: Logical drives can be discovered and interrogated using Visualizers are a powerful new feature of Visual Studio 2005 that allow
WMI. developers to see virtually every aspect of an object at debug time.
3. Correct: Although some database information can be retrieved using
WMI, a list of all OleDb-compliant databases cannot be determined Windows Management Instrumentation (WMI) and the System.Management
using the WMI query syntax namespace provide a framework for querying information about virtually every
aspect of a computer's resources.
4. Incorrect: A list of Windows Services running on a machine, and their
respective states, can be queried via WMI.