You can easily gain access to the requested type by using StructureMap's IContext.
ObjectFactory.Configure(
x => {
x.For<ILogger>().Use( context => new TestLogger( context.ParentType ) );
} );
In the above statement, context.ParentType returns the type being created by StructureMap.
Jeremy Miller has covered this topic in a blog post as well. The article uses outdated syntax but it is still relevant: http://codebetter.com/jeremymiller/2009/01/15/using-the-build-session-in-structuremap/
If you would like to inspect all of the available properties in the IContext, you can use an anonymous function and set a breakpoint on the return line like so:
ObjectFactory.Configure(
x => {
x.For<ILogger>()
.Use( context =>
{
// Set breakpoint here and inspect the context
return new TestLogger( context.ParentType );
} );
} );
Update:
Make sure to use the .AlwaysUnique() ( which is equivalent to the older syntax of .CacheBy( InstanceScope.Unique ) ) otherwise StructureMap will cache the first request of ILogger which will result in the original logger being injected into all subsequent requests for the ILogger during a single GetInstance() call.
This can easily occur if you have nested classes that each require an ILogger so a request for ObjectFactory.GetInstance< ISomeClass >() will result in an ILogger being created for the deepest class in the hierarchy and then all other classes will receive that same instance of ILogger which is most likely what you do not want to happen.
So taking this update and Ben's tip into consideration, here's a better solution:
x.For<ILogger>()
.AlwaysUnique()
.Use( c => new TestLogger( c.ParentType ?? c.BuildStack.Current.ConcreteType ) );