.net 6中使用NLog写文本日志
本文讲解如何在.net core 6中使用NLog写文本日志,首先需要在项目中添加NLog.Web.AspNetCore引用。
然后准备配置文件,在项目中添加CfgFile文件夹,在其下新建NLog.config文件,用于对NLog的配置,代码如下:
<?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 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>
然后在Program.cs进行注册NLog,注册代码如下:
using NLog.Web; var builder = WebApplication.CreateBuilder(args); #region NLog { //引用NLog.Web; //引用 builder.Logging.AddNLog("CfgFile/NLog.Config"); } #endregion
最后在Controller进行调用使用,代码如下:
public class FirstController : Controller { private readonly ILogger<FirstController> _Logger; private readonly ILoggerFactory _LoggerFactory; public FirstController(ILogger<FirstController> logger,ILoggerFactory loggerFactory) { this._Logger = logger; this._Logger.LogInformation($"{this.GetType().Name}被构造了log1"); this._LoggerFactory = loggerFactory; ILogger<FirstController> _Logger2 = this._LoggerFactory.CreateLogger<FirstController>(); _Logger2.LogInformation($"{this.GetType().Name}被构造了log2"); } public IActionResult Index() { ILogger<FirstController> _Logger3 = this._LoggerFactory.CreateLogger<FirstController>(); _Logger3.LogInformation($"index被执行了"); this._Logger.LogInformation($"index被执行了"); return View(); } }
猜您可能还喜欢
- net core+webapi+nginx windows 服务器部署(1381)
- .Nuget Packages 太占C盘,删除后可以放到其他盘(1357)
- ASP.NET Core 配置 Swagger 显示接口注释描述信息(1158)
- vue调用接口后获取不到后端返回的Header响应头(1073)
- .net core 6.0 web API + SwaggerUI + IIS部署(1007)
- .net core 系列实例开发教程-权限管理系统功能介绍(1002)
- .net core 实例教程(十二)配置启用Swagger中的【Authorize】按钮(920)
- .net core 实例教程(十一)生成JWT格式的token密码配置及代码(857)
- .net core 实例教程(一)新建项目(854)
- .net core 实例教程(十四)配置 Swagger 显示接口注释描述信息及支持版本控制(849)
评论列表
发表评论
文章分类
文章归档
- 2025年3月 (1)
- 2024年6月 (2)
- 2024年5月 (2)
- 2024年4月 (4)
- 2024年3月 (30)
- 2024年1月 (4)
- 2023年12月 (2)
- 2023年11月 (4)
- 2023年10月 (4)
- 2023年9月 (6)
- 2023年3月 (2)
- 2023年2月 (1)
- 2023年1月 (1)
- 2022年12月 (1)
- 2022年9月 (21)
- 2022年8月 (10)
- 2022年7月 (3)
- 2022年4月 (1)
- 2022年3月 (13)
- 2021年8月 (1)
- 2021年3月 (1)
- 2020年12月 (42)
- 2020年11月 (7)
- 2020年10月 (5)
- 2020年8月 (1)
- 2020年6月 (1)
- 2020年3月 (2)
- 2019年12月 (8)
- 2019年11月 (3)
- 2019年9月 (1)
- 2019年4月 (1)
- 2019年3月 (6)
- 2019年2月 (1)
- 2018年7月 (7)
阅读排行
- 1.asp.net mvc内微信pc端、H5、JsApi支付方式总结(5781)
- 2.Windows 10休眠文件更改存储位置(3492)
- 3.各大搜索网站网站收录提交入口地址(3384)
- 4.ECharts仪表盘实例及参数使用详解(3261)
- 5.windows 10安装myeclipse 10破解补丁cracker.jar、run.bat闪退解决办法(3256)
- 6.HTML5 WebSocket与C#建立Socket连接实现代码(3068)
- 7.华为鸿蒙系统清除微信浏览器缓存方法(2991)
- 8.CERT_HAS_EXPIRED错误如何解决(2609)
- 9.Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2428)
- 10.HBuilder编辑器格式化代码(2304)