Monitoring Exceptions is a SNAP

15818As a follow up to my earlier post Making Debugging a SNAP I would like to share with you a new feature I added recently which allows you to monitor exceptions within your managed processes. The exceptions we are talking about here are managed exceptions. Typically you want to see exceptions treated as exceptional, that is, you don’t want to throw exceptions to determine flow control within a normal execution path. You really want exceptions to be exceptional – that is, something has occurred which you did not expect and most likely do not desire. For .Net processes entering a try/catch is relatively cheap when it comes to performance but throwing an exception can be expensive. A process which throws a large number of exceptions during a normal run will have degraded performance. As each exception is thrown the exception record must be built which includes a walk of the stack and all the frames within a thread which can be relatively expensive, especially if you have very large stacks.

Getting Started

So now that I have convinced you that gaining a little insight into exceptions may be beneficial lets see how we can do that with SNAP.
  1. Download SNAP from the link below. The download includes a .Net 3.5 and .net 4.0 flavor of the tool along with x86 and x64 platform versions as well (so 4 exe(s) all together). Use the exe which is most appropriate for the target process you are attempting to collect information.
  2.  Select a process to monitor for exceptions. You have several choices here, you can choose the process by Process ID (PID), process name eg. w3wp.exe, or by the Application Pool Name in the case this is a IIS process. If you know you want to focus on a single process you can choose the PID option and use your favorite tool to grab the PID of the target process, I am a little partial to running “snap –c list” to get the PID but you should feel free to choose what works best for you. If you choose to use the process name and there is more than one process with this name all processes will be targeted and you will have exception logs from each – no worries because the exception log naming format will ensure you can determine which process each log is sourced.
  3. Execute the snap command, eg “snap –c emon –p 1234“ with the exception monitoring running wait until the issue you are troubleshooting reproduces or you feel you have enough data that you want to stop collection – once complete hit Ctrl+C to stop the exception monitoring process – note your monitored process may exit when the snap debugger is disconnected.
  4. Analyze the log files. By default you will find these in the same directory as snap.exe or if you provided an output directory check there.
snapv161811
As you monitor your process a stream of information will be sent to the console – no need to save any of this as it will all be written to a log file for each process when the target process exists or after you close down snap with a Ctrl+C. The data in the console is written in the following format:
time [Process ID : Thread ID] Exception Type –> Exception Message
Here is a sample of what the log will look like. The top section has a summary of the exceptions by type and count. The lower section of the document will include an ordered list of the exceptions as they were captured.
 
<?xml version="1.0" encoding="utf-8"?>
<exception_monitor version="1.6.18.11" processName="w3wp" pid="5652" machine="SP2010DEV" user="toddca" startTime="2012-06-18 12:11:15" startTimeUtc="2012-06-18 17:11:15" endTime="2012-06-18 12:14:26" endTimeUtc="2012-06-18 17:14:26">
  <summary>
    <exceptions>
      <exception type="System.TypeLoadException" count="7" />
    </exceptions>
    <exceptions>
      <exception type="System.Exception" count="3" />
    </exceptions>
    <exceptions>
      <exception type="System.UriFormatException" count="27" />
    </exceptions>
    <exceptions>
      <exception type="System.Configuration.ConfigurationErrorsException" count="32" />
    </exceptions>
    <exceptions>
      <exception type="System.IO.FileNotFoundException" count="9" />
    </exceptions>
    <exceptions>
      <exception type="System.ArgumentException" count="22" />
    </exceptions>
    <exceptions>
      <exception type="System.Web.HttpParseException" count="8" />
    </exceptions>
    <exceptions>
      <exception type="System.Collections.Generic.KeyNotFoundException" count="3" />
    </exceptions>
    <exceptions>
      <exception type="System.Threading.ThreadAbortException" count="3" />
    </exceptions>
    <exceptions>
      <exception type="System.ComponentModel.Win32Exception" count="1" />
    </exceptions>
  </summary>
  <exeptions>
    <exception whenOccured="2012-06-18 12:12:10" whenOccuredUtc="2012-06-18 17:12:10.359">
      <type>System.TypeLoadException</type>
      <message>Could not load type 'Microsoft.Office.SharePoint.ClientExtensions.Publishing.TakeListOfflineRibbonControl' from assembly 'Microsoft.Office.SharePoint.ClientExtensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.</message>
      <tid>9072</tid>
    </exception>
    <exception whenOccured="2012-06-18 12:12:11" whenOccuredUtc="2012-06-18 17:12:11.350">
      <type>System.Exception</type>
      <message />
      <tid>7180</tid>
    </exception>
    <exception whenOccured="2012-06-18 12:12:13" whenOccuredUtc="2012-06-18 17:12:13.843">
      <type>System.UriFormatException</type>
      <message>Invalid URI: The format of the URI could not be determined.</message>
      <tid>6852</tid>
    </exception>
    <exception whenOccured="2012-06-18 12:12:14" whenOccuredUtc="2012-06-18 17:12:14.024">
      <type>System.UriFormatException</type>
      <message>Invalid URI: The format of the URI could not be determined.</message>
      <tid>6852</tid>
    </exception>
    <exception whenOccured="2012-06-18 12:12:14" whenOccuredUtc="2012-06-18 17:12:14.028">
      <type>System.UriFormatException</type>
      <message>Invalid URI: The format of the URI could not be determined.</message>
      <tid>9072</tid>
    </exception>
    <exception whenOccured="2012-06-18 12:12:14" whenOccuredUtc="2012-06-18 17:12:14.198">
      <type>System.UriFormatException</type>
      <message>Invalid URI: The format of the URI could not be determined.</message>
      <tid>9072</tid>
    </exception>
    <exception whenOccured="2012-06-18 12:12:14" whenOccuredUtc="2012-06-18 17:12:14.352">
      <type>System.Configuration.ConfigurationErrorsException</type>
      <message>This element is not currently associated with any context</message>
      <tid>7180</tid>
    </exception>
    <!--snip-->

 

It is important to understand that while running this tool to monitor exceptions you may notice your target process does not perform as well as it did before the snap tool was attached. This is expected as snap is a managed debugger and you are basically running your process under a debugger which will never perform quite as good as when running normally. If you want 24×7 exception monitoring that you can leave on in production you might want to take a look at AviCode which is now included as part of System Center.

As with any tool offered for free on this site I don’t offer direct support, warranty, licensing, etc. but I am willing to answer any questions you may have.

Happy Debugging!

Tool Download: http://todd.in/getsnap (681 kb)

Post to Twitter Post to Facebook Post to LinkedIn Post to Delicious Post to Digg

3 thoughts on “Monitoring Exceptions is a SNAP”

  1. Hi Todd,
    Very cool tool and awesome blog! I checked out the last video but couldn’t see from it which mode you’d recommend for SharePoint / w3wp debugging – would you recommend .NET 3.5 x86 or .NET 3.5 and x64? Can you share a sample command line of using the tool to profile SharePoint? I’ve been trying to use it with a few custom event receivers, but I never see any of my custom code in the generated reports, which makes me think I must be using it wrong.
    Thanks,
    Joe

  2. Joe,
    For SharePoint 2010 you will want to use the exe from the \net35\x64\ directory. To take a snapshot of your website you can run: snap -c -si 5 -p <pid> and replace <pid> with the process ID of the w3wp.exe which is hosting/running your webiste. example: snap -c -is 5 -p 1234 This command will take a snap of your process every 5 seconds.

Comments are closed.