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

推荐订阅源

The Cloudflare Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LangChain Blog
W
WeLiveSecurity
P
Proofpoint News Feed
月光博客
月光博客
NISL@THU
NISL@THU
L
LINUX DO - 最新话题
Webroot Blog
Webroot Blog
T
Threatpost
Y
Y Combinator Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Vercel News
Vercel News
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
MyScale Blog
MyScale Blog
N
News and Events Feed by Topic
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
The Hacker News
The Hacker News
Schneier on Security
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
Recent Announcements
Recent Announcements
S
Security @ Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
T
The Exploit Database - CXSecurity.com
云风的 BLOG
云风的 BLOG
C
Cisco Blogs
雷峰网
雷峰网
量子位
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
I
Intezer
T
The Blog of Author Tim Ferriss
G
GRAHAM CLULEY
D
DataBreaches.Net
V
Vulnerabilities – Threatpost
P
Privacy & Cybersecurity Law Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
罗磊的独立博客

博客园 - 郎中令

锦衣夜行,AI乐园 Redis实战:用缓存为数据库减负(二) Redis初体验: 搭建与简单应用(一) .Net Core的SwaggerUI接口分类+固定路由 防御性编码:手搓NLog日志+Seq分布式 对接Java所谓的DES加解密 人大金仓数据库转换 人大金仓踩坑指南 RSA加解密笔记 HTTPS请求笔记- SSL安全通道验证问题 .NetCore打包部署(DLL) 字典映射处理 SM4算法快速预览与Framework4.5版本对接 简易首页防暴力-字典计时器 vite运行打包前端-部署Linux 记一次线上数据库异常的协助排查 降本增笑:记一次首页并发白屏优化过程 solr 基础介绍以及踩坑日记 动态展示缩放背景图
真实性编码:配置NLog日志+Seq分布式
郎中令 · 2025-09-01 · via 博客园 - 郎中令

        上一篇记录了 Nlog+异步+自定义+Seq, 虽然改造了传统的1.0版纯IO日志方法,但是总体上来看,还是属于硬编码,手搓业务过多,与主流的配置路线不符,如果是一两台服务器、个别开发人员的中小项目,这样使用也是没问题的。当然,如果想切换到配置路线,方便后续的扩展和维护,也是可以无缝处理的,2分钟即可搞定,具体步骤如下:

调整Nlog 配置文件,其中 seqKey 的值可以登录Seq站点,在设置菜单下,申请APIKey, 如果不设置裸奔也可以的

<?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" internalLogLevel="Warn" internalLogFile="nlog-internal.txt">
    
    <extensions><add assembly="NLog.Targets.Seq"/></extensions>

    <!-- 变量:日志根目录、Seq 地址、API Key -->
    <variable name="logRoot" value="${basedir}/Logs"/>
    <variable name="seqUrl"  value="http://localhost:5341"/>
    <variable name="seqKey"  value="Hsy2rL07HQBysPEyFtXB"/>
    <targets>
        <target name="file" xsi:type="AsyncWrapper" queueLimit="10000" overflowAction="Block">
            <target xsi:type="File"
                    fileName="${logRoot}/${level}/${logger}/${shortdate}.log" layout="${longdate} ${level:uppercase=true} ${logger} ${message} ${exception:format=tostring}"
                    keepFileOpen="true" concurrentWrites="false" createDirs="true"/>
        </target>
        <!-- Seq 远程目标 -->
        <target name="seq" xsi:type="BufferingWrapper" bufferSize="1000" flushTimeout="2000">
            <target xsi:type="Seq" serverUrl="${seqUrl}" apiKey="${seqKey}"/>
        </target>
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="file,seq"/>    <!-- 所有日志 ≥Debug 级同时写本地 + Seq -->
    </rules>
</nlog>

上面的配置文件,同样设置了 异步、自定义名称、Seq 记录,等同源代码上的大段的手搓业务,所以接下来我们在代码里面只需要保留最基本的写入方法即可,也就是说,整个 NLogHelper 类,只需要5行代码即可搞定

        
//只需要5行代码
private static readonly ConcurrentDictionary<string, ILogger> _cache = new(StringComparer.OrdinalIgnoreCase); private static ILogger GetLogger(string name) => _cache.GetOrAdd(name, LogManager.GetLogger); public static void WriteError(string message, string loggerName = "") => GetLogger(loggerName).Error(message); public static void WriteInfo(string message, string loggerName = "") => GetLogger(loggerName).Info(message); public static void WriteWarn(string message, string loggerName = "") => GetLogger(loggerName).Warn(message);

 至于怎么调用写入日志,无任何变化,原代码逻辑无需调整

        NLogHelper.WriteInfo($"记录请求的日志,{obj.sign}", ParamConst.LockLog);
        NLogHelper.WriteError($"账号不存在,{obj?.appKey}", ParamConst.APITEMPUSER);

看一下 seq 上请求的记录,如下图所示,没什么问题,一样的记录,能完整的看到数据

image

再看一下原本的本地log日志是否也正常写入,指定路径下,在自定义的 文件路径 + 文件名 中,可以找到对应的记录数据,说明也是无问题

image

简简单单,短短几分钟就切换完毕,非常的便捷,也方便后续的扩展