---
title: "Log JavaScript object access with Proxy in C#"
url: https://recursiveai.org/oneecma/cookbook/observe-javascript-objects-with-proxy/
description: "Wrap a JavaScript object with Proxy and Reflect in C#. See how to log property reads and writes and handle a revoked proxy in OneEcma."
version: "1.1.1"
---

# Log JavaScript object access with Proxy in C#

**OneEcma** is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneEcma) · [GitHub](https://github.com/recursive-ai-oss/oneecma-csharp) · [Docs](https://recursiveai.org/oneecma/docs/) · [Download example (.zip)](https://recursiveai.org/oneecma/cookbook/observe-javascript-objects-with-proxy/observe-javascript-objects-with-proxy.zip)

```shell
dotnet add package RecursiveAI.OneEcma
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneEcma;
var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const log = [];
const item = new Proxy({ qty: 2 }, {
  get(target, key, receiver) {
    log.push(`get ${String(key)}`);
    return Reflect.get(target, key, receiver);
  }
});
const qty = item.qty;
`${log.join(', ')}: ${qty}`;
""").AsString);
```

Unzip [the example](https://recursiveai.org/oneecma/cookbook/observe-javascript-objects-with-proxy/observe-javascript-objects-with-proxy.zip), then `dotnet run`.

Prints:

```text
get qty: 2
```

## The full program

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const log = [];
const { proxy, revoke } = Proxy.revocable({ qty: 2 }, {
  get(target, key, receiver) {
    log.push(`get ${String(key)}`);
    return Reflect.get(target, key, receiver);
  },
  set(target, key, value, receiver) {
    log.push(`set ${String(key)} = ${value}`);
    return Reflect.set(target, key, value, receiver);
  }
});

proxy.qty = 3;
const qty = proxy.qty;
log.push(`qty: ${qty}`);
revoke();
try {
  proxy.qty;
} catch (error) {
  log.push(`${error.name}: ${error.message}`);
}
log.join('\n');
""").AsString);
```

`dotnet run` prints:

```text
set qty = 3
get qty
qty: 3
TypeError: Cannot perform 'get' on a proxy that has been revoked
```

The full program logs a write and a read, then revokes the proxy. After revocation, a property read throws a JavaScript `TypeError` before the handler runs. The sample catches that error inside the script and prints its name and message.

## Variations

**Keep getter behavior when forwarding.** The getter `doubled` reads `this.qty`. Passing `receiver` to `Reflect.get` makes that nested access go through the proxy too:

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const target = {
  qty: 2,
  get doubled() { return this.qty * 2; }
};
const log = [];
const item = new Proxy(target, {
  get(target, key, receiver) {
    log.push(String(key));
    return Reflect.get(target, key, receiver);
  }
});
const value = item.doubled;
`${log.join(' -> ')}: ${value}`;
""").AsString);
```

```text
doubled -> qty: 4
```

## Related recipes

- [Call a JavaScript function from C#](https://recursiveai.org/oneecma/cookbook/call-javascript-function/)
- [Get a JavaScript error report in C#](https://recursiveai.org/oneecma/cookbook/error-reports/)

## The short answer

In OneEcma, evaluate a script that calls `new Proxy(target, handler)`. The handler’s `get` method logs a read and forwards it with `Reflect.get(target, key, receiver)`. Add a `set` method for writes, forwarding with `Reflect.set`. In these examples the target and proxy are JavaScript objects, and C# receives the log as a string. Wrapping a JavaScript object does not automatically intercept reads on a CLR object.

## How do you use a JavaScript Proxy to log property access?

A [handler method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) is called a trap. A `get` trap runs when code reads a property through the wrapper, and a `set` trap runs when code assigns through it. Log the operation before forwarding if you want to record the attempt, as the full program does for `set qty = 3`. Direct reads and writes on the original target do not pass through the wrapper. Hand out the proxy wherever you expect reads or writes to be logged.

Only the operations for which you provide traps get custom behavior. For example, a read of a nested object goes through the outer wrapper, but later reads on that nested object do not go through the same wrapper unless it, too, is proxied. A property log is not a copy of every change to the underlying object.

## What does Reflect do in JavaScript Proxy handlers?

[`Reflect.get`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/get) performs the normal property read with the receiver you pass. That receiver becomes `this` for a getter. In the variation, `doubled` reads `this.qty`, so both property names appear in the log. Passing the raw target as the receiver instead would make the nested read bypass the proxy. Forward to the trap’s `target` argument, not the wrapper, to avoid reentering the same trap for the same read.

[`Reflect.set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/set) reports whether the assignment succeeded. Return that Boolean from the trap rather than reporting success unconditionally. OneEcma’s [Proxy documentation](https://recursiveai.org/oneecma/docs/collections-proxy/#proxy) describes invariant checks: for instance, a trap cannot return a substitute value for a non-writable, non-configurable own data property.

## What happens when you revoke a JavaScript Proxy?

[`Proxy.revocable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/revocable) returns a wrapper and a `revoke` function. Calling it disables that wrapper; it does not undo a write already forwarded to the target. Keep the function if you need to stop use of a wrapper later. The last line of the program comes from the JavaScript `catch`, not a `get` trap: the revoked wrapper throws before the read can be forwarded or logged.
