.net 6 集成NLog

.net 6 webapi项目集成NLog

  • 上代码
      • [step 1 添加nuget](#step 1 添加nuget)
      • [step 2 添加支持](#step 2 添加支持)
      • [step 3 添加配置文件](#step 3 添加配置文件)
  • 结束

上代码

step 1 添加nuget

添加nuget 包 Roc

step 2 添加支持

修改program.cs

复制代码
var builder = WebApplication.CreateBuilder(args);
// 添加NLog日志支持
builder.AddRocNLog();

step 3 添加配置文件

添加配置文件 位置 看图, (注意位置和大小写)

复制代码
<?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"
      throwExceptions="true"
      internalLogLevel="Error"
      internalLogFile="nlogs/nlog-internal.log"
>
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!--<variable name="directory" value="${basedir}/logs"/>-->
  <variable name="directory" value="/home/logs/Roc"/>

  <targets async="true">
    <!-- Console -->
    <target name="Console"
            xsi:type="ColoredConsole"
            encoding="utf-8"
            layout="${longdate} [${level:uppercase=true}] ${logger} : ${message} ${exception:format=tostring}"
    />

    <!-- Info -->
    <target name="Info"
            xsi:type="File"
            encoding="utf-8"
            maxArchiveDays="7"
            archiveAboveSize="20971520"
            archiveEvery="Day"
            fileName="${directory}/log/${date:format=yyyy-MM-dd}.log"
            layout="${longdate} [${level}] [${callsite-linenumber}] ${callsite} : ${message}"
    />

    <!-- Error -->
    <target name="Error"
            xsi:type="File"
            encoding="utf-8"
            maxArchiveDays="7"
            archiveAboveSize="20971520"
            archiveEvery="Day"
            fileName="${directory}/bug/${date:format=yyyy-MM-dd}.log"
            layout="${longdate} [${level}] [${callsite-linenumber}] ${callsite} : ${message} ${exception:format=tostring}"
    />
  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="Console"/>
    <logger name="*" maxlevel="Info"  writeTo="Info"/>
    <logger name="*" minlevel="Error"  writeTo="Error"/>
  </rules>
</nlog>

结束

相关推荐
geovindu12 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
小羊先生car16 小时前
RTOS-F429-HAL-(动/静态)任务的创建(2026/7/27)
开发语言·算法·c#
地球驾驶员17 小时前
NX二次开发C#-获取体的外表面
开发语言·c#
向夏威夷 梦断明暄18 小时前
从 Bun 的 Rust 重写,看 C# 如何重建 AI 基础设施层
人工智能·rust·c#
心平气和量大福大19 小时前
C#-WPF-控件-LiveChart图表-线性2(LineSeries)-数据绑定
开发语言·c#·wpf
海盗123420 小时前
微软技术周报2026-07-27
microsoft·c#·.net
code bean1 天前
【C#】 `Channel<T>` 深度解析:生产者-消费者模式的现代解法
数据结构·c#
吴可可1232 天前
C# CAD二次开发:合并首尾重合多段线
c#
EIP低代码平台2 天前
EIP低代码平台 - 应用管理 - 表单设计
低代码·c#·权限·工作流·netcore
czhc11400756632 天前
726:zoffset
c#