引用
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="NLog" version="4.5.2" targetFramework="net461" /> <package id="NLog.Config" version="4.5.2" targetFramework="net461" /> <package id="NLog.Schema" version="4.5.2" targetFramework="net461" /> </packages>
配置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" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c: emp log-internal.log"> <targets> <target name="log_file" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="log_file" /> </rules> </nlog>
测试
class Program { private static Logger logger = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { logger.Trace("Trace Message"); logger.Debug("Debug Message"); logger.Info("Info Message"); logger.Error("Error Message"); logger.Fatal("Fatal Message"); } }