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

结束

相关推荐
淡海水8 小时前
01-03-认知篇-C#内存模型深度解析
unity·c#·游戏引擎·.net·gc
lzhdim13 小时前
C#不为人知的10个魔法特性:资深开发者也会震惊的底层奥秘
java·前端·javascript·算法·c#
前端 贾公子16 小时前
LangChain核心组件 == 提示词(Prompts)
开发语言·c#
码艺-Alimjan19 小时前
Web 网站打包桌面应用的另一种方式,超级简单(C# exe 33Kb)
开发语言·前端·c#
海天鹰20 小时前
C#入门:显示窗口
c#
淡海水20 小时前
16-02-C#常用数据结构源码-附录B-源码索引速查
unity·c#·游戏引擎·il2cpp
海天鹰21 小时前
C#入门:在cmd显示字符
c#
波力海苔夹心脆6751 天前
C# 序列化与反序列化详解:System.Text.Json、Newtonsoft.Json、XmlSerializer 用法、特性选项与安全实践
经验分享·后端·c#·json·.net
小羊没烦恼!2 天前
在Scrum中实施敏捷建模
java·开发语言·windows·算法·c#
Lost of 程序猿2 天前
建造者模式实战:告别“十参数构造函数“的数据导出任务
后端·设计模式·c#·asp.net