Chapter 20



  • Code contracts
  • Tracing
  • Event logging
  • Performance monitoring


The code downloads for this chapter are found at on the Download Code tab. The code for this chapter is divided into the following major examples:

  • Code Contracts
  • Tracing Demo
  • Tracing Demo with EventLog
  • Event Log
  • Event Log Reader
  • Performance Counter


This chapter explains how to get real-time information about your running application in order to identify any issues that it might have during production or to monitor resource usage to ensure that higher user loads can be accommodated. This is where the namespace System.Diagnostics comes into play. This namespace offers classes for tracing, event logging, performance counts, and code contracts.

One way to deal with errors in your application, of course, is by throwing exceptions. However, an application might not fail with an exception, but still not behave as expected. The application might be running well on most systems but have a problem on a few. On the live system, you can change the log behavior by changing a configuration value and get detailed live information about what’s going on in the application. This can be done with tracing.

If there are problems with applications, the system administrator needs to be informed. The Event Viewer is a commonly used tool that not only the system administrator ...

Get Professional C# 2012 and .NET 4.5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.