1.新建ASP.NET Core项目
1.1选择项目
1.2选择.Net版本
2. 添加NLog插件
2.1 通过Nuget安装
2.2下载相关的插件
3.修改NLog配置文件
3.1添加NLog配置文件
3.2修改NLog.config配置文件
配置文件全部内容:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 autoReload="true" 5 internalLogLevel="Warn" 6 internalLogFile="internal-nlog.txt"> 7 8 <!--define various log targets--> 9 <targets> 10 <target name="file" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard"> 11 <target xsi:type="File" fileName="${basedir}/logs/${shortdate}.txt" layout="${longdate} ${level:uppercase=true} ${event-context:item=Action} ${message} ${event-context:item=Amount} ${stacktrace}" /> 12 </target> 13 14 </targets> 15 16 <rules> 17 <logger name="*" minlevel="Info" writeTo="file" /> 18 </rules> 19 20 21 </nlog>
重点内容:
1 <targets>
2 <target xsi:type="File" name="LogFile" fileName="${basedir}/logs/${shortdate}-${level}.txt"
3 layout="${longdate}|${uppercase:${level}}|${message} ${exception}" />
4 </targets>
5
6 <rules>
7 <logger name="*" minlevel="Debug" writeTo="LogFile" />
8 </rules>
3.3修改StartUp.cs文件
1 public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
2 {
3 /*使用NLog*/
4 loggerFactory.AddNLog();
5 env.ConfigureNLog("NLog.config");
6
7 app.UseStaticFiles();
8
9 app.UseMvc(routes =>
10 {
11 routes.MapRoute(
12 name: "default",
13 template: "{controller=Home}/{action=Index}/{id?}");
14 });
15 }
4使用NLog
static Logger logger = LogManager.GetCurrentClassLogger();
....
logger.Info("NLog日志测试" + DateTime.Now);
5 Demo程序
下载地址:https://download.csdn.net/download/u010312811/10559572