惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
博客园 - 聂微东
罗磊的独立博客
W
WeLiveSecurity
博客园_首页
Scott Helme
Scott Helme
V
Visual Studio Blog
T
The Exploit Database - CXSecurity.com
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
L
Lohrmann on Cybersecurity
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
F
Full Disclosure
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 司徒正美
博客园 - Franky
C
CXSECURITY Database RSS Feed - CXSecurity.com
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
雷峰网
雷峰网
博客园 - 【当耐特】
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
T
Tor Project blog
V
V2EX
爱范儿
爱范儿
C
Check Point Blog
T
Threatpost
Project Zero
Project Zero
量子位
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
I
Intezer
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 不是豆豆

切换 Google Play 国家地区 NuGet 命令行记录 在 UnionTech OS Server 20 Euler 64bit 上安装 docker 环境 在 Windows 7 SP1 或 Windows Server 2008 R2 SP1 系统中安装 ASP.NET Core 10.0 运行时环境 记录各版本 Microsoft Visual C++ Redistributable 官方下载地址 在 PolarDB-PG 上安装 pgvector 扩展 在 microsoft onnxruntime 中使用 arcface model 出错:Invalid input scale: NumDimensions() != 3 在 Windows 下为 PostgreSQL 安装 pgvector 插件 如果两个 Steam 库文件夹中,有相同的两份游戏,这时删除第二份会怎样? 当 linux 主机内存较小运行程序(如 yum update)被 Killed 迁移 Gitea 从 Windows 到 Linux Docker 容器中(使用 SQLite 数据库) 在 EF Core 中使用传统的 DbFirst 模式 在 SkiaSharp 中对图片进行缩放时,结果锯齿较多的处理方案 在 asp.net core web 项目中,创建项目时因选择容器化部署,未配置 IIS Express 应如何进行修改 如何重置 sftpgo 的管理员密码 微信鸿蒙版 8.0.14 查询 navigator.maxTouchPoints 为 0 导致的 bug 在本地 claude code 中使用 copilot pro 订阅 在安装 miniforge3(conda)环境后,每次启动终端(PowerShell)都非常缓慢的处理方案 在 .net framework 4.8 下,若因为安装或升级了 System.ValueTuple 出现问题 在 Windows 11 系统下,日常使用浏览器(Edge、Chrome)常遇到画面撕裂或浏览器在经切换窗口后显示内容不正常 关于 CVE-2025-55315 漏洞问题查验记录 在 gitea 服务器端查询 lfs 文件占用情况 编写 GKD 规则 在 Windows 下 .net 项目中,使用 ViGEm 模拟游戏手柄 处理过大的 docker 日志 测试支持 PolarDB-X(高度兼容 MySQL) 的客户端图形工具 SteamDeck 重置系统密码教程 新版本 portainer ce 在操作容器时,报错 Forbidden - origin invalid
为传统 .net framework 项目切换 nlog 日志
不是豆豆 · 2025-08-12 · via 博客园 - 不是豆豆

1、安装 nuget 包:NLog、NLog.Targets.Trace

2、在 web.config 中 configSections 下添加 nlog 配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- 添加 NLog 配置节 -->
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
</configuration>

3、添加 nlog 配置,替换原有 listener

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- 添加 NLog 配置节 -->
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <extensions>
      <add assembly="NLog.Targets.Trace" />
    </extensions>
    <targets>
      <!-- 生产环境可使用 ${basedir} 替换路径 -->
      <target xsi:type="File" name="fileTarget" 
              fileName="D:\LogFiles\${shortdate}.log" 
              layout="${longdate} [${level:uppercase=true}] ${logger:shortName=true} - ${message} ${exception:format=tostring}" 
              archiveFileName="D:\LogFiles\${shortdate}-archived-{#}.log" 
              archiveAboveSize="10485760" 
              archiveEvery="Day" 
              archiveNumbering="Rolling" 
              maxArchiveFiles="365" />
      <target xsi:type="Console" name="consoleTarget" 
              layout="${time} [${level:uppercase=true}] ${message}" />
    </targets>
    <rules>
      <!-- 记录级别:Fatal、Error、Warn、Info、Debug、Trace -->
      <logger name="*" minlevel="Info" writeTo="fileTarget" />
      <logger name="*" minlevel="Debug" writeTo="consoleTarget" />
    </rules>
  </nlog>
  <system.diagnostics>
    <sources>
      <source name="Test.Module" switchValue="All">
        <listeners>
          <clear />
          <!-- 使用 NLog 日志 -->
          <add name="nlog" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="nlog" type="NLog.NLogTraceListener, NLog.Targets.Trace" />
    </sharedListeners>
    <!-- 为 NLog 性能优化,关闭两个配置 -->
    <trace autoflush="false" useGlobalLock="false">
      <!-- NLog 监听器在代码中初始化-->
    </trace>
  </system.diagnostics>
</configuration>

4、此时理论上原有的 Trace 日志就都由 NLog 进行记录了。

5、使用 NLog 直接记录方法:

namespace Test
{
    public class TestHelper
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        
        public void DoSomething(string input)
        {
            Logger.Info("输入:" + input);
        }
    }
}

参考链接:

https://github.com/NLog/NLog/wiki/Configuration-file#nlog-config-xml

https://github.com/NLog/NLog/wiki/NLog-Trace-Listener-for-System-Diagnostics-Trace