Skip to main content

5 日志系统

输出控制台和NLog

安装 NLog, NLog.Extensions.Logging, 并添加 nlog.config 文件

var sc = new ServiceCollection();
sc.AddLogging(logBuilder =>
{
    logBuilder.SetMinimumLevel(LogLevel.Trace);
    logBuilder.AddConsole();
    logBuilder.AddNLog();
});

sc.AddScoped<LogClass>();

var sp = sc.BuildServiceProvider();

var s = sp.GetRequiredService<LogClass>();
s.Run();



public class LogClass
{
    private readonly ILogger<LogClass> _logger;

    public LogClass(ILogger<LogClass> logger)
    {
        _logger = logger;
    }

    public void Run()
    {
        _logger.LogInformation("haha");
        _logger.LogDebug("hadebugha");
    }
}
    

<?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"
      throwConfigExceptions="true"
      internalLogLevel="Info"
      internalLogFile=".\logs\internal-nlog-AspNetCore.txt">

    <!-- enable asp.net core layout renderers -->
    <extensions>
        <add assembly="NLog.Web.AspNetCore" />
    </extensions>

    <!-- the targets to write to -->
    <targets>
        <!-- File Target for all log messages with basic details -->
        <target xsi:type="File" name="allfile" fileName=".\logs\nlog-AspNetCore-all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}" />

        <!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
        <target xsi:type="File" name="ownFile-web" fileName=".\logs\nlog-AspNetCore-own-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />

        <!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
        <target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />
    </targets>

    <!-- rules to map from logger name to target -->
    <rules>
        <!-- All logs, including from Microsoft -->
        <logger name="*" minlevel="Trace" writeTo="allfile" />

        <!-- Suppress output from Microsoft framework when non-critical -->
        <logger name="System.*" finalMinLevel="Warn" />
        <logger name="Microsoft.*" finalMinLevel="Warn" />
        <!-- Keep output from Microsoft.Hosting.Lifetime to console for fast startup detection -->
        <logger name="Microsoft.Hosting.Lifetime*" finalMinLevel="Info" writeTo="lifetimeConsole" />

        <logger name="*" minLevel="Trace" writeTo="ownFile-web" />
    </rules>
</nlog>
nlog.config 属性
<target xsi:type="File" name="defaultFile" fileName="logs/log-${shortdate}.log" layout="..." archiveAboveSize="10000" maxArchiveFiles="10" maxArchiveDays="10"/>
  • archiveAboveSize="10000"
    • 表示: 单个日志文件超过多少字节就把日志存档, 避免单个文件太大
  • maxArchiveFiles="10"
    • 表示: 最多保存指定数量个数的存档文件, 旧的会被删除
  • maxArchiveDays="10"
    • 表示: 设定保存若干天的日志存档