Kathleen Dollard - CodeRapid @kathleendollard kathleendollard [email protected] Blog: http://blogs.msmvps.com/kathleen http://www.pluralsight.com/author/kathleen -dollard Better Code, Better Debugging Development Debugging Production Legacy Development Debugging Production Legacy Development Debugging Production Legacy o How to approach bug free code o How to debug in tools o How to debug production code o How to approach bug free code o How to debug in tools o How to debug production code o Consistency ◦ ◦ ◦ ◦ Method length Variable usage Naming Spelling o Got FxCop? Visual Studio 2015 Analyzers o NuGet delivered ◦ Attached to project ◦ Runs in Visual Studio ◦ Runs in all builds o Write them! ◦ Or tweak them ◦ Upcoming course on Pluralsight.com Summary: Visual Studio 2015 Analyzers Life With Analyzers Life Without Analyzers o How to approach bug free code o How to debug in tools o How to debug production code o How to approach bug free code o How to debug in tools o How to debug production code A Debugging Strategy Scientific Method Repro, think, write test Something Happens No Can you repro in Visual Studio? Yes Create several hypotheses Select one to disprove Outside production? Test Evaluate We Work Differently During Development Development • During development, you focus on events from your application • You need quick access • Solve bugs every hour • Repeating many times expected Production • During production, interaction with other things matter • You have relatively more time and stress • Solve bugs every week? Month? • Repeating problems in production should be avoided Visual Studio 2015 o Debugging ◦ Breakpoint Peek ◦ Watch ◦ Undo Breakpoint delete o How to approach bug free code o How to debug in tools o How to debug production code o How to approach bug free code o How to debug in tools o How to debug production code Production Bugs Logger.Log.OpenFile(...) Your Logging Framework Semantic Log Abstraction Your Logging Framework Production Code System…Trace.Write(...) Production Code Abstraction Over Your Logging Framework Semantic Logging private void FireEvent2_Click(object sender, RoutedEventArgs e) { Logger.Log.Message("Test message"); } private void KeyEvent2_Click(object sender, RoutedEventArgs e) { Logger.Log.AccessByPrimaryKey(GetPrimaryKey(), "Customer"); } o Strongly typed o IntelliSense driven o Details like keywords and level defined/changed at central point o Aids code readability Things that make debugging hard o Desktop ◦ ◦ ◦ ◦ Rare Distance (can’t sit down and watch) Watching doesn’t help Registry corruption/error ◦ Machine dependent issues o Server ◦ ◦ ◦ ◦ ◦ Same issues as desktop plus Inter-process issues Higher pressure on threads and memory Authorization issues Not being an admin o Hookup Visual Studio remote debugging? o Run Visual Studio on your server to debug? o Tracing records o Outside the path through your app forces affect your app ◦ .NET ◦ Operating system ◦ Service/web calls ◦ Many others o ETW integrates view of your application with outside factors - holistic Old-Style Tracing o You create the entire picture o Tools roughly give you a crayon to draw with o Technique ◦ Trace.WriteXxxx ◦ TraceSource ◦ Third-party such as Log4Net o In-process recording devastates scalability New-Style Tracing o Out-of-proc or off-thread very quickly o Vast amount o Your of data already available app drops ‘orienteering’ locations like GPS points o EventSource and Semantic Logging Application Block (SLAB) On and off, filtering ETW Design ETW Session ETW Session ETW Controller Create trace entry ETW Session ETW Session ETW Provider ETW Sessions – managed by the operating system ETW Session Observe, access trace info ETW Session ETW Session ETW Session ETW Consumer ETW Session ETW Design Summary o ETW is composed of ◦ ◦ ◦ ◦ o Core (sessions) Controllers Providers Consumers ETW does most of the work outside your process ◦ Particularly important for resources like writing files o ETW is blazingly fast ◦ It’s in the operating system ◦ It creates binary files or streams o Manifests explain how to interpret binary files ◦ Contain provider IDs (use strings), event IDs (integers 0, 1…), and payload def ◦ Can be registered on the machine ◦ Can be in-line in a message Overly Simple (bad) EventSource Default Id is “SimpleEventSource” namespace EventDefinitions { public class SimpleEventSource : EventSource {static public SimpleEventSource Log = new SimpleEventSource(); public void Message(string Message) { WriteEvent(1, Message); } public void AccessByPrimaryKey( int PrimaryKey, string TableName) { WriteEvent(2, PrimaryKey, TableName); } } } o Please define a few things These must match the order of logging methods in the file Parameters/args must match “Normal” (good) EventSource [EventSource(Name = "KadGen-EtwSamples-Normal")] public class NormalEventSource : EventSource { static public NormalEventSource Log = new NormalEventSource(); [Event(1)] public void Message(string Message) { WriteEvent(1, Message); } [Event(2)] public void AccessByPrimaryKey( int PrimaryKey, string TableName) { WriteEvent(2, PrimaryKey, TableName); } o Please define a few things } Semantic Logging Application Block SLAB o Uses EventSource (.NET 4.5+) ◦ No other dependencies o Fantastic documentation, including semantic logging concepts o Out-of-proc o Use provides consumers you’re familiar with in-proc if it’s the best your team can do now o Sadly, lag behind EventSource version Profiling Profiling o Do you use a profiling tool? ◦ Do you know how to use one? o Do you believe you will never have a performance problem? ◦ Are you planning on hiring an expert and have budget in place? Visual Studio 2015 o Profiling Visual Studio 2015 o Profiling ◦ PerfTips Profiling o Do you use a profiling tool? ◦ Do you have one you know how to use? o Do you believe you will never have a performance problem? ◦ Are you planning on hiring an expert and have budget in place? o Pragmatic plan #1 (imperfect base plan) ◦ PerfTips to find candidate issues ◦ Start/Stop tracing to confirm ◦ Manually explore that <150 lines of code o Pragmatic plan #2 (issue seems unpredictable, broad, nonsensical) ◦ Look for allocation issues o Pragmatic plan #3: Call an expert o Pragmatic plan #4: Live with it Development Debugging Production Legacy Development Debugging Production Legacy Debugging o Scientific method ◦ Record ideas ◦ Try to disprove one ◦ Reconsider ideas Development Production Legacy o Learn Visual Studio tools o Provide logging ◦ Out of proc and semantic o Refactor ◦ Then fix o [email protected] o My blog: http://msmvps.com/blogs/kathleen o @kathleendollard on Twitter o www.WintellectNow.com, Free Series o Pluralsight, Kathleen Dollard ◦ ◦ ◦ ◦ Event Tracing for Windows (ETW) in .NET, http://bit.ly/1iZeoyZ .NET Puzzles, http://bit.ly/19z2JaD What’s New in .NET 4.5, http://bit.ly/YTb962 Module 3 is ETW Visual Studio 2015 Analyzers, upcoming o Vance Morrison’s blog: http://blogs.msdn.com/b/vancem/ o SLAB https://github.com/mspnp/semantic-logging
© Copyright 2024