Your SlideShare is downloading. ×
Back-2-Basics: Exception & Event Instrumentation in .NET<br />
2<br />
Check Out Your Local User Groups!<br />San Diego Cloud Computing User Group<br />www.azureusergroup.com/group/sandiegoazur...
Win Free Software!<br />Rules<br />Provide your business card (or email and name)*<br />Indicate on the back what software...
Summary<br />5<br />
Logging Exceptions & Events<br />
Why Do We Need To?<br />Debugging statements only work in debug builds and while in VS<br />No way to tell what applicatio...
Logging Methods You Use?<br />Log to Event log?<br />Custom text files?<br />Custom xml files?<br />Database?<br />….<br />
The .NET Solution<br />Trace Listeners!<br />Easy to use from any .NET application including ASP.NET!!<br />Extremely conf...
.NET Trace Listeners<br />
Overview<br />Write informative messages about the execution of an application at run time.<br />Phases<br />Instrumentati...
Overview<br />Output is written to one to many listeners.<br />Part of the Trace.Listeners collection<br />Output Methods:...
Out of the Box Listeners<br />TextWriterTraceListener<br />Writes to the TextWriter or Steam class<br />EventLogTraceListe...
Out of the Box Listeners<br />ConsoleTraceListener<br />Writes output to the console window<br />DelimitedListTraceListene...
Configuration<br />Done via code or application config file<br />Turn off/ on listeners<br />Set trace levels<br />Set fil...
Configuration Example<br /><system.diagnostics><br />  <sources><br />    <source name="DefaultSource" switchName="Default...
Add custom functionality & enhance .NET listeners<br />Custom Trace Listeners<br />
Overview<br />Create your own listeners or enhance current listeners<br />Inherit TraceListener<br />Override:<br />Write ...
Overview<br />Override:<br />TraceData - Writes trace information, a data object and event information to the listener spe...
Issues with .NET Listeners<br />XmlWriterTraceListener<br />Does not write well formed xml<br />Creates one huge file<br /...
Taking Exception & Event logging to the extreme!<br />dotNetTips.Utility Listeners<br />
Overview<br />Listeners part of the dotNetTips.Utility<br />Open source project available at http://codeplex.com/dotnettip...
Advanced Debugging Info<br />Via the LogEntry class<br />Automatic when logging Exceptions:<br />Tread/ Process informatio...
More on LogEntry<br />Add custom information into a collection<br />Additional properties<br />Category<br />Id (GUID)<br ...
Enhanced Listeners<br />EventLogTraceListeners<br />XmlTraceListeners<br />Fixes all issues of the .NET implementation!<br...
Added Listeners<br />EmailTraceListener<br />Email trace events via smtp server<br />WebServiceTraceListener<br />Sends tr...
Writing Exceptions/ Events<br />Use LogWriter<br />Custom for logging Exceptions, Events and LogEntry<br />Logging Excepti...
Lets Kick It Up A Notch!<br />Centralized Logging System<br />
The Problem<br />Each client/ server machine logs locally<br />Support has to logon to each machine to read events<br />No...
The Solution<br />Centralized Logging System<br />Uses WebServerTraceListener<br />Writes to web service that stores event...
Summary<br />Use TraceListeners to log Exceptions/ Events!<br />Use my Centralized Logging System<br />dotNetTips.Utility ...
Upcoming SlideShare
Loading in...5
×

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

442

Published on

This session 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
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
442
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • ASK AUDIENCE BEFORE SHOWING LIST.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.
  • Transcript of "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. Check Out Your Local User Groups!<br />San Diego Cloud Computing User Group<br />www.azureusergroup.com/group/sandiegoazureusergroup<br />San Diego .NET Developers Group<br />www.sddotnetdg.org<br />San Diego .NET User Group<br />www.sandiegodotnet.com<br />San Diego SQL Server User Group<br />www.sdsqlug.org<br />
    4. 4. Win Free Software!<br />Rules<br />Provide your business card (or email and name)*<br />Indicate on the back what software you are interested in<br />Otherwise I will pick <br />Winners will be picked next week<br />*Yes, most likely I’m going to send you and email about my user group (sddotnetdg.org) and or web site (dotNetTips.com)<br />Prizes<br />CodeRush and Refactor Pro from DevExpress (4)<br />SecondCopy (automatic backup software) (5) *<br />CodeIt.Right Standard from SubMain (4)<br />*Requires mailing address and phone number<br />
    5. 5. Summary<br />5<br />
    6. 6. Logging Exceptions & Events<br />
    7. 7. 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 />Log information for reporting<br />7<br />
    8. 8. Logging Methods You Use?<br />Log to Event log?<br />Custom text files?<br />Custom xml files?<br />Database?<br />….<br />
    9. 9. 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 />
    10. 10. .NET Trace Listeners<br />
    11. 11. 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 />11<br />
    12. 12. 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 />
    13. 13. 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 />
    14. 14. 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 />
    15. 15. Configuration<br />Done via code or application config file<br />Turn off/ on listeners<br />Set trace levels<br />Set filters<br />
    16. 16. Configuration Example<br /><system.diagnostics><br /> <sources><br /> <source name="DefaultSource" switchName="DefaultSwitch"><br /> <listeners><br /> <add name="FileLog"/><br /> </listeners><br /></source><br /></sources><br /><switches><br /> <add name="DefaultSwitch" value="Information" /><br /></switches><br /> <sharedListeners><br /> <add name="FileLog“ type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL“ initializeData="FileLogWriter"/><br /> </sharedListeners><br /></system.diagnostics><br />
    17. 17.
    18. 18. Add custom functionality & enhance .NET listeners<br />Custom Trace Listeners<br />
    19. 19. 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 />19<br />
    20. 20. 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 />20<br />
    21. 21. 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 />
    22. 22. Taking Exception & Event logging to the extreme!<br />dotNetTips.Utility Listeners<br />
    23. 23. Overview<br />Listeners part of the dotNetTips.Utility<br />Open source project available at http://codeplex.com/dotnettips<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 />23<br />
    24. 24. 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 />
    25. 25. 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 />
    26. 26. 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 />
    27. 27. 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 />
    28. 28. 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 />
    29. 29.
    30. 30. Lets Kick It Up A Notch!<br />Centralized Logging System<br />
    31. 31. 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 />31<br />
    32. 32. 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 />
    33. 33.
    34. 34. Summary<br />Use TraceListeners to log Exceptions/ Events!<br />Use my Centralized Logging System<br />dotNetTips.Utility (codeplex.com/dotnettips)<br />ASP.NET site/ database (dotNetTips.com)<br />Also look into the Enterprise Library<br />Lots more then just logging<br />

    ×