Whist trying to work out if there is any way to get around the problem I am suffering with Sharepoint workflows idling inside a Typemock Isolator test harness I have been having a good look at CThru; a set if libraries for Typemock that, and I quote Codeplex here, ‘… allows creating interception and isolation frameworks for logging, testing and many other things very simply’. This is the framework is used to create the Silverlight mocking frame work on the same Codeplex site.
To aid my analysis I wrote a basic Logger using the Aspect concepts of CThru, which I call as follows:
// set the name of the types I want to monitor
TestProject.LoggingAspect.TypeNamesToMatch.Add(“SharePoint”);
// tell it where to look for aspects
CThru.CThruEngine.AddAspectsInAssembly(System.Reflection.Assembly.GetExecutingAssembly());
// and start it up
CThru.CThruEngine.StartListening();
The source is below is just included in my assembly, it allow me to chose if I want to log as text for CSV format. I am sure it will need editing for your logging needs but it gives you the basic idea….
using System;
using CThru;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
namespace TestProject
{
/// <summary>
/// A sample Aspect logger for CThru
/// </summary>
class LoggingAspect : Aspect
{
/// <summary>
/// The current logger in use
/// </summary>
private static IAspectLogger logger = new DebugTextLogger() ;
/// <summary>
/// A list of the available logging formats
/// </summary>
public enum LoggingMethod
{
TextToDebug,
CommaSeparatedToDebug
}
```
```
/// <summary>
/// The list of string to do partial matches against when logging
/// If any string in this list is in the namespace or typename it gets logged
/// If this list is empty then all types are logged
/// </summary>
public static List<string\> TypeNamesToMatch = new List<string\>();
/// <summary>
/// Sets the current logging format
/// </summary>
public static LoggingMethod CurrentLoggingMethod
{
set
{
switch (value)
{
default:
case LoggingMethod.TextToDebug:
logger = new DebugTextLogger();
break;
case LoggingMethod.CommaSeparatedToDebug:
logger = new DebugCVSLogger();
break;
}
}
}
public static bool LogToCSV = false;
public override void StaticConstructorBehavior(DuringCallbackEventArgs e)
{
LogEvent("LoggingAspect.StaticConstructorBehavior", e);
}
```
```
public override void ConstructorBehavior(DuringCallbackEventArgs e)
{
LogEvent("LoggingAspect.ConstructorBehavior", e);
}
public override void MethodBehavior(DuringCallbackEventArgs e)
{
LogEvent("LoggingAspect.MethodBehavior", e);
if (e.MethodName == "StsCompareStrings")
{
e.MethodBehavior = MethodBehaviors.ReturnsCustomValue;
e.ReturnValueOrException = true;
}
}
public override void MissingMethodBehavior(DuringCallbackEventArgs e)
{
LogEvent("LoggingAspect.MissingMethodBehavior", e);
}
```
```
private static void LogEvent(string description, DuringCallbackEventArgs e)
{
logger.LogEvent(description, e);
}
/// <summary>
/// The control to see which
/// </summary>
/// <param name="info">The info on the currently handled assembly</param>
/// <returns>True if we should monitor this event</returns>
public override bool ShouldIntercept(InterceptInfo info)
{
if (TypeNamesToMatch.Count > 0)
{
foreach (string name in TypeNamesToMatch)
{
// find the first match of this string in a namespace typename
if (info.TypeName.Contains(name) == true)
{
return true;
}
}
}
else
{
// none in the list match all
return true;
}
return false;
}
/// <summary>
/// Helper method to format the parameters as a list in a string
/// </summary>
/// <param name="e">The handled event</param>
/// <returns>A strung listing the params and their values</returns>
public static string ParametersListToString(DuringCallbackEventArgs e)
{
var sb = new StringBuilder();
if (e.ParameterValues != null)
{
for (int i = 0; i < e.ParameterValues.Length; i++)
{
```
```
if (e.ParameterValues\[i\] != null)
{
sb.Append(String.Format("{0} \[{1}\]", e.ParameterValues\[i\].GetType(), e.ParameterValues\[i\]));
}
else
{
sb.Append("null");
}
if (i < e.ParameterValues.Length - 1)
{
sb.Append(",");
}
}
}
return sb.ToString();
}
}
/// <summary>
/// Logger interface
/// </summary>
public interface IAspectLogger
{
void LogEvent(string description, DuringCallbackEventArgs e);
}
/// <summary>
/// Logs an items as plain text
/// </summary>
public class DebugTextLogger : IAspectLogger
{
public void LogEvent(string description, DuringCallbackEventArgs e)
{
Debug.WriteLine(string.Format("{0}: {1}{2}.{3}({4})",
description,
e.TargetInstance == null ? "\[Static\] " : string.Empty,
e.TypeName,
e.MethodName,
LoggingAspect.ParametersListToString(e)));
}
}
/// <summary>
/// Logs an items as comma separated to ease analysis
/// </summary>
public class DebugCVSLogger : IAspectLogger
{
public DebugCVSLogger()
{
// write out a header so we know the colomns
Debug.WriteLine(string.Format("{0},{1},{2},{3},{4}",
"Event logged",
"Is Static",
"Type name",
"Method name",
"Parameter List...."));
}
```
```
public void LogEvent(string description, DuringCallbackEventArgs e)
{
Debug.WriteLine(string.Format("{0},{1},{2},{3},{4}",
description,
e.TargetInstance == null ? "True" : "False",
e.TypeName,
e.MethodName,
LoggingAspect.ParametersListToString(e)));
}
}
}