首先用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. MIT实验警示:人类或需要人工智能辅助才能理解复杂逻辑

    麻省理工实验揭示人类的天赋缺陷 麻省理工学院林肯实验室(MIT Lincoln Laboratory)的一项研究表明,尽管形式规范具有数学上的精确性,但人类并不一定能对其进行解释.换句话说就是,人类在 ...

  2. 快速排序(quick_sort)

    快速排序大体分为三个步骤: 1.确定分界点 q[(l+r) >> 1] 或者 q[(l+r+1) >> 1] ,两者得看情况而定,不能用 q[l] 或者 q[r] 了 因为会超 ...

  3. 搭建 MongoDB (v6.0) 副本集记录

    副本集概述 副本集(Replica Set)是一组带有故障转移的 MongoDB 实例组成的集群,由一个主(Primary)服务器和多个从(Secondary)服务器构成.通过Replication, ...

  4. 一个NET8 AOT编译的辅助项目,让你的任何可执行应用快速部署为服务

    不知道大家有没有和小编一样,很多时候想将自己的一些应用转为服务运行,比如一些控制台应用或者.NET Core应用,但是又不想重新编码,把他们转为服务,本文将给大家提供些我使用过的方法,并提供一个基于N ...

  5. Hive的使用以及如何利用echarts实现可视化在前端页面展示(四)---连接idea使用echarts可视化界面

    说来惭愧,我的javaweb烂得一批,其他步骤我还是很顺利地,这个最简单的,我遇到了一系列问题.只能说,有时候失败也是一种成功吧 这一步其实就是正常的jdbc,没什么可说明的,但是关于使用echart ...

  6. Gradio-Lite: 完全在浏览器里运行的无服务器 Gradio

    Gradio 是一个经常用于创建交互式机器学习应用的 Python 库.在以前按照传统方法,如果想对外分享 Gradio 应用,就需要依赖服务器设备和相关资源,而这对于自己部署的开发人员来说并不友好. ...

  7. 解决Spring Boot Mail无法发送HTML格式的邮件

    Spring Boot版本:2.6.2 查阅spring-boot-starter-mail源码的MimeMessageHelper.setText方法,发现有个Boolean类型参数控制是否是HTM ...

  8. [ABC246E] Bishop

    Problem Statement We have an $N \times N$ chessboard. Let $(i, j)$ denote the square at the $i$-th r ...

  9. Spring中Bean的加载方式~

    1.方式一:基于spring.xml方式配置Bean user import lombok.Data; /** * @author : ly */ @Data public class User { ...

  10. Java中的并发队列

    1.队列 队列是一种数据结构.它有两个基本操作:在队列尾部加入一个元素,和从队列头部移除一个元素(注意不要弄混队列的头部和尾部)就是说,队列以一种先进先出的方式管理数据,如果你试图向一个 已经满了的阻 ...