我想在文件中保存Nlog错误。如何在我的方法中使用NLog.config? 将存档文件夹下的日志错误保存为
2010-06-05.log
2010-06-06.log
2010-06-07.log
2010-06-08.log
...
中的NLOG.config
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="File"
layout="${longdate} ${logger} ${message}"
fileName="${basedir}/${shortdate}.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
在我班上;
namespace MyNamespace
{
public class MyClass
{
public void Method()
{
/* How do I save errors to an NLog file in here ?? */
}
}
}
答案 0 :(得分:1)
您应该实例化Logger instance
(通常每个类一个)。然后,您可以调用各种日志记录方法,例如Error()
,Warning()
,Info()
,Debug()
,以适当的级别记录您想要的任何内容。
namespace MyNamespace
{
using NLog;
public class MyClass
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public void Method()
{
Logger.Error("My error message");
}
}
}