Nuget包的引用
NLog.Extensions.Logging
创建nlog.config配置文件
同样适用于Linux环境,将在当前执行目录下的logs目录中打印日志
<?xml version="1.0" encoding="utf-8" ?> <!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema--> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogFile="/logs/console-example-internal.log" internalLogLevel="Info" > <!-- the targets to write to --> <targets> <!-- write logs to file --> <target xsi:type="File" name="target1" fileName="${basedir}/logs/${shortdate}/console-example.log" layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" /> <target xsi:type="Console" name="target2" layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" /> </targets> <!-- rules to map from logger name to target --> <rules> <logger name="*" minlevel="Trace" writeTo="target1,target2" /> </rules> </nlog>View Code
使用
var logger = LogManager.GetLogger("*"); logger.Info("这个信息"); logger.Debug("这个调试"); logger.Error("这个是错误"); LogManager.Shutdown();View Code