.NET Core如何使用NLog

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配置文件

配置文件全部内容:

<?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"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="internal-nlog.txt">

<!--define various log targets-->
<targets>
<target name="file" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
<target xsi:type="File" fileName="${basedir}/logs/${shortdate}.txt" layout="${longdate} ${level:uppercase=true} ${event-context:item=Action} ${message} ${event-context:item=Amount} ${stacktrace}" />
</target>

</targets>

<rules>
<logger name="*" minlevel="Info" writeTo="file" />
</rules>


</nlog>

重点内容:

<targets>
<target xsi:type="File" name="LogFile" fileName="${basedir}/logs/${shortdate}-${level}.txt"
layout="${longdate}|${uppercase:${level}}|${message} ${exception}" />
</targets>

<rules>
<logger name="*" minlevel="Debug" writeTo="LogFile" />
</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);
posted @ 2019-11-01 13:26  盛开的雨季  阅读(2890)  评论(0编辑  收藏  举报