Using Injectors with CInject

3 minute read

If you don't know what is CInject, I would recommend you to read this article and get the latest version of CInject from CodePlex website.

There are few injectors that are shipped with CInject

  • LogInjector - Allows injecting logging in any .NET assembly/executable even if you do not have code of the assembly/executable.  LogInjector is built on .NET 4.0 Client Profile and uses log4net for logging purpose
  • PerformanceInjector - Allows logging the time required to execute injected method in the target assemblies
  • ObjectValueInjector - Allows getting any property of arguments passed to the injected method

You can get started with directly using them without writing a single line of code.


Quick Guide to using CInject

So, to get started all you would require is to

  • Download the latest stable version of CInject and unzip it to, lets say, C:\CInject
  • Locate the assemblies (DLL) / executables (EXE) that need to be injected with along with their configuration files
  • Locate your injectors assembly (DLL) and their configuration files.
  • Open the downloaded CInject application [C\CInject\Cinject.exe]
  • Choosing the Target Assembly
    • If you are targeting a single assembly (to be injected), click on Assembly > Load Assembly (F3).  As soon as you select the assembly, CInject will try to create a tree of classes and methods in the assembly.
    • If you are targeting multiple assemblies (to be injected), click on Assembly > Select Directory (F2).  CInject will browse through DLLs and EXEs in the directory and create a tree of classes and methods in each assembly or executable
  • Choosing the Injector Assembly
    • Auto Load - If you have copied the injector assemblies into CInject directory [here, C:\CInject], they will be automatically loaded in CInject
    • Manually Load - Click on Assembly > Load Injector (F4).  This will load the injectors
  • Select all the classes/methods (in the blue panel) that you would want to inject. Then, select the LogInjector (in the yellow panel)
  • You can click on 'Add selected injectors to selected methods in target assembly'.  This will add some entries in the grid (Assembly, Method, Injector).  However, this does not mean that the assembly has been injected.  If by mistake you have selected a wrong injector or a method, you can remove them by selecting it in the grid and clicking 'Remove selected row'
  • Select Inject > Run (F5) to inject and proceed ahead to inject the target assemblies.
  • You will be prompted when the injection has been completed.  With CInject 1.4, all the required files for each injector will be automatically copied to the folder of target assembly
  • You can now execute the target assembly. This is the new assembly with selected injectors

LogInjector Configuration

You can alter the log configuration by editing LogInject.log4net.xml based on the Apache log4net configuration.

Currently, LogInjector supports DEBUG, INFO and ERROR modes

  • Debug:   Prints additional information such as calls to the destructor of the method, type of parameter to each method invoked
  • Info: Just logs the name method invoked
  • Error: Logs exceptions if LogInjector fails at any point

Apart from the method information, it logs Data-Time, Thread Id, and Assembly.  You can configure these details in the xml file accompanied with the CInject executables.

In case you would want to use any other version of .NET, you would have to rebuild the code in Visual Studio.

PerformanceInjector Configuration

PerformanceInjector also uses log4net for logging the performance of the methods.  The configuration can be mentioned in loginject.log4net.xml file.

PerformanceInjector supports only INFO level of logging.

ObjectValueInjector Configuration

ObjectValueInjector uses ObjectSearch.xml to define which property needs to be retrieved in the arguments to the injected methods.

If the defined property (in configuration) does not exist in the argument, this injector does not throw any exception.  If the defined property (in configuration) exist in the argument, injector will try to get the value of the property.  If property value is NULL, it will log <null>

ObjectValueInjector uses DEBUG level of logging