C# 使用 NLog 输出日志到文件夹

  1. 在项目中使用 NuGet 安装 NLog 包以及 NLog.Config

在这里插入图片描述

  1. 配置 nlog.config

在项目的根目录下创建一个 Nlog.config 文件(如果还没有),然后添加如下配置:

<?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">
<!-- 定义日志目标 async="true" 异步写日志-->
	<targets async="true">
    <!-- log_file 配置,按天生成日志文件夹,保留时间7-->
    <target name="log_file" xsi:type="File"
            fileName="logs/${shortdate}/LogFile_${shortdate}.log"
            layout="${longdate}|${level:uppercase=true}|${message}${onexception:${newline}${exception:format=tostring}}${newline}"
            archiveEvery="Day"
            archiveNumbering="Rolling"
            maxArchiveDays="7"
            archiveAboveSize="5242880"
            keepFileOpen="false"
            concurrentWrites="true" />
    <!-- err_file 配置 -->
    <target name="err_file" xsi:type="File"
            fileName="logs/${shortdate}/ErrorFile_${shortdate}.log"
            layout="${longdate}|${level:uppercase=true}|${messa
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小丫头呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值