Asp.Net Core 2.0 之旅---NLog日志的使用教程
2018-01-17 13:14
981 查看
1、安装NLog,搜索 NLog.Web.AspNetCore,并将它安装在web项目中。
2、配置NLog,在StartUpzz启动类中的 Configure 方法中 配置NLog

3、 配置nlog.config文件,首先在项目根目录下新建文件nlog.config,将下面的信息复制到此文件中。配置文件中的targets节点是我们重要关注点:
type="File" 表示 输出为文件类型,
fileName="${basedir}/Logs/${shortdate}/nlog-all-${shortdate}.log" 表示 文件的路径(${dasedir}这个指定用于获取网站的根路径,不知道为啥项目调试时,总是无缘无故停止,把${basedir}/Logs/去掉就正常,这个记录一下,也希望有遇到的朋友能够帮忙解决。当前配置的文件路径如果发布到 Linux上的话,可能不会生成日志文件,这可能是Linux的文件权限的问题,需要赋予项目所在的文件夹rw的权限。)
layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception}" 表示 日志输出的样式,这里我不做过多的解释,因为这方面的资料很全,大家可以自行搜索。
<?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="info"
internalLogFile="NLogInternal.log">
<!-- Load the ASP.NET Core plugin -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="${basedir}/Logs/${shortdate}/nlog-all-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="${basedir}/Logs/${shortdate}/nlog-own-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception}" />
<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>4、配置完成后,我们就可以愉快的使用这个NLogL了。

上面的例子 是使用 构造方法依赖注入的方式,如果不了解依赖注入的话,请看我的系列教程 Asp.Net Core 2.0 之旅---AutoFac IOC容器的使用教程。
5、至此就可以使用它了。
_logger.LogInformation("1");
_logger.LogWarning("1");
_logger.LogDebug("1");是不是 很简单呢?
备注:如果有跟我一样喜欢 NET CORE 的,请加599063383这个群。让我们共同进步!!!
2、配置NLog,在StartUpzz启动类中的 Configure 方法中 配置NLog
3、 配置nlog.config文件,首先在项目根目录下新建文件nlog.config,将下面的信息复制到此文件中。配置文件中的targets节点是我们重要关注点:
type="File" 表示 输出为文件类型,
fileName="${basedir}/Logs/${shortdate}/nlog-all-${shortdate}.log" 表示 文件的路径(${dasedir}这个指定用于获取网站的根路径,不知道为啥项目调试时,总是无缘无故停止,把${basedir}/Logs/去掉就正常,这个记录一下,也希望有遇到的朋友能够帮忙解决。当前配置的文件路径如果发布到 Linux上的话,可能不会生成日志文件,这可能是Linux的文件权限的问题,需要赋予项目所在的文件夹rw的权限。)
layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception}" 表示 日志输出的样式,这里我不做过多的解释,因为这方面的资料很全,大家可以自行搜索。
<?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="info"
internalLogFile="NLogInternal.log">
<!-- Load the ASP.NET Core plugin -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="${basedir}/Logs/${shortdate}/nlog-all-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="${basedir}/Logs/${shortdate}/nlog-own-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception}" />
<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>4、配置完成后,我们就可以愉快的使用这个NLogL了。
上面的例子 是使用 构造方法依赖注入的方式,如果不了解依赖注入的话,请看我的系列教程 Asp.Net Core 2.0 之旅---AutoFac IOC容器的使用教程。
5、至此就可以使用它了。
_logger.LogInformation("1");
_logger.LogWarning("1");
_logger.LogDebug("1");是不是 很简单呢?
备注:如果有跟我一样喜欢 NET CORE 的,请加599063383这个群。让我们共同进步!!!
相关文章推荐
- ASP.NET Core 2.0 使用NLog实现日志记录
- EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真
- Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例
- Asp.net Core中使用NLog,并封装成公共的日志方法
- [转]ASP.NET Core 开发-Logging 使用NLog 写日志文件
- asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程
- Asp.Net Core 2.0 之旅---AutoFac IOC容器的使用教程
- ASP.NET Core使用NLog记录日志到Microsoft Sql Server
- ASP.NET Core开发教程之Logging利用NLog写日志文件
- ASP.NET Core 开发-Logging 使用NLog 写日志文件
- ASP.NET Core 2.0 配置NLog日志组件
- ASP.NET Core 2.0系列学习笔记-NLog日志配置文件
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- ASP.NET Core 开发-Logging 使用NLog 写日志文件
- Scott Mitchell 的ASP.NET 2.0数据教程之十四:使用FormView 的模板
- Scott Mitchell 的ASP.NET 2.0数据操作教程之八:使用两个DropDownList过滤的主/从报表
- Scott Mitchell 的ASP.NET 2.0数据操作教程之七:使用DropDownList过滤的主/从报表
- Scott Mitchell 的ASP.NET 2.0数据操作教程之十:使用 GridView 和DetailView实现的主/从报表
- ASP.NET 2.0数据教程之四:: 使用ObjectDataSource展现数据
- Scott Mitchell 的ASP.NET 2.0数据操作教程之八:使用两个DropDownList过滤的主/从报表