Back-2-Basics: Exception & Event Instrumentation in .NET


Published on

This will instruct any level of programmer on how to easily use tracing that is built into .NET to log and analyze Exceptions and events that occur during application runtime. This is invaluable to fix bugs that only happen while the application is running. .NET TraceListeners will be discussed in detail along with how to write your own custom TraceListeners. I will also discuss and provide code for my centralized exception/ event logging system that allows applications at customer sites or on multiple servers to log to central database. Developers or technical support personal can then view these entries via an ASP.NET web site.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Do you have issues with file logging with ASP.NET??
  • Briefly discuss the difference between Debug and Tracing.When you create a distributed application, you might find it difficult to test the application in the manner in which it will be used. Few development teams have the capability to test all possible combinations of operating systems or Web browsers (including all the localized language options), or to simulate the high number of users that will access the application at the same time. Under these circumstances, you cannot test how a distributed application will respond to high volumes, different setups, and unique end-user behaviors. Also, many parts of a distributed application have no user interface with which you can interact directly or view the activity of those parts.However, you can compensate for this by enabling distributed applications to describe certain events of interest to system administrators, especially things that go wrong, by instrumenting the application — that is, by placing trace statements at strategic locations in your code. Then if something unexpected occurs at run time (for example, excessively slow response time), you can determine the likely cause.
  • Back-2-Basics: Exception & Event Instrumentation in .NET

    1. 1. Back-2-Basics: Exception & Event Instrumentation in .NET<br />
    2. 2. 2<br />
    3. 3. Summary<br />3<br />
    4. 4. Logging Exceptions & Events<br />
    5. 5. Why Do We Need To?<br />Debugging statements only work in debug builds and while in VS<br />No way to tell what applications are doing during runtime<br />Need to log exceptions for evaluation by support & development teams<br />Need to log other information during runtime<br />5<br />
    6. 6. Logging Methods You Use?<br />Log to Event log?<br />Custom text files?<br />Custom xml files?<br />Database?<br />….<br />
    7. 7. The .NET Solution<br />Trace Listeners!<br />Easy to use from any .NET application including ASP.NET!!<br />Extremely configurable<br />Easy to write your own listeners<br />
    8. 8. .NET Trace Listeners<br />
    9. 9. Overview<br />Write informative messages about the execution of an application at run time.<br />Phases<br />Instrumentation — you add trace code to your application. <br />Tracing — the tracing code writes information to the specified target. <br />Analysis — you evaluate the tracing information to identify and understand problems in the application.<br />9<br />
    10. 10. Overview<br />Output is written to one to many listeners.<br />Part of the Trace.Listeners collection<br />Output Methods:<br />Assert - The specified text; or, if none is specified, the Call Stack. <br />Fail - The specified text; or, if none is specified, the Call Stack.<br />Write<br />WriteIf<br />WriteLine<br />WriteLineIf<br />
    11. 11. Out of the Box Listeners<br />TextWriterTraceListener<br />Writes to the TextWriter or Steam class<br />EventLogTraceListener<br />Writes to an event log<br />Event works with ASP.NET!<br />DefaultTraceListener<br />Writes Write and WriteLine messages to the OutputDebugString and to the Debugger.Log method (debug window in VS)<br />
    12. 12. Out of the Box Listeners<br />ConsoleTraceListener<br />Writes output to the console window<br />DelimitedListTraceListener <br />Writes to the text writer in a delimited text format<br />XmlWriterTraceListener<br />Writes to xml file.<br />
    13. 13. Configuration<br />Done via code or application config file<br />Turn off/ on listeners<br />Set trace levels<br />Set filters<br />
    14. 14. Configuration Example<br />&lt;system.diagnostics&gt;<br /> &lt;sources&gt;<br /> &lt;source name=&quot;DefaultSource&quot; switchName=&quot;DefaultSwitch&quot;&gt;<br /> &lt;listeners&gt;<br /> &lt;add name=&quot;FileLog&quot;/&gt;<br /> &lt;/listeners&gt;<br />&lt;/source&gt;<br />&lt;/sources&gt;<br />&lt;switches&gt;<br /> &lt;add name=&quot;DefaultSwitch&quot; value=&quot;Information&quot; /&gt;<br />&lt;/switches&gt;<br /> &lt;sharedListeners&gt;<br /> &lt;add name=&quot;FileLog“ type=&quot;Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL“ initializeData=&quot;FileLogWriter&quot;/&gt;<br /> &lt;/sharedListeners&gt;<br />&lt;/system.diagnostics&gt;<br />
    15. 15.
    16. 16. Add custom functionality/ enhance .NET listeners<br />Custom Trace Listeners<br />
    17. 17. Overview<br />Create your own listeners or enhance current listeners<br />Inherit TraceListener<br />Override:<br />Write - writes the specified message to the listener.<br />WriteLine - Writes a message to the listener.<br />TraceEvent - Writes trace information, a message, and event information to the listener specific output.<br />17<br />
    18. 18. Overview<br />Override:<br />TraceData - Writes trace information, a data object and event information to the listener specific output.<br />GetSupportedAttributes - Gets the custom attributes supported by the trace listener.<br />Add to config file<br />In place or in addition to the .NET listeners.<br />18<br />
    19. 19. Issues with .NET Listeners<br />XmlWriterTraceListener<br />Does not write well formed xml<br />Creates one huge file<br />With ASP.NET it says locked<br />Can not move, delete etc. <br />Can not view latest trace writes<br />Due to flushing issues<br />Most other file based listeners most likely suffer from the same issues<br />
    20. 20. Taking Exception & Event logging to the extreme!<br />dotNetTips.Utility Listeners<br />
    21. 21. Overview<br />Listeners part of the dotNetTips.Utility<br />Open source project available at<br />Current version requires .NET 3.5<br />Fixes/ enhances .NET Listeners<br />Adds new Listeners<br />Outputs A LOT more debugging info!<br />Add custom information during runtime!<br />Built-in configuration!<br />21<br />
    22. 22. Advanced Debugging Info<br />Via the LogEntry class<br />Automatic when logging Exceptions:<br />Tread/ Process information<br />Domain name<br />Application information<br />User information<br />Computer information<br />Auto log class and method where Exception happened<br />Much more!<br />
    23. 23. More on LogEntry<br />Add custom information into a collection<br />Additional properties<br />Category<br />Id (GUID)<br />User<br />MachineName<br />Severity<br />Source<br />SourceVersion<br />
    24. 24. Enhanced Listeners<br />EventLogTraceListeners<br />XmlTraceListeners<br />Fixes all issues of the .NET implementation!<br />Writes well formed xml!<br />Does not lock file!<br />Creates a new file for each day<br />Automatic delete of old files (configurable)<br />
    25. 25. Added Listeners<br />EmailTraceListener<br />Email trace events via smtp server<br />WebServiceTraceListener<br />Sends trace events to a web service method<br />TCPTraceListener (coming soon)<br />Sends trace events to TCP listeners<br />Watch events in real time!<br />
    26. 26. Writing Exceptions/ Events<br />Use LogWriter<br />Custom for logging Exceptions, Events and LogEntry<br />Logging Exceptions and Events uses LogEntry under the hood<br />Writes to the correct log collection based on type of application<br />Methods<br />WriteEntry<br />Messages (events) and LogEntry<br />WriteException<br />
    27. 27.
    28. 28. Lets Kick It Up A Notch!<br />Centralized Logging System<br />
    29. 29. The Problem<br />Each client/ server machine logs locally<br />Support has to logon to each machine to read events<br />Not possible sometimes at client location<br />No search capabilities<br />Server<br />Server<br />Server<br />Log<br />File<br />Log<br />File<br />Log<br />File<br />Server<br />Server<br />Server<br />Log<br />File<br />Log<br />File<br />Log<br />File<br />29<br />
    30. 30. The Solution<br />Centralized Logging System<br />Uses WebServerTraceListener<br />Writes to web service that stores events in SQL Server<br />Search/View for Events via ASP.NET!<br />Client or Server<br />Web<br />Service<br />SQL<br />Server<br />ASP.<br />NET<br />
    31. 31.
    32. 32. Summary<br />Use TraceListeners to log Exceptions/ Events!<br />Use my Centralized Logging System<br />dotNetTips.Utility (<br />ASP.NET site/ database (<br />Also look into the Enterprise Library<br />Lots more then just logging<br />