CollectionSpy 1.1.0

dotnet add package CollectionSpy --version 1.1.0
                    
NuGet\Install-Package CollectionSpy -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CollectionSpy" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CollectionSpy" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CollectionSpy" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CollectionSpy --version 1.1.0
                    
#r "nuget: CollectionSpy, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CollectionSpy@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CollectionSpy&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=CollectionSpy&version=1.1.0
                    
Install as a Cake Tool

<div align="center">

đŸ•ĩī¸â€â™‚ī¸ CollectionSpy

The Zero-Friction Debugging Toolkit for C# Collections

NuGet NuGet Downloads License .NET 8.0

Stop guessing who modified your data. Trap them red-handed.

English | įŽ€äŊ“中文

</div>


🚀 Why CollectionSpy?

Debugging complex state management, legacy code, or high-frequency data streams (like PLC signals in industrial automation) can be a nightmare when a List or Dictionary is modified unexpectedly.

  • ❌ ObservableCollection is too verbose for temporary debugging and pollutes your architecture.
  • ❌ Conditional Breakpoints in IDEs (like Visual Studio) are painfully slow and cannot be shared with your team.
  • ❌ AOP Frameworks (like PostSharp) are heavy, slow to compile, and overkill for simple debugging tasks.

CollectionSpy solves this with a fluent, declarative API. It provides drop-in replacements (TrapList, TrapDictionary, etc.) that let you inject Breakpoints, Stack Traces, or Custom Logs exactly when specific data conditions are met.


đŸ“Ļ Installation

Grab the latest version from NuGet:

dotnet add package CollectionSpy

Or via the Package Manager Console:

Install-Package CollectionSpy

🎮 Live Dashboard Demo

Want to see CollectionSpy in action? We've built a real-time WPF PLC Signal Monitor Dashboard to demonstrate how to use TrapList with UI data binding (INotifyCollectionChanged).

(Note: You can add a GIF or Screenshot here later by running the WPF app and capturing the screen)

  1. Clone the repo.
  2. Set TrapLibrary.WpfDemo as the startup project.
  3. Run it and click the "Add Overheat Signal (Trap!)" button to see the logs trigger instantly!

⚡ Quick Start

1. Spy on a List

You can create a TrapList directly, or convert any existing IEnumerable using the elegant fluent extensions:

using Debugging.Traps;
using Debugging.Traps.Extensions; // Gives you .ToTrapList(), .ToTrapDictionary(), etc.

// Instead of new List<User>(), just do this:
var users = GetUsers().ToTrapList(); 

// đŸŽ¯ Scenario 1: Break execution when a bad object is added
users.OnAdd()
     .When(u => u.Name == null) // The condition
     .Do(TrapActions.Break());  // The trap (Debugger.Break)

// đŸŽ¯ Scenario 2: Log a warning when a specific critical ID is removed
users.OnRemove()
     .When(u => u.Id == 999)
     .Do(TrapActions.Log("🚨 WARNING: Admin user 999 was removed!"));

2. Spy on a Dictionary

Perfect for monitoring configuration changes or caching layers.

var config = GetConfigs().ToTrapDictionary();

// 🚨 Alert if a secure setting is downgraded to HTTP
config.OnUpdate()
      .When((key, value) => key == "ApiUrl" && value.StartsWith("http:"))
      .Do(TrapActions.Log("SECURITY ALERT: API URL downgraded to insecure HTTP!"));

3. Spy on a HashSet

Catch inefficient code logic or duplicate entries easily.

var uniqueTags = new TrapHashSet<string>();

// 🐌 Detect inefficient code: attempting to add a tag that is already present
uniqueTags.OnAdd()
          .When(tag => uniqueTags.Contains(tag))
          .Do(TrapActions.Log("Inefficient Code: Tag already exists in the set!"));

4. Spy on Queues and Stacks

// Spy on a Queue (FIFO) - Great for Task processing
var jobQueue = new List<string> { "init_job" }.ToTrapQueue();

jobQueue.OnEnqueue()
        .When(job => job == "POISON_PILL")
        .Do(TrapActions.Log("Critical: Poison pill enqueued!"));

// Spy on a Stack (LIFO) - Great for UI Navigation tracking
var navStack = new TrapStack<string>();

navStack.OnPop()
        .When(page => page == "Root")
        .Do(TrapActions.DumpStackTrace("Root Page Popped By:"));

đŸ›Ąī¸ Performance & Production Safety

CollectionSpy is engineered for critical debugging in production environments with virtually zero overhead when you need it to be fast.

⚡ Zero-Allocation Architecture (v1.0+)

The library uses a highly optimized Copy-On-Write strategy for rule storage.

  • Zero Allocations: Executing traps (adding/removing items) incurs 0 bytes of memory allocation on the hot path. No closures or hidden objects are created during enumeration.
  • Microsecond Overhead: Even with active traps evaluating conditions, the overhead is measured in single-digit microseconds.
  • Thread Safe Configuration: Rule addition and removal are lock-free and thread-safe.

📊 Benchmark Snippet

Comparison of List<int>.Add() operations (10,000 items):

Method Mean Time Ratio Gen0 Allocations
Native List<T> ~7.9 Îŧs 1.0x -
AddWithoutTrap ~9.7 Îŧs 1.2x -
TrapList (Active) ~71.0 Îŧs 8.9x -

Benchmarks run on AMD Ryzen 9 8945HX, .NET 8.0. Check BENCHMARKS.md for full details.

🚀 Bypassing Traps (Bulk Inserts)

Need to initialize a large collection without triggering traps and ruining performance? Use the Bypass API:

// Fast bulk insert, ZERO trap overhead
myTrapList.AddWithoutTrap(newItem);
myTrapList.AddRange(largeCollection); // Native speed

đŸŽ›ī¸ The Global Kill Switch

To disable all overhead in production, simply flip the master switch:

TrapManager.Enabled = false;

When disabled, the interception logic returns immediately (fast-fail), imposing negligible overhead. Note that in Release builds, the library will emit a single Console Warning on startup to alert you if traps are left active.


đŸ—ēī¸ Roadmap & Next Steps

We are actively evolving CollectionSpy from a "handy tool" to an "industrial-grade framework". Check out our Roadmap for upcoming features, including:

  • ✅ INotifyCollectionChanged support for WPF/WinForms data binding (Completed!).
  • ✅ Thread-safe ConcurrentDictionary support for high-performance backend processing (Completed!).
  • ⚡ Source Generators for true zero-overhead, AOT-friendly compilation.

🤝 Contributing

Contributions, issues, and feature requests are welcome! Feel free to check issues page.

📝 License

This project is MIT licensed.


Crafted with â¤ī¸ by angleyanalbedo

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 85 3/30/2026
1.0.1 88 3/24/2026
1.0.0 79 3/23/2026
0.0.7 80 3/22/2026
0.0.4 74 3/22/2026
0.0.2 71 3/22/2026
0.0.1 81 3/22/2026