Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Breaking Changes:

Enhancements:
- Minimally improved support for methods having `ref struct` parameter and return types, such as `Span<T>`: Intercepting such methods caused the runtime to throw `InvalidProgramException` and `NullReferenceException` due to forbidden conversions of `ref struct` values when transferring them into & out of `IInvocation` instances. To prevent these exceptions from being thrown, such values now get replaced with `null` in `IInvocation`, and with `default` values in return values and `out` arguments. When proceeding to a target, the target methods likewise receive such nullified values. (@stakx, #665)
- A new `System.Diagnostics.Tracing.EventSource` named _Castle.DynamicProxy_ publishes DynamicProxy events and metrics. Those can be inspected using tools such as [`dotnet-trace`](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-trace) (for events) and [`dotnet-counters`](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-counters) (for metrics). (@stakx, #717)
- Dependencies were updated

Bugfixes:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ Symbol | .NET 4.6.2 | .NET Standard 2.0 | .
`FEATURE_APPDOMAIN` | :white_check_mark: | :no_entry_sign: | :no_entry_sign:
`FEATURE_ASSEMBLYBUILDER_SAVE` | :white_check_mark: | :no_entry_sign: | :no_entry_sign:
`FEATURE_BYREFLIKE` | :no_entry_sign: | :no_entry_sign: | :white_check_mark:
`FEATURE_EVENTCOUNTER` | :no_entry_sign: | :no_entry_sign: | :white_check_mark:
`FEATURE_SERIALIZATION` | :white_check_mark: | :no_entry_sign: | :no_entry_sign:
`FEATURE_SYSTEM_CONFIGURATION` | :white_check_mark: | :no_entry_sign: | :no_entry_sign:

* `FEATURE_APPDOMAIN` - enables support for features that make use of an AppDomain in the host.
* `FEATURE_ASSEMBLYBUILDER_SAVE` - enabled support for saving the dynamically generated proxy assembly.
* `FEATURE_BYREFLIKE` - enables support for by-ref-like (`ref struct`) types such as `Span<T>` and `ReadOnlySpan<T>`.
* `FEATURE_EVENTCOUNTER` - enables support for emitting `EventCounter`-based metrics to an `EventSource` named **Castle.DynamicProxy**.
* `FEATURE_SERIALIZATION` - enables support for serialization of dynamic proxies and other types.
* `FEATURE_SYSTEM_CONFIGURATION` - enables features that use System.Configuration and the ConfigurationManager.
4 changes: 4 additions & 0 deletions buildscripts/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
<DefineConstants>$(DefineConstants);FEATURE_BYREFLIKE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='net8.0' Or '$(TargetFramework)'=='net9.0'">
<DefineConstants>$(DefineConstants);FEATURE_EVENTCOUNTER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<None Include="$(SolutionDir)docs\images\castle-logo.png" Pack="true" PackagePath=""/>
</ItemGroup>
Expand Down
131 changes: 131 additions & 0 deletions src/Castle.Core/DynamicProxy/DynamicProxyEventSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable

namespace Castle.DynamicProxy
{
using System;
using System.Diagnostics.Tracing;
#if FEATURE_EVENTCOUNTER
using System.Threading;
#endif

[EventSource(Name = "Castle.DynamicProxy")]
internal sealed class DynamicProxyEventSource : EventSource
{
public static DynamicProxyEventSource Log { get; } = new DynamicProxyEventSource();

#if FEATURE_EVENTCOUNTER
private int typeCacheHitCount;
private int typeCacheMissCount;
private int typeGeneratedCount;
#endif

private DynamicProxyEventSource()
{
}

private enum EventId
{
None,
TypeCacheHit,
TypeCacheMiss,
TypeGenerated,
}

[NonEvent]
public void TypeCacheHit(Type requested, Type cached)
{
#if FEATURE_EVENTCOUNTER
Interlocked.Increment(ref typeCacheHitCount);
#endif

if (IsEnabled())
{
TypeCacheHit(requested.FullName!, cached.FullName!);
}
}

[Event((int)EventId.TypeCacheHit)]
private void TypeCacheHit(string requested, string cached)
{
WriteEvent((int)EventId.TypeCacheHit, requested, cached);
}

[NonEvent]
public void TypeCacheMiss(Type requested)
{
#if FEATURE_EVENTCOUNTER
Interlocked.Increment(ref typeCacheMissCount);
#endif

if (IsEnabled())
{
TypeCacheMiss(requested.FullName!);
}
}

[Event((int)EventId.TypeCacheMiss)]
private void TypeCacheMiss(string requested)
{
WriteEvent((int)EventId.TypeCacheMiss, requested);
}

[NonEvent]
public void TypeGenerated(Type type)
{
#if FEATURE_EVENTCOUNTER
Interlocked.Increment(ref typeGeneratedCount);
#endif

if (IsEnabled())
{
TypeGenerated(type.FullName!);
}
}

[Event((int)EventId.TypeGenerated)]
private void TypeGenerated(string type)
{
WriteEvent((int)EventId.TypeGenerated, type);
}

#if FEATURE_EVENTCOUNTER
protected override void OnEventCommand(EventCommandEventArgs command)
{
if (command.Command == EventCommand.Enable)
{
_ = new PollingCounter("castle.dynamic_proxy.type_cache_hit.count", this, () => typeCacheHitCount)
{
DisplayName = "Type cache hits",
DisplayUnits = "count",
};

_ = new PollingCounter("castle.dynamic_proxy.type_cache_miss.count", this, () => typeCacheMissCount)
{
DisplayName = "Type cache misses",
DisplayUnits = "count",
};

_ = new PollingCounter("castle.dynamic_proxy.type_generated.count", this, () => typeGeneratedCount)
{
DisplayName = "Types generated",
DisplayUnits = "count",
};
}
}
#endif
}
}
2 changes: 2 additions & 0 deletions src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public Type GetProxyType()
{
notFoundInTypeCache = true;
Logger.DebugFormat("No cached proxy type was found for target type {0}.", targetType.FullName);
DynamicProxyEventSource.Log.TypeCacheMiss(requested: targetType);

EnsureOptionsOverrideEqualsAndGetHashCode();

Expand All @@ -88,6 +89,7 @@ public Type GetProxyType()
if (!notFoundInTypeCache)
{
Logger.DebugFormat("Found cached proxy type {0} for target type {1}.", proxyType.FullName, targetType.FullName);
DynamicProxyEventSource.Log.TypeCacheHit(requested: targetType, cached: proxyType);
}

return proxyType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
private GenericTypeParameterBuilder[] genericTypeParams;

internal const TypeAttributes DefaultTypeAttributes =
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Serializable;

Check warning on line 47 in src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs

View workflow job for this annotation

GitHub Actions / Build and test (macos-latest)

'TypeAttributes.Serializable' is obsolete: 'Formatter-based serialization is obsolete and should not be used.' (https://aka.ms/dotnet-warnings/SYSLIB0050)

Check warning on line 47 in src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest)

'TypeAttributes.Serializable' is obsolete: 'Formatter-based serialization is obsolete and should not be used.' (https://aka.ms/dotnet-warnings/SYSLIB0050)

Check warning on line 47 in src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest)

'TypeAttributes.Serializable' is obsolete: 'Formatter-based serialization is obsolete and should not be used.' (https://aka.ms/dotnet-warnings/SYSLIB0050)

public ClassEmitter(TypeBuilder typeBuilder)
{
Expand Down Expand Up @@ -195,7 +195,7 @@

if (!serializable)
{
atts |= FieldAttributes.NotSerialized;

Check warning on line 198 in src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs

View workflow job for this annotation

GitHub Actions / Build and test (macos-latest)

'FieldAttributes.NotSerialized' is obsolete: 'Formatter-based serialization is obsolete and should not be used.' (https://aka.ms/dotnet-warnings/SYSLIB0050)

Check warning on line 198 in src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest)

'FieldAttributes.NotSerialized' is obsolete: 'Formatter-based serialization is obsolete and should not be used.' (https://aka.ms/dotnet-warnings/SYSLIB0050)

Check warning on line 198 in src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest)

'FieldAttributes.NotSerialized' is obsolete: 'Formatter-based serialization is obsolete and should not be used.' (https://aka.ms/dotnet-warnings/SYSLIB0050)
}

return CreateField(name, fieldType, atts);
Expand Down Expand Up @@ -403,6 +403,7 @@
}

var type = typeBuilder.CreateTypeInfo();
DynamicProxyEventSource.Log.TypeGenerated(type);

return type;
}
Expand Down
Loading