using NLog; private static Logger logger = LogManager.GetCurrentClassLogger(); //初始化日志类
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:\temp\nlog-internal.log"> <variable name="logDirectory" value="${basedir}/logs"/> <variable name="traceVal" value="${date:format=HH\:mm\:ss} [Thread-${threadname:whenEmpty=${threadid}}]|${message}"/> <variable name="layoutVal" value="${date:format=HH\:mm\:ss} [Thread-${threadname:whenEmpty=${threadid}}]|${uppercase:${level}}|${callsite:fileName=True}${newline}${message}${newline}${exception}"/> <variable name="consoleVal" value="${date:format=HH\:mm\:ss}|${pad:padding=5:inner=${level:uppercase=true}}|${message}"/> <targets> <target name="console" xsi:type="ColoredConsole" useDefaultRowHighlightingRules="false" layout="${consoleVal}" > <highlight-row condition="level == LogLevel.Debug" foregroundColor="DarkGray" /> <highlight-row condition="level == LogLevel.Info" foregroundColor="Gray" /> <highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" /> <highlight-row condition="level == LogLevel.Error" foregroundColor="Red" /> <highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White" /> </target> <target xsi:type="File" name="logfile" fileName="${logDirectory}/info_${shortdate}.log" keepFileOpen="false" layout="${layoutVal}" /> <target xsi:type="File" name="warnfile" fileName="${logDirectory}/warn_${shortdate}.log" keepFileOpen="false" layout="${layoutVal}" /> <target xsi:type="File" name="errfile" fileName="${logDirectory}/error_${shortdate}.log" keepFileOpen="false" layout="${layoutVal}" /> <target xsi:type="File" name="debugfile" fileName="${logDirectory}/debug_${shortdate}.log" keepFileOpen="false" layout="${layoutVal}" /> <target xsi:type="File" name="tracefile" fileName="${logDirectory}/trace_${shortdate}.log" keepFileOpen="false" layout="${traceVal}" /> </targets> <rules> <logger name="*" writeTo="console" /> <logger name="*" level="Trace" writeTo="tracefile"/> <logger name="*" level="Error" writeTo="errfile" /> <logger name="*" level="Warn" writeTo="warnfile"/> <logger name="*" minlevel="Info" writeTo="logfile"/> <logger name="*" minlevel="Debug" writeTo="debugfile" /> </rules> </nlog>