上一篇文章描述了如何在ASP.NET Core中使用Log4Net记录日志。本篇将使用另外一个组件NLog在ASP.NET Core中记录日志。

1.引入程序集 NLog.Web.AspNetCore(NuGet中直接添加)

2.增加配置文件,配置Nlog生效

3.注入得到Nlog生效,写文本日志

4.引入数据库相关程序集  System.Data.SqlClient

5.初始化数据库日志表,配置写日志到数据库

NLog对应的XML格式的配置文件如下:

<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/> <!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<target name="AllDatabase" xsi:type="Database"
dbProvider="System.Data.SqlClient.SqlConnection, System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=LogManager;Persist Security Info=True;User ID=sa;Password=a123456!"
commandText="insert into dbo.NLog (Application, Logged, Level, Message,Logger, CallSite, Exception) values (@Application, @Logged, @Level, @Message,@Logger, @Callsite, @Exception);">
<parameter name="@application" layout="AspNetCoreNlog" />
<parameter name="@logged" layout="${date}" />
<parameter name="@level" layout="${level}" />
<parameter name="@message" layout="${message}" />
<parameter name="@logger" layout="${logger}" />
<parameter name="@callSite" layout="${callsite:filename=true}" />
<parameter name="@exception" layout="${exception:tostring}" />
</target> <target xsi:type="File" name="allfile" fileName="NLog\nlog-all-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<!--同样是将文件写入日志中,写入的内容有所差别,差别在layout属性中体现。写入日志的数量有差别,差别在路由逻辑中体现-->
<target xsi:type="File" name="ownFile-web" fileName="NLog\nlog-my-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="Null" name="blackhole" />
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
</targets> <rules>
<logger name="*" minlevel="Trace" writeTo="AllDatabase" />
<!-- add your logging rules here -->
<!--路由顺序会对日志打印产生影响。路由匹配逻辑为顺序匹配。-->
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<!--以Microsoft打头的日志将进入此路由,由于此路由没有writeTo属性,所有会被忽略-->
<!--且此路由设置了final,所以当此路由被匹配到时。不会再匹配此路由下面的路由。未匹配到此路由时才会继续匹配下一个路由-->
<logger name="Microsoft.*" minlevel="Trace" final="true" />
<!--上方已经过滤了所有Microsoft.*的日志,所以此处的日志只会打印除Microsoft.*外的日志-->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>

C#代码如下:

#region NLogin
{
//Nuget引入:NLog.Web.AspNetCore
//builder.Logging.AddNLog("CfgFile/NLog.config");
}
#endregion

  

最新文章

  1. Java 条形码 二维码 的生成与解析
  2. 给notepad++添加右键菜单
  3. android wireshark抓包和fiddler抓包
  4. SharePoint 读取 Site Columns 的数据并绑定到DropdownList
  5. [TimusOJ1057]Amount of Degrees
  6. CSS3与页面布局学习总结
  7. 一个类有两个方法,其中一个是同步的,另一个是非同步的; 现在又两个线程A和B,请问:当线程A访问此类的同步方法时,线程B是否能访问此类的非同步方法?
  8. Mac OS下编写对拍程序
  9. java异常处理练习
  10. PHP查看在线服务器与本地服务器支持函数差别
  11. ios 类似的效果淘宝商品详细页面
  12. qml能够这么玩
  13. 【代码学习】PHP文件的上传和下载
  14. 测者的测试技术手册:Java中的null类型是测试不可超越的鸿沟
  15. C常量与控制语句
  16. 设置SharePoint部门站点各个文件夹的权限
  17. 求矩形面积(问题来自PythonTip)
  18. POJ1611:The Suspects(模板题)
  19. 查看centos操作系统、java_jdk、hadoop位数
  20. applicationContext.xml的文件位置就可以有两种默认实现

热门文章

  1. 浅谈flume
  2. supervisor 安装及基本使用
  3. 免费语音转文字----使用Adobe Premiere Pro
  4. vim自动输入P的问题
  5. SpringBoot的使用
  6. ElasticSearch、ElasticSearch-head的安装和问题解决
  7. 浏览器 - 重绘(repaint)重排(reflow)
  8. centos8 安装docker启动失败
  9. 使用Libusb和hidapi测试HID设备
  10. 学习笔记-Java流程控制