我尝试将NLog插入我的项目,并且第一次这样做,代码如下:
static class Program
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private static void Main(string[] args)
{
logger.Trace("Enter Main");
MyClass.DoWork();
logger.Trace("Exit Main");
}
class MyClass
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public static void DoWork()
{
logger.Trace("Enter DoWork");
var mgc = new MyGreatClass();
var task = mgc.RunAsync(...);
logger.Trace("Exit DoWork");
}
}
class MyGreatClass
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
async Task<bool> RunAsync()
{
logger.Trace("Log something");
await DoSomethig();
}
}
Nlog.config
文件如下所示:
<targets>
<target name="file" xsi:type="File" fileName="${basedir}/logdata${date:format=HH-mm-ss}.log"
layout="${date:format=HH\:mm\:ss}|${message}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
但是,在记录它时会创建3个不同的日志文件,如何使Nlog只创建并登录一个文件?在运行一个应用程序时创建许多日志文件是一种好习惯吗?