[ninject] Configuring nlog

559 views
Skip to first unread message

Scooter

unread,
May 13, 2010, 1:03:42 PM5/13/10
to ninject
Hi,

I'm new to ninject and nlog. I'm trying to figure out how to setup
logging.

I downloaded the extension, but there's not a word about how to use
it. I referenced the DLL in my project and now I don't know how to get
it working.

What I want to do is start by logging everything that happens without
having to call a log method myself, and once that's working, log based
on calls I do myself.

I'd like to configure logging using the fluent interface for now -
I'll figure out the config file later.

Can someone give me some direction on getting this working?

Thanks -Sc00ter

--
You received this message because you are subscribed to the Google Groups "ninject" group.
To post to this group, send email to nin...@googlegroups.com.
To unsubscribe from this group, send email to ninject+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ninject?hl=en.

Tom Rathbone

unread,
May 13, 2010, 1:14:15 PM5/13/10
to nin...@googlegroups.com
Add Nlog, Ninject.Extensions.Logging and
Ninject.Extensions.Logging.NLog to your project.

Add something like this to your app.config:

<configSections>4
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="console" xsi:type="Console"
layout="${date:format=HH\:mm\:ss} ${level:uppercase=true} ${logger}
${message} ${exception:format=message,type,stacktrace}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="console" />
</rules>
</nlog>

Inject ILogger into your classes:

using Ninject.Extensions.Logging;
public class HelloWorld
{
private ILogger _log;
public HelloWorld(ILogger log)
{
_log = log;
}

public void SayHello()
{
_log.Info("Hello, World!");
}
}

ILogger is injected automatically by the Ninject kernel when the
object is created:

var kernel = new StandardKernel();
var hello = kernel.Get<HelloWorld>();
hello.SayHello();

Hope that helps.

T.

Tom Rathbone

unread,
May 13, 2010, 1:21:55 PM5/13/10
to nin...@googlegroups.com
P.S. NLog is a logging framework as opposed to a tracing tool, if you
want to automatically log method calls or some such you'll need to
find a way to instrument them with logging. The Ninject interception
extension might go some way to enabling this but wouldn't help in a
bunch of cases (sealed classes/methods etc), and would only allow you
to instrument calls on classes created by Ninject.

Scooter

unread,
May 13, 2010, 1:52:46 PM5/13/10
to ninject
Worked perfectly - thanks!

And I understand your point about tracing, makes sense.
> > For more options, visit this group athttp://groups.google.com/group/ninject?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "ninject" group.
> To post to this group, send email to nin...@googlegroups.com.
> To unsubscribe from this group, send email to ninject+u...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/ninject?hl=en.- Hide quoted text -
>
> - Show quoted text -

Scooter

unread,
May 13, 2010, 2:03:35 PM5/13/10
to ninject
Can I configure logging using the fluent interface - maybe using a
Provider (Ninject.Activation.Provider)?

I understand the underlying issues - I just want to see how it can be
done.

Thanks -sc00ter

On May 13, 1:14 pm, Tom Rathbone <tom.rathb...@gmail.com> wrote:
> > For more options, visit this group athttp://groups.google.com/group/ninject?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "ninject" group.
> To post to this group, send email to nin...@googlegroups.com.
> To unsubscribe from this group, send email to ninject+u...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/ninject?hl=en.- Hide quoted text -
>
> - Show quoted text -

Tom Rathbone

unread,
May 13, 2010, 2:49:41 PM5/13/10
to nin...@googlegroups.com
I'm not sure how fluent the API is but you can access and configure
NLog directly through the static methods on NLog.LogManager

T.

Scooter

unread,
May 13, 2010, 4:22:32 PM5/13/10
to ninject
Thanks - I'm trying it now, but I think I'm missing something with the
factory. I can configure NLog as you suggest, but hooking it into
ninject is not as straight-forward. I found some old articles around,
but things have changed since then.

I'll keep working on it -- thanks again!
> >> For more options, visit this group athttp://groups.google.com/group/ninject?hl=en.-Hide quoted text -
Reply all
Reply to author
Forward
0 new messages