首先用NuGet安装NLog依赖DLL

NLog

NLog.Config

NLog.Schema

NLog配置文件NLog.config:

<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off"
internalLogFile="${basedir}/Log/nlog-internal.log"> <!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<!--<variable name="myvar" value="myvalue"/>--> <variable name="logDir" value="${basedir}/Log"/>
<variable name="logFileName" value="${date:format=yyyyMMdd}.txt"/>
<variable name="logArchiveFileName" value="${date:format=yyyyMMdd}_{#}.txt"/>
<variable name="logLayout" value="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} [${level}] ${message}"/> <!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
--> <targets> <!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
--> <!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
--> <target xsi:type="File" name="debug"
layout="${logLayout}"
fileName="${logDir}/Debug/${logFileName}"
archiveFileName="${logDir}/Debug/${logArchiveFileName}"
archiveAboveSize="10485760"
archiveNumbering="Sequence"
maxArchiveFiles="10000"
concurrentWrites="true"
keepFileOpen="true"
openFileCacheTimeout="30"
encoding="UTF-8" /> <target xsi:type="File" name="info"
layout="${logLayout}"
fileName="${logDir}/Info/${logFileName}"
archiveFileName="${logDir}/Info/${logArchiveFileName}"
archiveAboveSize="10485760"
archiveNumbering="Sequence"
maxArchiveFiles="10000"
concurrentWrites="true"
keepFileOpen="true"
openFileCacheTimeout="30"
encoding="UTF-8" /> <target xsi:type="File" name="error"
layout="${logLayout}"
fileName="${logDir}/Error/${logFileName}"
archiveFileName="${logDir}/Error/${logArchiveFileName}"
archiveAboveSize="10485760"
archiveNumbering="Sequence"
maxArchiveFiles="10000"
concurrentWrites="true"
keepFileOpen="true"
openFileCacheTimeout="30"
encoding="UTF-8" /> </targets> <rules>
<!-- add your logging rules here --> <!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
--> <!-- 不打印Quartz组件的日志 -->
<logger name="Quartz.*" maxlevel="Info" final="true" /> <logger name="*" minlevel="Debug" maxlevel="Debug" writeTo="debug" /> <logger name="*" minlevel="Info" maxlevel="Error" writeTo="info" /> <logger name="*" minlevel="Error" maxlevel="Error" writeTo="error" /> </rules>
</nlog>

变量定义:

private Logger _log = NLog.LogManager.GetLogger("NLogTest");

或者:

private Logger _log = NLog.LogManager.GetCurrentClassLogger();

写日志示例:

private void button4_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
Log("==== 开始 ========");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
List<Task> taskList = new List<Task>();
Task tsk = null;
int taskCount = 0; tsk = Task.Run(() =>
{
for (int i = 0; i < n; i++)
{
_log.Info("测试日志 " + i.ToString("000000"));
Interlocked.Increment(ref taskCount);
}
});
taskList.Add(tsk); tsk = Task.Run(() =>
{
for (int i = 0; i < n; i++)
{
_log.Debug("测试日志 " + i.ToString("000000"));
Interlocked.Increment(ref taskCount);
}
});
taskList.Add(tsk); tsk = Task.Run(() =>
{
for (int i = 0; i < n; i++)
{
_log.Error("测试日志 " + i.ToString("000000"));
Interlocked.Increment(ref taskCount);
}
});
taskList.Add(tsk); Task.WaitAll(taskList.ToArray());
Log("Task Count=" + taskCount); Log("==== 结束 " + ",耗时:" + stopwatch.Elapsed.TotalSeconds.ToString("0.000") + " 秒 ========");
stopwatch.Stop();
});
}

C# NLog 配置的更多相关文章

  1. ASP.NET Core根据环境切换NLog配置

    1.新建NLog配置文件,名称分别为nlog.config和nlog.debug.config <?xml version="1.0"?> <nlog xmlns ...

  2. Nlog配置

    初次使用nlog,里里外外找了好久,终于搞会了. 使用nlog建日志输出到txt文件.数据库.邮件 nlog配置,如图 码云dome

  3. NLog 配置

    之前我介绍过如何使用log4net来记录日志,但最近喜欢上了另一个简单好用的日志框架NLog. 关于NLog和log4net的比较这里就不多讨论了,感兴趣的朋友可以参看.NET日志工具介绍和log4n ...

  4. NLog 配置与使用

    有段时间没写博客了,过年放假,一直在弄CMS.什么都自己写了一遍,今天写写NLog,之前一用的log4net,感觉配置起来还是有些麻烦. NuGet 添加组件 配置 NLog.config <? ...

  5. Nlog配置实例

      彩色Console target <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns= ...

  6. Nlog 配置总结

    Writes log messages to one or more files. Since NLog 4.3 the ${basedir} isn't needed anymore for rel ...

  7. NLog配置分享

    新建一个文件命名为NLog.Config,然后添加如下代码 <?xml version="1.0" encoding="utf-8" ?> < ...

  8. NLog配置JsonLayout中文输出为unicode问题

    日志输出现要改为json格式,网上查询layout配置为JsonLayout就可以了,结果发现输出中文为unicode编码,看很多文章说配置encode="false"就可以了,结 ...

  9. 常用NLog配置

    <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...

  10. .net core webapi +ddd(领域驱动)+nlog配置+swagger配置 学习笔记(1)

    搭建一个.net core webapi项目  在开始之前,请先安装最新版本的VS2017,以及最新的.net core 2.1. 首先创建一个Asp.Net Core Web应用程序 这个应用程序是 ...

随机推荐

  1. 【主流技术】详解 Spring Boot 2.7.x 集成 ElasticSearch7.x 全过程(二)

    目录 前言 一.添加依赖 二. yml 配置 三.注入依赖 四.CRUD 常用 API ES 实体类 documents 操作 常见条件查询(重点) 分页查询 排序 构造查询 测试调用 五.文章小结 ...

  2. 文心一言 VS 讯飞星火 VS chatgpt (142)-- 算法导论12.1 2题

    二.用go语言,二叉搜索树性质与最小堆性质(见 6.1 节)之间有什么不同?能使用最小堆性质在 O(n)时间内按序输出一棵有 n 个结点树的关键字吗?可以的话,请说明如何做,否则解释理由. 文心一言: ...

  3. ABAP 生产订单长文本增强 <销售计划 、物料独立需求 长文本带入 计划订单-生产订单 >

    计划订单长文本带入生产订单 尝试在生产订单保存后 用 creat_text 函数 去创建长文本,发现前台不显示,查看 文本抬头底表 STXL 发现有值 ,用READ 函数 读取 能读. DATA:td ...

  4. 什么是物理信息系统(cps)?

    物理信息系统(Cyber-Physical Systems,简称CPS)是由计算机.网络和物理组件相互交互的智能系统.它集成了实时计算.通信网络和物理过程控制,以提供智能化的感知.决策和执行功能. C ...

  5. Python实现模块热加载

    为什么需要热加载 在某些情况,你可能不希望关闭Python进程并重新打开,或者你无法重新启动Python,这时候就需要实现实时修改代码实时生效,而不用重新启动Python 在我的需求下,这个功能非常重 ...

  6. Meta3D -- 开源的Web3D低代码平台

    大家好,Meta3D是开源的Web3D低代码平台,快速搭建Web3D编辑器,共建开放互助的web3d生态 Github 进入平台 功能演示 加入UI Control 加入Action脚本 运行&quo ...

  7. ElasticSearch之Shard request cache settings

    对于查询操作,Elasticsearch提供了缓存特性来暂存结果. 对于相同条件的查询请求,在缓存中的数据失效前,响应后续的查询操作时可以直接从缓存中提取结果,有效降低检索操作的时延,提升检索数据时的 ...

  8. ElasticSearch之Exists API

    检查指定名称的索引是否存在. 命令样例如下: curl -I "https://localhost:9200/testindex_002?pretty" --cacert $ES_ ...

  9. Springboot3核心特性

    一.简介 1. 前置知识 Java17 Spring.SpringMVC.MyBatis Maven.IDEA 2. 环境要求 环境&工具 版本(or later) SpringBoot 3. ...

  10. STM32CubeMX教程3 GPIO输入 - 按键响应

    1.准备材料 开发板(STM32F407G-DISC1) ST-LINK/V2驱动 STM32CubeMX软件(Version 6.10.0) keil µVision5 IDE(MDK-Arm) 2 ...