引入get包:NLog.Extensions.Logging
添加一个nlog.cofig文件 并设置属性,始终复制或较新则复制
<?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"> <targets async="true"> <!--maxArchiveDays最长保存N天,archiveAboveSize一个文件最大为N字节--> <target name="LogAll" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760" fileName="Logs/All/【${shortdate}】-ALL日志-.txt" layout="【时间】[${date}]${newline}【日志等级】[${level:uppercase=true}]${newline}【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline}${newline} " /> <target name="LogTrace" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760" fileName="Logs/Trace跟踪日志/【${shortdate}】-Trace日志.txt" layout="【时间】[${date}]${newline}【日志等级】[${level:uppercase=true}]${newline}【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline}${newline} " /> <target name="LogDebug" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760" fileName="Logs/Debug调试日志/【${shortdate}】-Debug日志.txt" layout="【时间】[${date}]${newline}【日志等级】[${level:uppercase=true}]${newline}【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline}${newline} " /> <target name="LogInfo" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760" fileName="Logs/Info信息日志/【${shortdate}】-Info日志.txt" layout="【时间】[${date}]${newline}【日志等级】[${level:uppercase=true}]${newline}【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline}${newline} " /> <target name="LogWarn" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760" fileName="Logs/Warn警告日志/【${shortdate}】-Warn日志.txt" layout="【时间】[${date}]${newline}【日志等级】[${level:uppercase=true}]${newline}【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline}${newline} " /> <target name="LogError" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760" fileName="Logs/Error错误日志/【${shortdate}】-Error日志.txt" layout="【时间】[${date}]${newline}【日志等级】[${level:uppercase=true}]${newline}【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline}${newline} " /> <target name="LogFatal" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760" fileName="Logs/Fatal致命日志/【${shortdate}】-Fatal日志.txt" layout="【时间】[${date}]${newline}【日志等级】[${level:uppercase=true}]${newline}【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline}${newline} " /> </targets> <rules> <!--日志记录规则,符合规则的writeTo到相应的日志目标中--> <logger name="*" minlevel="Debug" writeTo="LogAll" /> <logger name="*" level="Trace" writeTo="LogTrace" />\ <logger name="*" level="Debug" writeTo="LogDebug" /> <logger name="*" level="Info" writeTo="LogInfo" /> <logger name="*" level="Warn" writeTo="LogWarn" /> <logger name="*" level="Error" writeTo="LogError" /> <logger name="*" level="Fatal" writeTo="LogFatal" /> </rules> </nlog>
注入到程序
builder.Services.AddLogging(logging => { logging.AddNLog(); });
使用的时候 构造函数
private readonly ILogger<DyVideoController> _logger; public DyVideoController(IDyVideoServer dyVideoServer, ILogger<DyVideoController> logger) { this._videoServer = dyVideoServer; this._logger = logger; }
效果:
标签:基本,logging,使用,Nlog,ILogger,._,logger,dyVideoServer From: https://www.cnblogs.com/zhang-3/p/17456340.html