-
Notifications
You must be signed in to change notification settings - Fork 83
UsingAnIoCPattern
mbrit edited this page Sep 17, 2012
·
1 revision
After referencing the MetroLog and Metrolog.platform libraries, using MetroLog is very easy. Just obtain a reference to the ILogManager
with LogManagerFactory.DefaultLogManager
. If you're doing your own IoC, you can alternatively use LogManagerFactory.CreateLogManager()
if you don't want to use the default instance.
Once you have an ILogManager
, you can call GetLogger
on it to get an instance for each Type
to use.
For those using DI or an IoC container, you can inject ILogManager
by registering the value of the DefaultLogManager
property
in your container setup. This is the recommended approach instead of using LogManagerFactory.DefaultLogManager
thoughout your code.
###Example
using System;
using MetroLog;
class LogExample
{
static void Main(string[] args)
{
ILogManager logManager = LogManagerFactory.DefaultLogManager;
// Inject the ILogManager manually
SomeMagicClass c = new SomeMagicClass(logManager);
c.DoMagic();
}
}
class SomeMagicClass
{
private readonly ILogger _log;
public SomeMagicClass(ILogManager logManager)
{
_log = logManager.GetLogger<SomeMagicClass>();
}
public void DoMagic()
{
// Log something interesting
_log.Info("We are about to do magic!");
}
}