Skip to content

Commit 45dbddf

Browse files
committed
Also emit parameterized events from EventSource
These can be collected e.g. using the `dotnet-trace` CLI tool (specify `--providers Castle.DynamicProxy`); the resulting `.nettrace` files can then be opened for analysis in e.g. Visual Studio or Perfview. See: https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-trace
1 parent c0869ed commit 45dbddf

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/Castle.Core/DynamicProxy/DynamicProxyEventSource.cs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,69 @@ private DynamicProxyEventSource()
3737
{
3838
}
3939

40+
private enum EventId
41+
{
42+
None,
43+
TypeCacheHit,
44+
TypeCacheMiss,
45+
TypeGenerated,
46+
}
47+
4048
[NonEvent]
41-
public void TypeCacheHit()
49+
public void TypeCacheHit(Type requested, Type cached)
4250
{
4351
#if FEATURE_EVENTCOUNTER
4452
Interlocked.Increment(ref typeCacheHitCount);
4553
#endif
54+
55+
if (IsEnabled())
56+
{
57+
TypeCacheHit(requested.FullName!, cached.FullName!);
58+
}
59+
}
60+
61+
[Event((int)EventId.TypeCacheHit)]
62+
private void TypeCacheHit(string requested, string cached)
63+
{
64+
WriteEvent((int)EventId.TypeCacheHit, requested, cached);
4665
}
4766

4867
[NonEvent]
49-
public void TypeCacheMiss()
68+
public void TypeCacheMiss(Type requested)
5069
{
5170
#if FEATURE_EVENTCOUNTER
5271
Interlocked.Increment(ref typeCacheMissCount);
5372
#endif
73+
74+
if (IsEnabled())
75+
{
76+
TypeCacheMiss(requested.FullName!);
77+
}
78+
}
79+
80+
[Event((int)EventId.TypeCacheMiss)]
81+
private void TypeCacheMiss(string requested)
82+
{
83+
WriteEvent((int)EventId.TypeCacheMiss, requested);
5484
}
5585

5686
[NonEvent]
57-
public void TypeGenerated()
87+
public void TypeGenerated(Type type)
5888
{
5989
#if FEATURE_EVENTCOUNTER
6090
Interlocked.Increment(ref typeGeneratedCount);
6191
#endif
92+
93+
if (IsEnabled())
94+
{
95+
TypeGenerated(type.FullName!);
96+
}
97+
}
98+
99+
[Event((int)EventId.TypeGenerated)]
100+
private void TypeGenerated(string type)
101+
{
102+
WriteEvent((int)EventId.TypeGenerated, type);
62103
}
63104

64105
#if FEATURE_EVENTCOUNTER

src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public Type GetProxyType()
7878
{
7979
notFoundInTypeCache = true;
8080
Logger.DebugFormat("No cached proxy type was found for target type {0}.", targetType.FullName);
81-
DynamicProxyEventSource.Log.TypeCacheMiss();
81+
DynamicProxyEventSource.Log.TypeCacheMiss(requested: targetType);
8282

8383
EnsureOptionsOverrideEqualsAndGetHashCode();
8484

@@ -89,7 +89,7 @@ public Type GetProxyType()
8989
if (!notFoundInTypeCache)
9090
{
9191
Logger.DebugFormat("Found cached proxy type {0} for target type {1}.", proxyType.FullName, targetType.FullName);
92-
DynamicProxyEventSource.Log.TypeCacheHit();
92+
DynamicProxyEventSource.Log.TypeCacheHit(requested: targetType, cached: proxyType);
9393
}
9494

9595
return proxyType;

src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public Type BuildType()
403403
}
404404

405405
var type = typeBuilder.CreateTypeInfo();
406-
DynamicProxyEventSource.Log.TypeGenerated();
406+
DynamicProxyEventSource.Log.TypeGenerated(type);
407407

408408
return type;
409409
}

0 commit comments

Comments
 (0)