WHAT'S NEW?
Loading...

Day 14 Programming in C# 70-483

Index


  • Introduction
  • Logging and tracing
  • Profiling
  • Performance counters
  • References

Introduction

This post is part of a series of post to help you prepare the MCSD certification, particularly the certification exam 70-483, based on the book:


You will find all the code available in the following GitHub repository. Lets talk a little bit about threads and how they work using a .Net development environment.

Logging and tracing

  • Tracing refers to monitor the execution of your applications. Typically, you enable it when you want to investigate something. 
  • Logging is always enabled and tracks events occurring within your application, can be an error or just some useful information. In case of something critical occurred you can rise an email to someone.

.Net helps you to trace and log your app with the Debug class within the System.Diagnostics namespace. As its name suggests is only available in debug mode (the ConditionalAttribute with a value of DEBUG is applied to this class). By default, it writes to the Output window, see the following example where if the Debug.Assert fails a message box asks you to retry, abort or ignore.


The TraceSource class can be used in a similar way as before but it gives us more functionality by using its three parameters:
  1. severity of the event happened with the TraceEventType enum:
    1. Critical: most severe
    2. Error: problem handled.
    3. Warning: something unusual
    4. Information: relevant data
    5. Verbose: the loosest
    6. Stop/Start/Suspend/Resume/Transfer: related to the flow of the app
  2. id to group our calls with the event ID number. Our own groups 
    1. 1.000-1.999 Db calls
    2. 10.000-19.000 WS calls
  3. Message displayed.
Example of the TraceSource usage:


By default all is written to the Output window. TraceListeners can help you to change this behavior. Types of TraceListeners:
  • ConsoleTraceListener
  • DelimitedListTraceListener
  • EventLogTraceListener
  • TextWriteTraceListener
  • XmlWriteTraceListener
Example of how to change the default output of a TraceSource using a TraceListener programmaticly:


You can define these listeners within your application configuration file (app.config / web.config) which makes more easier to change it in live environments instead of having to change your code and doing a new deployment.

You can also write your logs directly to the Windows Event Log by using the EventLog class within the System.Diagnostics namespace (remember to run Visual Studio as administrator). The following example will create a new entry in the Windows event log and in the second execution will add an event to that entry. You can open the "Event Viewer" in Windows and look for "MyNewLog" within the "Applications and Service logs".


To read the event log you can do it programmatically by getting an EventLogEntry object and reading its properties. Besides, you can subscribe to changes with the EntryWritten event and be notified when a new entry is added. See examples here:


Profiling

Profiling refers to the amount of memory your apps use, which methods are called, for how long... The Stopwatch class within the System.Diagnostics namespace will help you found bottlenecks in your code by telling you the time spent in certain areas by calling: Start, Stop and Reset methods.

Visual Studio also has a wizard tool to check performance in your apps within the Analyze menu. You will find four options there:

  1. CPU sampling: it's an initial search for performance problems.
  2. Instrumentation: timing information for each function called.
  3. .Net memory allocation
  4. Resource content data: multithreaded application, it helps you find out why methods have to wait until certain resource is released.

Performance counters

The most typical are: CPU usage, memory usage or length of a query, and all can be viewed with the app perfmon.exe provided by Windows. You can read them by code:



Bear in mind you need to be administrator or member of the "Performance Monitor Users" group. As you can see in the example the PerformanceCounter class implements the IDisposable interface because we can use the using statement. The following types can be useful when you plan to create your own performance counters:

  • NumberOfItems32 / NumberOfItems64: number of operations
  • RateOfCountsPerSecond32 / RateOfCountsPerSecond64: calculate the amount per second of an item or operation
  • AverageTimer32
This is all for today and with this post we've finished chapter number 3. I hope you learnt something today and I hope to see you in the next post.

0 comments:

Post a Comment