Nlog基本使用

nlog,基本,使用 · 浏览次数 : 8

小编点评

```csharp public class DyVideoController : IDyVideoServer { private IDyVideoServer dyVideoServer; private ILogger _logger; public DyVideoController(IDyVideoServer dyVideoServer, ILogger<DyVideoController> logger) { this._videoServer = dyVideoServer; this._logger = logger; } // ...其他方法 ... } ``` **效果:** * 使用 `ILogger` 创建 `DyVideoController` 实例时,会自动将日志记录到 `NLog` 文件中。 * 在 `DyVideoController` 中,可以使用 `_logger` 访问日志记录器。

正文

引入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="Microsoft.*"  writeTo="" final="true" />
        <!--日志记录规则,符合规则的writeTo到相应的日志目标中-->
        <logger name="*" level="Fatal" writeTo="LogFatal" final="true"/>
        <logger name="*" level="Error" writeTo="LogError" final="true"/>
        <logger name="*" level="Warn"  writeTo="LogWarn" final="true"/>
        <logger name="*" level="Info"  writeTo="LogInfo" final="true"/>
        <logger name="*" level="Debug" writeTo="LogDebug" final="true"/>
        <logger name="*" level="Trace" writeTo="LogTrace" final="true"/>
        <logger name="*" minlevel="Debug" writeTo="LogAll" />        
    </rules>
</nlog>
View Code
<?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}]【日志等级】[${level:uppercase=true}]【位置】[${logger}]${newline}【信息】[${message:withexception=true}] ${newline}" />
        <target name="LogTrace" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760"  fileName="Logs/Trace跟踪日志/【${shortdate}】-Trace日志.txt" layout="【时间】[${date}]【日志等级】[${level:uppercase=true}]【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline} " />
        <target name="LogDebug" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760"  fileName="Logs/Debug调试日志/【${shortdate}】-Debug日志.txt" layout="【时间】[${date}]【日志等级】[${level:uppercase=true}]【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline} " />
        <target name="LogInfo" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760"  fileName="Logs/Info信息日志/【${shortdate}】-Info日志.txt" layout="【时间】[${date}]【日志等级】[${level:uppercase=true}]【位置】[${logger}]${newline}【信息】[${message:withexception=true}] ${newline}" />
        <target name="LogWarn" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760"  fileName="Logs/Warn警告日志/【${shortdate}】-Warn日志.txt" layout="【时间】[${date}]【日志等级】[${level:uppercase=true}]【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline} " />
        <target name="LogError" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760"  fileName="Logs/Error错误日志/【${shortdate}】-Error日志.txt" layout="【时间】[${date}]【日志等级】[${level:uppercase=true}]【位置】[${logger}]${newline}【信息】[${message:withexception=true}]${newline} " />
        <target name="LogFatal" xsi:type="File" maxArchiveDays="30" archiveAboveSize="10485760"  fileName="Logs/Fatal致命日志/【${shortdate}】-Fatal日志.txt" layout="【时间】[${date}]【日志等级】[${level:uppercase=true}]【位置】[${logger}]${newline}【信息】[${message:withexception=true}] ${newline}" />
    </targets>

    <rules>
        <!--日志记录规则,符合规则的writeTo到相应的日志目标中-->
        <logger name="Microsoft.*"  writeTo="" final="true" />
        <!--日志记录规则,符合规则的writeTo到相应的日志目标中-->
        <logger name="*" level="Fatal" writeTo="LogFatal" final="true" />
        <logger name="*" level="Error" writeTo="LogError" final="true" />
        <logger name="*" level="Warn"  writeTo="LogWarn" final="true" />
        <logger name="*" level="Info"  writeTo="LogInfo" final="true" />
        <logger name="*" level="Debug" writeTo="LogDebug" final="true" />
        <logger name="*" level="Trace" writeTo="LogTrace" final="true" />
        <logger name="*" minlevel="Debug" writeTo="LogAll" />
    </rules>
</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">

    <targets async="true">
        <!--maxArchiveDays最长保存N天,archiveAboveSize一个文件最大为N字节-->
        <target name="LogDebug" xsi:type="File" maxArchiveDays="7" archiveAboveSize="10485760"  fileName="Logs/Debug信息/【${shortdate}】.txt" layout="【${date}】【${logger}】${newline}【信息】${message:withexception=true}${newline}${newline} " />
        <target name="LogInfo" xsi:type="File" maxArchiveDays="7" archiveAboveSize="10485760"  fileName="Logs/Info信息/【${shortdate}】.txt" layout="【${date}】【${logger}】${newline}【信息】${message:withexception=true}${newline}${newline} " />
        <target name="LogError" xsi:type="File" maxArchiveDays="7" archiveAboveSize="10485760"  fileName="Logs/Error信息/【${shortdate}】.txt" layout="【${date}】【${logger}】${newline}【信息】${message:withexception=true}${newline}${newline} " />
    </targets>

    <rules>
        <!--日志记录规则,符合规则的writeTo到相应的日志目标中-->
        <logger name="Microsoft.*"  writeTo="" final="true" />
        <!--日志记录规则,符合规则的writeTo到相应的日志目标中-->
        <logger name="*" level="Error" writeTo="LogError" final="true"/>
        <logger name="*" level="Info"  writeTo="LogInfo" final="true"/>
        <logger name="*" level="Debug" writeTo="LogDebug" final="true"/>
    </rules>
</nlog>
View Code
<?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;
        }

 效果:

 

 

 

与Nlog基本使用相似的内容: