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.
3. Check Out Your Local User Groups! San Diego Cloud Computing User Group www.azureusergroup.com/group/sandiegoazureusergroup San Diego .NET Developers Group www.sddotnetdg.org San Diego .NET User Group www.sandiegodotnet.com San Diego SQL Server User Group www.sdsqlug.org
4. Win Free Software! Rules Provide your business card (or email and name)* Indicate on the back what software you are interested in Otherwise I will pick Winners will be picked next week *Yes, most likely I’m going to send you and email about my user group (sddotnetdg.org) and or web site (dotNetTips.com) Prizes CodeRush and Refactor Pro from DevExpress (4) SecondCopy (automatic backup software) (5) * CodeIt.Right Standard from SubMain (4) *Requires mailing address and phone number
7. Why Do We Need To? Debugging statements only work in debug builds and while in VS No way to tell what applications are doing during runtime Need to log exceptions for evaluation by support & development teams Need to log other information during runtime Log information for reporting 7
8. Logging Methods You Use? Log to Event log? Custom text files? Custom xml files? Database? ….
9. The .NET Solution Trace Listeners! Easy to use from any .NET application including ASP.NET!! Extremely configurable Easy to write your own listeners
11. Overview Write informative messages about the execution of an application at run time. Phases Instrumentation — you add trace code to your application. Tracing — the tracing code writes information to the specified target. Analysis — you evaluate the tracing information to identify and understand problems in the application. 11
12. Overview Output is written to one to many listeners. Part of the Trace.Listeners collection Output Methods: Assert - The specified text; or, if none is specified, the Call Stack. Fail - The specified text; or, if none is specified, the Call Stack. Write WriteIf WriteLine WriteLineIf
13. Out of the Box Listeners TextWriterTraceListener Writes to the TextWriter or Steam class EventLogTraceListener Writes to an event log Event works with ASP.NET! DefaultTraceListener Writes Write and WriteLine messages to the OutputDebugString and to the Debugger.Log method (debug window in VS)
14. Out of the Box Listeners ConsoleTraceListener Writes output to the console window DelimitedListTraceListener Writes to the text writer in a delimited text format XmlWriterTraceListener Writes to xml file.
15. Configuration Done via code or application config file Turn off/ on listeners Set trace levels Set filters
19. Overview Create your own listeners or enhance current listeners Inherit TraceListener Override: Write - writes the specified message to the listener. WriteLine - Writes a message to the listener. TraceEvent - Writes trace information, a message, and event information to the listener specific output. 19
20. Overview Override: TraceData - Writes trace information, a data object and event information to the listener specific output. GetSupportedAttributes - Gets the custom attributes supported by the trace listener. Add to config file In place or in addition to the .NET listeners. 20
21. Issues with .NET Listeners XmlWriterTraceListener Does not write well formed xml Creates one huge file With ASP.NET it says locked Can not move, delete etc. Can not view latest trace writes Due to flushing issues Most other file based listeners most likely suffer from the same issues
22. Taking Exception & Event logging to the extreme! dotNetTips.Utility Listeners
23. Overview Listeners part of the dotNetTips.Utility Open source project available at http://codeplex.com/dotnettips Current version requires .NET 3.5 Fixes/ enhances .NET Listeners Adds new Listeners Outputs A LOT more debugging info! Add custom information during runtime! Built-in configuration! 23
24. Advanced Debugging Info Via the LogEntry class Automatic when logging Exceptions: Tread/ Process information Domain name Application information User information Computer information Auto log class and method where Exception happened Much more!
25. More on LogEntry Add custom information into a collection Additional properties Category Id (GUID) User MachineName Severity Source SourceVersion
26. Enhanced Listeners EventLogTraceListeners XmlTraceListeners Fixes all issues of the .NET implementation! Writes well formed xml! Does not lock file! Creates a new file for each day Automatic delete of old files (configurable)
27. Added Listeners EmailTraceListener Email trace events via smtp server WebServiceTraceListener Sends trace events to a web service method TCPTraceListener (coming soon) Sends trace events to TCP listeners Watch events in real time!
28. Writing Exceptions/ Events Use LogWriter Custom for logging Exceptions, Events and LogEntry Logging Exceptions and Events uses LogEntry under the hood Writes to the correct log collection based on type of application Methods WriteEntry Messages (events) and LogEntry WriteException
29.
30. Lets Kick It Up A Notch! Centralized Logging System
31. The Problem Each client/ server machine logs locally Support has to logon to each machine to read events Not possible sometimes at client location No search capabilities Server Server Server Log File Log File Log File Server Server Server Log File Log File Log File 31
32. The Solution Centralized Logging System Uses WebServerTraceListener Writes to web service that stores events in SQL Server Search/View for Events via ASP.NET! Client or Server Web Service SQL Server ASP. NET
33.
34. Summary Use TraceListeners to log Exceptions/ Events! Use my Centralized Logging System dotNetTips.Utility (codeplex.com/dotnettips) ASP.NET site/ database (dotNetTips.com) Also look into the Enterprise Library Lots more then just logging
Notas do Editor
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.