NLog安装:
Install-Package NLog
NLog配置:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="nlog" type="NLog.Config.ConfigSectionHandler,NLog" /> </configSections> <!--autoReload="true"表示在不重新启动应用程序的情况下,修改配置文件,NLog会自动加载应--> <!--NLog内部的日志消息写到应用程序目录下的logs文件夹里的internalLog.txt文件中;(--> <nlog autoReload="true" internalLogLevel="Trace" internalLogFile="logs/internalLog.txt"> <targets> <!--文件输出(type="File")--> <target name="FileOutput" type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${callsite} ${level}: ${message} ${event-context:item=exception} ${stacktrace} ${event-context:item=stacktrace}" /> <!--控制台输出(type="File")--> <target name="ConsoleOutput" type="Console" layout="${date:format=yyyy-MM-dd HHmmss} ${callsite} ${level} ${message}"></target> </targets> <rules> <!--<logger name="Test" minlevel="Debug" maxlevel="Error" writeTo="FileOutput,ConsoleOutput" />--> <logger name="*" minlevel="Debug" maxlevel="Error" writeTo="FileOutput,ConsoleOutput" /> </rules> </nlog> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
NLog使用:
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();//LogManager.GetLogger("Test"); static void Main(string[] args) { Logger.Trace("This is Trace"); Logger.Debug("This is Debug"); Logger.Info("This is Info"); Logger.Error("This is Error"); Logger.Fatal("This is Fatal"); Console.ReadLine(); }