WhatIAmDoing by Ignat Andrei
Nuget / site data
Details
Info
Name: WhatIAmDoing
What I Am Doing - see what your software is doing. Add also RSCG_WhatIAmDoing_Common
Author: Ignat Andrei
NuGet: https://www.nuget.org/packages/RSCG_WhatIAmDoing/
https://www.nuget.org/packages/RSCG_WhatIAmDoing_Common/
You can find more details at https://github.com/ignatandrei/RSCG_WhatIAmDoing
Original Readme
RSCG_WhatIAmDoing
Instrument C# software on CI to find what the software is doing.
Introduction
Decide what you want to monitor ... and then monitor it.
Example
Packages
Add the following into the csproj you want to be monitores
<ItemGroup>
<!-- <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" /> -->
<PackageReference Include="RSCG_WhatIAmDoing" Version="8.2024.10201.735" />
<PackageReference Include="RSCG_WhatIAmDoing_Common" Version="8.2024.10201.735" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);RSCG_InterceptorTemplate</InterceptorsPreviewNamespaces>
</PropertyGroup>
Code
Intercept static methods
Assume that you want to monitor all calls to System.Console
//[ExposeClass(typeof(Encoding), nameof(Encoding.EncodingName))]
//[InterceptStatic("System.IO.File.*ts")]
//[InterceptStatic("System.IO.File.*")]
[InterceptStatic("System.Console.*")] // regex
//[InterceptStatic("WIAD_DemoConsole.Fib.*")]
internal class InterceptorMethodStatic : InterceptorMethodStaticBase, IInterceptorMethodStatic
{
}
Intercept instance methods
Assume that you want to monitor all calls to any method of any instance of Person
//[InterceptInstanceClass(typeof(Person),"ame")]
//[InterceptInstanceClass(typeof(Person), "parat")]
//[InterceptInstanceClass(typeof(Person), "ncodi")]
[InterceptInstanceClass(typeof(Person), ".*")] //regex
public class InterceptorMethodInstanceClass: InterceptorMethodInstanceClassBase, IInterceptorMethodInstanceClass
{
public InterceptorMethodInstanceClass()
{
}
}
See the results
var data= CachingData.Methods().ToArray();
foreach (var item in data)
{
WriteLine($"Method {item.typeAndMethodData.MethodName} from class {item.typeAndMethodData.TypeOfClass} Time: {item.StartedAtDate} state {item.State} ");
WriteLine($" =>Arguments: {item.ArgumentsAsString()}");
if ((item.State & AccumulatedStateMethod.HasResult) == AccumulatedStateMethod.HasResult)
{
WriteLine($" =>Result: {item.Result}");
}
}
More details
If you want to implement YOUR interception , implement the following interfaces
IInterceptorMethodInstanceClass
IInterceptorMethodStatic
License
MIT
About
Intercept any method in any project
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- InterceptorStatic.cs
This is the CSharp Project that references WhatIAmDoing
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!-- <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" /> -->
<PackageReference Include="RSCG_WhatIAmDoing" Version="8.2024.10201.735" />
<PackageReference Include="RSCG_WhatIAmDoing_Common" Version="8.2024.10201.735" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);RSCG_InterceptorTemplate</InterceptorsPreviewNamespaces>
</PropertyGroup>
</Project>
This is the use of WhatIAmDoing in Program.cs
using RSCG_WhatIAmDoing_Common;
Console.WriteLine("Hello, World!");
var data = CachingData.Methods().ToArray();
foreach (var item in data)
{
Console.WriteLine($"Method {item.typeAndMethodData.MethodName} from class {item.typeAndMethodData.TypeOfClass} Time: {item.StartedAtDate} state {item.State} ");
Console.WriteLine($" =>Arguments: {item.ArgumentsAsString()}");
if ((item.State & AccumulatedStateMethod.HasResult) == AccumulatedStateMethod.HasResult)
{
Console.WriteLine($" =>Result: {item.Result}");
}
}
This is the use of WhatIAmDoing in InterceptorStatic.cs
using RSCG_WhatIAmDoing;
using RSCG_WhatIAmDoing_Common;
namespace WIADDemo;
//[ExposeClass(typeof(Encoding), nameof(Encoding.EncodingName))]
[InterceptStatic("System.Console.*")] // regex
internal class InterceptorMethodStatic : InterceptorMethodStaticBase, IInterceptorMethodStatic
{
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- System.Console_WriteLine_1.cs
- System.Console_WriteLine_2.cs
- System.Console_WriteLine_3.cs
- System.Console_WriteLine_4.cs
#pragma warning disable CS1591
#pragma warning disable CS9113
namespace System.Runtime.CompilerServices{
[AttributeUsage(AttributeTargets.Method,AllowMultiple =true)]
file class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
{
}
}//end namespace
namespace RSCG_InterceptorTemplate{
static partial class SimpleIntercept
{
//replace code:Console.WriteLine("Hello, World!");
//replace code:123456789!123456789!123456789!12345
[System.Runtime.CompilerServices.InterceptsLocation(@"D:\gth\RSCG_Examples\v2\rscg_examples\WIAD\src\WIADDemo\Program.cs", 3, 9)]
//[System.Diagnostics.DebuggerStepThrough()]
public static void Intercept__WriteLine_0 ( string? value )
{
System.Collections.Generic.Dictionary<string,string?> valValues = new (){
};//end valValues
System.Collections.Generic.Dictionary<string,string?> stringValues = new() {
{ "value", value ?.ToString() } ,
};//end stringValues
System.Collections.Generic.Dictionary<string,string?>
expValues = new() {
};//end exposeValues
var x=WIADDemo.InterceptorMethodStatic .InterceptStaticMethodBefore(
$$$""""
{"IsVoid":true,"Tag":"","MethodName":"WriteLine","TypeOfClass":"System.Console"}
""""
,valValues , stringValues , expValues
)
;
try{
System.Console.WriteLine(value);
WIADDemo.InterceptorMethodStatic .InterceptMethodAfterWithoutResult(x);
}
catch(System.Exception ex){
WIADDemo.InterceptorMethodStatic .InterceptMethodException(x,ex);
throw;
}
finally{
WIADDemo.InterceptorMethodStatic .InterceptMethodFinally(x);
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
#pragma warning disable CS1591
#pragma warning disable CS9113
namespace System.Runtime.CompilerServices{
[AttributeUsage(AttributeTargets.Method,AllowMultiple =true)]
file class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
{
}
}//end namespace
namespace RSCG_InterceptorTemplate{
static partial class SimpleIntercept
{
//replace code: Console.WriteLine($"Method {item.typeAndMethodData.MethodName} from class {item.typeAndMethodData.TypeOfClass} Time: {item.StartedAtDate} state {item.State} ");
//replace code:123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!123456789!1234
[System.Runtime.CompilerServices.InterceptsLocation(@"D:\gth\RSCG_Examples\v2\rscg_examples\WIAD\src\WIADDemo\Program.cs", 8, 13)]
//[System.Diagnostics.DebuggerStepThrough()]
public static void Intercept__WriteLine_1 ( string? value )
{
System.Collections.Generic.Dictionary<string,string?> valValues = new (){
};//end valValues
System.Collections.Generic.Dictionary<string,string?> stringValues = new() {
{ "value", value ?.ToString() } ,
};//end stringValues
System.Collections.Generic.Dictionary<string,string?>
expValues = new() {
};//end exposeValues
var x=WIADDemo.InterceptorMethodStatic .InterceptStaticMethodBefore(
$$$""""
{"IsVoid":true,"Tag":"","MethodName":"WriteLine","TypeOfClass":"System.Console"}
""""
,valValues , stringValues , expValues
)
;
try{
System.Console.WriteLine(value);
WIADDemo.InterceptorMethodStatic .InterceptMethodAfterWithoutResult(x);
}
catch(System.Exception ex){
WIADDemo.InterceptorMethodStatic .InterceptMethodException(x,ex);
throw;
}
finally{
WIADDemo.InterceptorMethodStatic .InterceptMethodFinally(x);
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
#pragma warning disable CS1591
#pragma warning disable CS9113
namespace System.Runtime.CompilerServices{
[AttributeUsage(AttributeTargets.Method,AllowMultiple =true)]
file class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
{
}
}//end namespace
namespace RSCG_InterceptorTemplate{
static partial class SimpleIntercept
{
//replace code: Console.WriteLine($" =>Arguments: {item.ArgumentsAsString()}");
//replace code:123456789!123456789!123456789!123456789!123456789!123456789!12345678
[System.Runtime.CompilerServices.InterceptsLocation(@"D:\gth\RSCG_Examples\v2\rscg_examples\WIAD\src\WIADDemo\Program.cs", 9, 13)]
//[System.Diagnostics.DebuggerStepThrough()]
public static void Intercept__WriteLine_2 ( string? value )
{
System.Collections.Generic.Dictionary<string,string?> valValues = new (){
};//end valValues
System.Collections.Generic.Dictionary<string,string?> stringValues = new() {
{ "value", value ?.ToString() } ,
};//end stringValues
System.Collections.Generic.Dictionary<string,string?>
expValues = new() {
};//end exposeValues
var x=WIADDemo.InterceptorMethodStatic .InterceptStaticMethodBefore(
$$$""""
{"IsVoid":true,"Tag":"","MethodName":"WriteLine","TypeOfClass":"System.Console"}
""""
,valValues , stringValues , expValues
)
;
try{
System.Console.WriteLine(value);
WIADDemo.InterceptorMethodStatic .InterceptMethodAfterWithoutResult(x);
}
catch(System.Exception ex){
WIADDemo.InterceptorMethodStatic .InterceptMethodException(x,ex);
throw;
}
finally{
WIADDemo.InterceptorMethodStatic .InterceptMethodFinally(x);
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
#pragma warning disable CS1591
#pragma warning disable CS9113
namespace System.Runtime.CompilerServices{
[AttributeUsage(AttributeTargets.Method,AllowMultiple =true)]
file class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
{
}
}//end namespace
namespace RSCG_InterceptorTemplate{
static partial class SimpleIntercept
{
//replace code: Console.WriteLine($" =>Result: {item.Result}");
//replace code:123456789!123456789!123456789!123456789!123456789!123456
[System.Runtime.CompilerServices.InterceptsLocation(@"D:\gth\RSCG_Examples\v2\rscg_examples\WIAD\src\WIADDemo\Program.cs", 12, 17)]
//[System.Diagnostics.DebuggerStepThrough()]
public static void Intercept__WriteLine_3 ( string? value )
{
System.Collections.Generic.Dictionary<string,string?> valValues = new (){
};//end valValues
System.Collections.Generic.Dictionary<string,string?> stringValues = new() {
{ "value", value ?.ToString() } ,
};//end stringValues
System.Collections.Generic.Dictionary<string,string?>
expValues = new() {
};//end exposeValues
var x=WIADDemo.InterceptorMethodStatic .InterceptStaticMethodBefore(
$$$""""
{"IsVoid":true,"Tag":"","MethodName":"WriteLine","TypeOfClass":"System.Console"}
""""
,valValues , stringValues , expValues
)
;
try{
System.Console.WriteLine(value);
WIADDemo.InterceptorMethodStatic .InterceptMethodAfterWithoutResult(x);
}
catch(System.Exception ex){
WIADDemo.InterceptorMethodStatic .InterceptMethodException(x,ex);
throw;
}
finally{
WIADDemo.InterceptorMethodStatic .InterceptMethodFinally(x);
}
}
}//end class
}//namespace RSCG_InterceptorTemplate
Usefull
Download Example (.NET C# )
Share WhatIAmDoing
https://ignatandrei.github.io/RSCG_Examples/v2/docs/WhatIAmDoing