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

结束

相关推荐
xiaoshuaishuai816 小时前
C# Codex 脚本编写
java·服务器·数据库·c#
weixin_4474432519 小时前
AI启蒙Lean4
python·c#
我是唐青枫1 天前
C#.NET ValueTaskSource 深入解析:零分配异步、ManualResetValueTaskSourceCore 与使用边界
c#·.net
iCxhust1 天前
C#程序,窗体1向窗体2的textbox控件写入字符串“hello”
开发语言·c#
iCxhust1 天前
C#如何实现textbox文本多行输出 且自动换行输出
开发语言·c#
政沅同学1 天前
C# 一种很好用的设计模式 不同对象之间进行解耦通信
设计模式·c#
咩图1 天前
C#进程通讯-命名管道通讯
c#·命名管道·进程通讯
SunnyDays10111 天前
如何使用 C# 高效实现 Excel 与 CSV 的互相转换
c#·excel·csv
Byron Loong1 天前
【网络】C#TCP 通讯
网络·tcp/ip·c#
geovindu1 天前
密码进行加盐哈希 using CSharp,Python,Go,Java
java·python·golang·c#·哈希算法