Skip to main content

WhatIAmDoing by Ignat Andrei

Nuget / site data

NugetNuget GitHub last commit GitHub Repo stars

Details

Info

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

Source : https://github.com/ignatandrei/RSCG_WhatIAmDoing

Original Readme

note

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

note

Intercept any method in any project

How to use

Example ( source csproj, source files )

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>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

#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


Usefull

Download Example (.NET C# )

Share WhatIAmDoing

https://ignatandrei.github.io/RSCG_Examples/v2/docs/WhatIAmDoing

In the same category (AOP) - 0 other generators