Nlog配置🔥
CZerocheng 1/5/2024 Nlog日志
# 加载位置
如果你想改变Nlog的配置文件的加载位置,你可以在程序的初始化时调用
string path = System.Environment.CurrentDirectory;
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(Path.Combine(path, "Resources", "Configs", "NLog.config"));
1
2
2
这个设置的话他就会加载根目录下Resources/Configs/NLog.config的配置文件。
private Logger logger = LogManager.GetCurrentClassLogger();
1
在你想要记录日志的地方定义一个logger对象,调用
logger.Info(string mes);
logger.Warn(string mes);
logger.Error(string mes);
1
2
3
2
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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
internalLogLevel="Info"
internalLogFile="nlog-internal.log">
<!--自定义变量-->
<variable name="logDir" value="${basedir}/Logs/${date:format=yyyyMMdd}"/>
<!-- Load the ASP.NET Core plugin,enable asp.net core layout renderers-->
<extensions>
<add assembly="NLogViewer"/>
</extensions>
<!--define various log targets-->
<!--fileName:日志存储的路径及名称-->
<!--layout:日志输出格式-->
<!-- archiveFileName:要用于存档的文件的名称 可以指定日志 -->
<!-- archiveAboveSize:以字节为单位的大小,超过该大小的日志文件将被自动存档 -->
<!-- archiveNumbering:对文件档案进行编号的方式 -->
<!-- keepFileOpen:指示是否在每次记录事件时保持日志文件打开,将此属性更改为 true 将大大提高性能,但也会保持文件句柄锁定。启用此选项时,请考虑设置openFileCacheTimeout = 30,因为它将允许存档操作并对被删除的日志文件做出反应。 -->
<targets async="true">
<target name="CameraLog" xsi:type="File"
fileName="${logDir}/Camera/CameraLog.txt"
archiveFileName="${logDir}/Camera/bak/CameraLog.{###}.txt"
archiveEvery="Day"
archiveNumbering="DateAndSequence"
archiveAboveSize="20000000"
maxArchiveFiles="200"
keepFileOpen="true"
layout="${log_message}"/>
</targets>
<!-- name:记录者的名字,如果你想要记录具体的类,你可以用命名空间.类名 -->
<!-- minlevel:最低级别 -->
<!-- maxlevel:最高级别 -->
<!-- level:单一日志级别 -->
<!-- levels: 一系列日志级别,由逗号分隔 -->
<!-- writeTo:规则匹配时日志应该被写入的一系列目标,由逗号分隔 -->
<rules>
<logger name="*" levels="Error" writeTo="CameraLog" />
</rules>
</nlog>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
这样的话,但当你调用
logger.Error(string mes);
1
日志文件就会保存在根目录下Logs/时间/Camera文件夹下的CameraLog.txt下。如果你想把信息和警告也记录到日志里面,那就在rules里面设置minLevel。
<rules>
<logger name="*" minLevel="Info" writeTo="CameraLog" />
</rules>
1
2
3
2
3
这里的CameraLog是targets中target的name。 如果你要改变日志内容的格式,那就要自己配置layout格式
<variable name="log_message" value="${longdate} | ${level:uppercase=false:padding=-5} | ${callsite} | ${callsite-linenumber} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}}"/>
1
设置后,日志打印的格式如下
2024-01-5 11:08:46.5491 | Error | NLogDemo.Model.Student.WriteError | 38 | 成功写入错误!Attempted to divide by zero.
1
这里的"NLogDemo.Model.Student.WriteError"是报错的类,38是具体的行数。
# 记录到控制台
点击查看代码
<?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"
internalLogLevel="Info"
internalLogFile="nlog-internal.log">
<!-- Load the ASP.NET Core plugin,enable asp.net core layout renderers-->
<extensions>
<add assembly="NLogViewer"/>
</extensions>
<variable name="layout" value="${longdate} | ${level:uppercase=false:padding=-5} | ${callsite} | ${callsite-linenumber} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}}"/>
<variable name="MicrosoftLevel" value="${level:lowercase=true:truncate=4:when=level==LogLevel.Info or level==LogLevel.Warn}${when:when=level==LogLevel.Error:inner=fail}${when:when=level==LogLevel.Fatal:inner=crit}${when:when=level==LogLevel.Debug:inner=dbug}${when:when=level==LogLevel.Trace:inner=trce}" />
<variable name="MicrosoftLayout" value="${MicrosoftLevel}: ${layout}" />
<targets async="true">
<!--控制台日志-->
<target name="console" xsi:type="ColoredConsole" layout="${MicrosoftLayout}" useDefaultRowHighlightingRules="false">
<highlight-word foregroundColor="DarkGreen" regex="^info" />
<highlight-word foregroundColor="Yellow" regex="^warn" />
<highlight-word foregroundColor="Black" backgroundColor="Red" regex="^fail" />
<highlight-word foregroundColor="White" backgroundColor="Red" regex="^crit" />
</target>
</targets>
<rules>
<logger name="*" minLevel="Info" writeTo="console" />
</rules>
</nlog>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32