log4net Factory Registration

3,644 views
Skip to first unread message

Matt Burton

unread,
Sep 4, 2009, 2:17:01 PM9/4/09
to aut...@googlegroups.com
We're trying to standardize how we're doing our interaction with
log4net and we'd like to do it via constructor injection to make it an
explicit dependency. The problem is that it uses a factory method to
create a logger for a given type, but Autofac doesn't supply enough
information in the context supplied at runtime to be able to pull it
off. We found this solution that someone had posted:

http://stackoverflow.com/questions/1159212/automatic-factory-with-common-logging-and-autofac

It works, but that's just nasty. Are there any other solutions given
the capabilities of Autofac?

Thanks,
Matt

bling

unread,
Sep 4, 2009, 2:39:06 PM9/4/09
to Autofac
This should work.

string loggerName = typeof(MyService).Name + ".Log";
b.Register(x => LogManager.GetLogger(typeof(MyService))).Named
(loggerName);
b.Register<MyService>().WithArguments(new ResolvedParameter((p, i) =>
p.ParameterType == typeof(ILog), (p, i) => i.Resolve<ILog>
(loggerName)));

On Sep 4, 11:17 am, Matt Burton <matt.bur...@gmail.com> wrote:
> We're trying to standardize how we're doing our interaction with
> log4net and we'd like to do it via constructor injection to make it an
> explicit dependency. The problem is that it uses a factory method to
> create a logger for a given type, but Autofac doesn't supply enough
> information in the context supplied at runtime to be able to pull it
> off. We found this solution that someone had posted:
>
> http://stackoverflow.com/questions/1159212/automatic-factory-with-com...

Matt Burton

unread,
Sep 4, 2009, 3:05:53 PM9/4/09
to aut...@googlegroups.com
Thanks for the reply. That definitely works on a case by case basis,
i.e. register *this* type using *this* logger, but the eventual goal
is to come up with something generic so that I can register N services
that all take a dependency on ILog, and have Autofac do the right
thing supplying a logger instance for each type that declares the
dependency. Any ideas there?

bling

unread,
Sep 4, 2009, 3:41:17 PM9/4/09
to Autofac
Hmmmm....try this then:

public class LoggingModule : IModule
{
public void Configure(IContainer container)
{
container.ComponentRegistered += (sender, e) =>
e.ComponentRegistration.Preparing += OnComponentPreparing;
}

private static void OnComponentPreparing(object sender,
PreparingEventArgs e)
{
Type t =
e.Component.Descriptor.BestKnownImplementationType;
e.Parameters = e.Parameters.Union(new[]
{
new ResolvedParameter((p, i) => p.ParameterType ==
typeof(ILog), (p, i) => LogManager.GetLogger(t))
});
}
}

b.RegisterModule(new LoggingModule());

On Sep 4, 12:05 pm, Matt Burton <matt.bur...@gmail.com> wrote:
> Thanks for the reply. That definitely works on a case by case basis,
> i.e. register *this* type using *this* logger, but the eventual goal
> is to come up with something generic so that I can register N services
> that all take a dependency on ILog, and have Autofac do the right
> thing supplying a logger instance for each type that declares the
> dependency. Any ideas there?
>

Matt Burton

unread,
Sep 4, 2009, 5:06:26 PM9/4/09
to aut...@googlegroups.com
Excellent - works perfectly! Thanks a million - most appreciated.

Thanks,
Matt

minhnh...@gmail.com

unread,
Feb 7, 2017, 6:23:33 AM2/7/17
to Autofac
Thanks for your help, It works well at almost cases (via constructors and properties). But at the first time (no any log files created) I tried logging in OWinMiddleWare I used
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

It can't create any log files but if I run some actions in controller (create log files, logging at OWinMiddleWare will work well). Any ideas?
Reply all
Reply to author
Forward
0 new messages