1

Hi i try use Nlog in with caliburn micro, I have use this tutorial http://buksbaum.us/2010/08/08/how-to-do-logging-with-caliburn-micro/.

Firstt I defined Nloagger class, here is it:

public class NLogLogger : ILog 
{ 
    #region Fields 

    private readonly NLog.Logger _innerLogger; 

    #endregion 

    #region Constructors 

    public NLogLogger(Type type) 
    { 
        _innerLogger = NLog.LogManager.GetLogger(type.Name); 
    } 
    #endregion 

   #region ILog Members 

    public void Error(Exception exception) 
    { 
        _innerLogger.ErrorException(exception.Message, exception); 
    } 

    public void Info(string format, params object[] args) 
    { 
        _innerLogger.Info(format, args); 
    } 

    public void Warn(string format, params object[] args) 
    { 
        _innerLogger.Warn(format, args); 
    } 
    #endregion

}

That I modified MEF bootraper:

#region Constructors

    public MefBootStrapper()
        : base()
    {
        _msgBox = new MessageBoxes();
        _doHandle = true;
    }

    static MefBootStrapper()
    {
        LogManager.GetLog = type => new NLogLogger(type);
    }


#endregion

and last modified app.config

<?xml version="1.0"?>

<configuration>

  <configSections>
    <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="logfile" xsi:type="File" fileName="file.txt" />
    </targets>

    <rules>
      <logger name="*" minlevel="Error" writeTo="logfile" />
    </rules>
  </nlog>


</configuration>

It is really stupid for me but I dont know how to use now logger in View model class and second I would like to know if it is possible log with NLog to XML files.

Thank you for support

2 Answers2

0

To use a logger, you need to ask for one. You can do this by requesting a logger instance in the Constructor (explicit) or property (implicit), or even ask the log manager to give it to you.

Here's what you can do in your ViewModel:

public void MyViewModel 
{
    public MyViewModel()
    {
        Caliburn.Core.Logging.ILog logger = Caliburn.Core.Logging.LogManager.GetLog(typeof(MyViewModel));
        logger.Info("Something Happened");
    }
}

Alternatively you can inject a ILog instance (via constructor or property), if you have already registered one in the container you chose with Caliburn. I use Castle Windsor, so the registration part would look like this:

container.Register(Component.For<ILog>().ImplementedBy<NLogLogger>().LifeStyle.Transient);

And you can just ask for a logger in your constructor:

public void MyViewModel 
{
    public MyViewModel(ILog logger)
    {
        logger.Info("Something Happened");
    }
}
Hadi Eskandari
  • 25,575
  • 8
  • 51
  • 65
0

I realize that an answer has been accepted for this question, but I thought that I would mention that your NLog wrapper could be written better. As it is implemented right now, you will not get the correct information if you turn on the logging of callsite information (the class/method making the logging call). Rather than getting the callsite from where you are calling your NLog wrapper, you will get the callsite where the wrapper calls NLog.

Take a look at my answer to the following question for the correct way to write an NLog wrapper (correct in the sense that the callsite information is preserved).

How to retain callsite information when wrapping NLog

The question asks how to write a wrapper for NLog with a particular pattern (his Log method gets the NLog logger and then calls the appropriate Info, Warn, Error, etc method). This pattern is a little bit unorthodox (getting the logger during every call). The more typical pattern, which you are doing, is to get the NLog logger in your constructor. See the following link for an example of how to write an NLog wrapper in this pattern that preserves the callsite information:

Nlog Callsite is wrong when wrapper is used

Note that a very similar technique is required if you want to write a wrapper for log4net.

Good luck!

Community
  • 1
  • 1
wageoghe
  • 27,390
  • 13
  • 88
  • 116