NLog.config 配置示例 生产环境上使用

2020-12-17  乐帮网

c# netcore nlog

我在生产环境使用的配置如下,把Error和调试信息分开,同时按日期和大小分割日志,每天按序号生成日志文件名称,如果日志文件大小超过100M则自动切割。超过30天的日志自动清除。

<?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.
    -->

    <!--
    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}" />
    -->
    <!--Error保存至文件-->
    <target name="error_file" xsi:type="File" maxArchiveFiles="30"  encoding="utf-8"
            fileName="${basedir}/Logs/${date:yyyyMMdd}_Error.log"
            archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_Error.{#}.log"
            archiveDateFormat="yyyyMMdd"
            archiveAboveSize="104857600"
            archiveNumbering="Sequence"
            layout="${date:yyyy-MM-dd HH\:mm\:ss} ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace}" />
    <!--Trace保存至文件-->
    <target name="trace_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8"
            fileName="${basedir}/Logs/${date:yyyyMMdd}_Trace.log"
            archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_Trace.{#}.log"
            archiveDateFormat="yyyyMMdd"
            archiveAboveSize="104857600"
            archiveNumbering="Sequence"
            layout="${date:yyyy-MM-dd HH\:mm\:ss} ${uppercase:${level}}: ${message}" />
  </targets>

  <rules>
    <!-- add your logging rules here -->
    <logger name="*" minlevel="Trace" maxlevel="Warn" writeTo="trace_file" />
    <!--<logger name="*" minlevel="Debug" writeTo="debugger" />-->
    <logger name="*" minlevel="Error" writeTo="error_file" />
    <!--
    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>

相关文章:

NetCore 中使用Nlog 简单几步搞定

欢迎指正,可在微信公众号里直接留言。

公众号二维码

关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com

庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。

欧阳修

付款二维码

如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力