How to log each instance of a Class into different files with Nlog?

时间:2015-07-08 15:43:58

标签: c# wpf nlog

I have a project where I am testing eight pieces of the same equipment at the same time. I need to be able to write to a log for each piece of equipment. I also need to have another log file that has system log messages. In the past I have written my own logging but would like to try Nlog. I wrote a simple test to see if this would work and I am not having much success. What I would like to do is have each instance of TempObject log into its own file. Currently the following code logs into each of the log files:

NLog.Config

<?xml version="1.0" encoding="utf-8" ?>
<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="logfile.txt" />
    <target name="myObj1" xsi:type="File" fileName="myObj1.txt" />
    <target name="myObj2" xsi:type="File" fileName="myObj2.txt" />
    <target name="myObj3" xsi:type="File" fileName="myObj3.txt" />
    <target name="myObj4" xsi:type="File" fileName="myObj4.txt" />
    <target name="console" xsi:type="Console" />
  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile" />
    <logger name="*" minlevel="Info" writeTo="myObj1" />
    <logger name="*" minlevel="Info" writeTo="myObj2" />
    <logger name="*" minlevel="Info" writeTo="myObj3" />
    <logger name="*" minlevel="Info" writeTo="myObj4" />
    <logger name="*" minlevel="Info" writeTo="console" />
  </rules>
</nlog>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
  private static Logger logger = LogManager.GetCurrentClassLogger();

  private TempObject myObj1 = new TempObject(1);
  private TempObject myObj2 = new TempObject(2);
  private TempObject myObj3 = new TempObject(3);
  private TempObject myObj4 = new TempObject(4);

  public MainWindow()
  {
    InitializeComponent();
    logger.Info("this is the first log message");        
    myObj1.LogMessage();
    myObj2.LogMessage();
    myObj3.LogMessage();
    myObj4.LogMessage();
    SetUpDisplay();
  }
}

TempObject.cs

public class TempObject
{
  private int myUUT;

  private static Logger logger = LogManager.GetCurrentClassLogger();

  public TempObject(int uutNumber)
  {
    myUUT = uutNumber;
  }

  public void LogMessage()
  {
    logger.Info("UUT" + myUUT);
  }
}

1 个答案:

答案 0 :(得分:3)

我必须在TempObject中更改以下内容并且它可以正常工作。

public class TempObject
{
  private int myUUT;

  private Logger myLogger;

  public TempObject(int uutNumber)
  {
    myUUT = uutNumber;
    string logString = "myObj" + myUUT;
    myLogger = LogManager.GetLogger(logString);
  }

  public void LogMessage(string msg)
  {
    myLogger.Info("UUT" + myUUT+ "|" + msg);
  }
}

和配置文件的规则部分:

<rules>
  <logger name="*" minlevel="Info" writeTo="logfile" />
  <logger name="myObj1" minlevel="Info" writeTo="myObj1" />
  <logger name="myObj2" minlevel="Info" writeTo="myObj2" />
  <logger name="myObj3" minlevel="Info" writeTo="myObj3" />
  <logger name="myObj4" minlevel="Info" writeTo="myObj4" />
  <logger name="*" minlevel="Info" writeTo="console" />
</rules>