DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Edited on Originally published at delta1labs.com

What renaming does to your Serilog output

You obfuscate a desktop app, ship it, and the structured logs it uploads for support start coming back unreadable — not missing, not malformed, just full of { "b": "Dana Okafor", "c": 49.90 } where last release said { "CustomerName": "...", "Total": ... }. Nothing threw. Serilog ran exactly as before. The property names changed, because one of the two ways you hand an object to Serilog reads those names at runtime, and renaming is precisely the thing that rewrites them.

The real question

Serilog never sees your source. It sees a message template string and some argument values, and it turns them into a set of named properties. So the question for an obfuscated build is the usual one: which property names did Serilog capture as literal text that travels in your assembly unchanged, and which did it capture by reflecting over live metadata that renaming has rewritten? Get that distinction right and your logs stay readable through obfuscation. Get it wrong and your structured events quietly rename themselves.

Two values, two code paths

Take a trivial checkout log over a record.

public record Order(string CustomerName, decimal Total, int Items);

var order = new Order("Dana Okafor", 49.90m, 3);

Log.Information("Checkout for {Order}", order);   // scalar path
Log.Information("Checkout for {@Order}", order);  // destructure path
Enter fullscreen mode Exit fullscreen mode

The only difference is the @. Serilog parses each template and, for the token named Order, records a capturing hint: the first is Destructuring.Default (treat as scalar), the second is Destructuring.Destructure. That hint decides the path.

The scalar path, inside Serilog's PropertyValueConverter, falls through to a ScalarValue built from order.ToString(). For a C# record, ToString() is compiler-generated, and it is generated to write each member name as a string literal:

// Order.PrintMembers, decompiled
builder.Append("CustomerName = ");
builder.Append((object)CustomerName);
builder.Append(", Total = ");
builder.Append(Total.ToString());
builder.Append(", Items = ");
builder.Append(Items.ToString());
Enter fullscreen mode Exit fullscreen mode

Those "CustomerName = " strings are ldstr loads in the #US heap. Rename the CustomerName property to b and this method still prints CustomerName = …, because the text was frozen at compile time and no longer has anything to do with the property's metadata name. The scalar log line survives renaming intact.

The destructure path does not call ToString(). It walks the object:

// Serilog.Capturing — the shape of what destructuring does
var props = type.GetPropertiesRecursive();           // public, readable instance
var members = new List<LogEventProperty>(props.Length);
foreach (var pi in props)
    members.Add(new LogEventProperty(pi.Name, Convert(pi.GetValue(value))));
return new StructureValue(members, type.Name);
Enter fullscreen mode Exit fullscreen mode

Every captured property is named after pi.Name — the current PropertyInfo.Name, read now, at runtime. After Nebula renames CustomerName to b and the Order type to a, that is exactly what reflection returns, so the StructureValue comes out with members b, c, d and a type tag of a. A JSON sink writes those as its keys. The event is complete and correct; its names are gone.

The same object, logged two ways. The scalar path reads names baked into the record's ToString(); the destructure path reads them live from metadata, so only it follows renaming.

Message templates are literals too

The third common form sidesteps objects entirely:

Log.Information("Checkout for {Customer} total {Total} x{Items}",
    order.CustomerName, order.Total, order.Items);
Enter fullscreen mode Exit fullscreen mode

The token names Customer, Total and Items live in the template string "Checkout for {Customer} …", which is a single ldstr in your method. Serilog parses that literal at runtime and pairs each token with a positional argument. Renaming the properties behind order.CustomerName changes nothing the template can see — the structured event has keys Customer, Total, Items regardless of what the backing members are called. Explicit templates are rename-proof by construction. (Nebula can still encrypt the template string; the value Serilog parses after decryption is byte-for-byte the original, so the token names are unchanged.)

So the rule is clean: token names and record ToString() are compile-time literals and survive; {@...} destructuring reflects live names and follows renaming. The same split explains the related surprises — ForContext<T>() sets SourceContext to typeof(T).FullName read at runtime, so it reports the renamed type name, and any enricher that reflects over a type to add properties reads renamed names too.

Scoping Nebula

If you want structured logs and full obfuscation, decide per object which side of the split it is on. The objects you destructure into logs are usually plain DTOs with no logic worth hiding, so keep their public member names and rename everything else. Put those shapes under one namespace:

{
  "schemaVersion": 1,
  "inputs": ["bin/Release/net8.0/Checkout.dll"],
  "outputDirectory": "protected",
  "encryptStrings": true,
  "controlFlowObfuscation": true,
  "renaming": {
    "rules": [
      { "pattern": "Checkout.Logging.Shapes.*", "action": "keep", "members": "public" },
      { "pattern": "Checkout.*",                "action": "rename" }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode
nebula protect --config nebula.config.json
Enter fullscreen mode Exit fullscreen mode

The first rule keeps the public property names on your log shapes so destructured events stay readable; members: "public" still lets those types' private fields and methods rename, and the kept types keep their control-flow obfuscation and string encryption — you surrender a few property names, not the code behind them. Everything outside that namespace — your pricing, your licensing, your domain logic — renames to a, b, c.

If you would rather keep nothing named, go the other way: log with explicit templates in the sensitive code and project objects through a chosen shape instead of reflecting the real one.

Log.Logger = new LoggerConfiguration()
    .Destructure.ByTransforming<Order>(o => new { o.Items, Total = o.Total })
    .CreateLogger();
Enter fullscreen mode Exit fullscreen mode

ByTransforming runs your lambda, so the anonymous shape's names (Items, Total) are literals the compiler emits — stable, and under your control, with the fields you don't want in logs simply left out.

When it breaks

Three edges are quieter than the main case. First, SourceContext filtering: if a sink, a sub-logger, or a log query routes on SourceContext, a renamed ForContext<T>() type name stops matching — keep the type names you filter on, or set the context with an explicit string: ForContext("SourceContext", "Checkout"). Second, source-generated logging via [LogProperties] or LoggerMessage is the opposite of destructuring: the generator bakes each property name into the generated method as a literal at compile time, so those names survive renaming exactly like a template token — no keep rule needed. Third, coverage: the rename is invisible until a given event actually fires, so assert on the protected build's log output in a smoke test, not on the debug build's, before you trust it in the field.

Takeaway

Obfuscation does not break Serilog; it rewrites the one kind of name Serilog reads at runtime. A {@Order} destructure reflects over live properties, so renaming turns its keys into b and c; a {Order} scalar, a record's ToString(), and every {Token} in a message template are compile-time literals that pass through untouched. Decide which objects belong in logs, keep just those shapes' public names, and let Nebula.NET rename the rest. Open the result in Glass.NET to confirm your logic reads as a, b, c while your log shapes still spell out the names your support team needs.

Top comments (0)