Nlog的使用教程
Nuget包引入:
手动生成配置文件: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"
autoReload="true"
throwExceptions="false"
internalLogLevel="Trace">
<variable name="fileFormat"
value="
${newline}date: ${date}
${newline}level: ${level}
${newline}logger: ${logger}
${newline}machinename: ${machinename}
${newline}message: ${message}
${newline}------------------------------------------------------------" />
<targets>
<target name="logfile"
xsi:type="File"
maxArchiveFiles="1"
layout="${fileFormat}"
archiveAboveSize="102400000"
fileName="${basedir}/Logs/${date:format=yyyy-MM}/${shortdate}.log" />
</targets>
<!--写入到文件-->
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Trace" writeTo="file"/>
</rules>
</nlog>
基本的配置就这么点,十分的简单,最主要的代码段就是<target>标签内的
控制台的部分代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Programs
{
static void Main(string[] args)
{
Console.WriteLine("This is my Demo");
NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
logger.Fatal("发生致命错误");
logger.Warn("警告信息");
Console.ReadKey();
}
}
}
测试结果: