1、首先用vs2010創建命令行工程nlogdemo
2、在程序包管理器控制台中輸入:install-package nlog -version 4.4.12
這句是怎么來的,要是你用過nuget包管理工具,那就可以跳過這里的說明了。
要使用nuget添加nlog包到項目中。請看下圖。
然后在程序包管理工具控制台下輸入:install-package nlog -version 4.4.12
再看看install-package nlog -version 4.4.12這個怎么找。
打開百度搜索:nuget
然后在nuget官網上搜索欄輸入:nlog 回車
選擇第一項nlog
然后在 version history 下選擇 4.4.12 這個版本
至於為什么選擇這個版本,因為這個版本下載的次數多。嘿嘿,沒辦法,隨大流。當然還要看這個版本包的依賴項
這個包的沒有什么特殊依賴項,所以可以使用。然后拷貝
這里這句話就是要我們要找的。是不是挺簡單的。
當我們在包程序包管理器控制台中輸入:install-package nlog -version 4.4.12 然后按下回車鍵,vs ide 會自動到 nuget.org 這里下載依賴包。
nlog 包添加完之后,還要添加 nlog.config(4.4.12)(install-package nlog.config -version 4.4.12) 這個包,按道理 nlog 和 nlog.config 應該一起的,
突然從某個版本開始分開了,要單獨添加。具體情況可以去官網看介紹:
添加 nlog.config 的方法跟上面添加 nlog 的方法一樣。
3、簡單封裝 log
添加類log.cs到工程中,修改代碼如下:
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
|
public
sealed
class
log
{
private
static
nlog.logger _logger = nlog.logmanager.getcurrentclasslogger();
private
log() { }
public
static
void
trace(
string
strmsg)
{
_logger.trace(strmsg);
}
public
static
void
debug(
string
strmsg)
{
_logger.debug(strmsg);
}
public
static
void
info(
string
strmsg)
{
_logger.info(strmsg);
}
public
static
void
warn(
string
strmsg)
{
_logger.warn(strmsg);
}
public
static
void
error(
string
strmsg)
{
_logger.error(strmsg);
}
public
static
void
fatal(
string
strmsg)
{
_logger.fatal(strmsg);
}
}
|
4、修改nlog.config文件,具體內容如下:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<nlog xmlns=
"http://www.nlog-project.org/schemas/nlog.xsd"
autoreload=
"true"
throwexceptions=
"false"
internalloglevel=
"off"
internallogfile=
"c:\temp\nlog-internal.log"
>
<!-- optional, add some variables
https:
//github.com/nlog/nlog/wiki/configuration-file#variables
-->
<!--
<variable name=
"myvar"
value=
"myvalue"
/>
-->
<variable name=
"fileformat"
value="
${newline}date:${date}
${newline}level:${level}
${newline}logger:${logger}
${newline}machinename:${machinename}
${newline}message:${message}
${newline}appdomain:${appdomain}
${newline}assembly-version:${assembly-version}
${newline}basedir:${basedir}
${newline}callsite:${callsite}
${newline}counter:${counter}
${newline}nlogdir:${nlogdir}
${newline}processid:${processid}
${newline}processname:${processname}
${newline}specialfolder:${specialfolder}
${newline}stacktrace: ${stacktrace}
${newline}------------------------------------------------------------" />
<!--
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 name=
"file"
xsi:type=
"file"
filename=
"${basedir}/logs/${date:format=yyyy-mm}/${shortdate}.log"
layout=
"${fileformat}"
maxarchivefiles=
"5"
archiveabovesize=
"10240"
archiveevery=
"day"
/>
</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"
/>
-->
<!--
level example
fatal highest level: important stuff down
error
for
example application crashes / exceptions.
warn incorrect behavior but the application can
continue
info normal behavior like mail sent, user updated profile etc.
debug executed queries, user authenticated, session expired
trace begin method x, end method x etc
-->
<!--
logging 水平分為以下等級“trace<<debug<<info<<warn<<error<<fatal ”,如果我們選擇info值,則trace和debug等級的信息不會被輸出。
-->
<logger name=
"*"
minlevel=
"trace"
writeto=
"file"
/>
</rules>
</nlog>
|
簡單解釋:
1
|
variable log文件的內容輸出格式
|
1
|
targets 目標文件(要生成的log文件)的配置(文件名、格式變量、文件個數、文件大小等等)
|
1
|
rules 規則,也就是俗話說的log輸出級別<br><br>以上內容不進行過多解釋了,再多解釋也不如官網的說明。詳細介紹請看官網:https:
//github.com/nlog/nlog/wiki/configuration-file#configuration-file-format
|
5、使用log輸出日志到文件,簡單示例代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class
program
{
static
void
main(
string
[] args)
{
runtest();
console.writeline(
"press a key end ..."
);
console.readkey(
true
);
}
static
void
runtest()
{
for
(
int
i = 0; i < 1000; i++)
{
log.info(
string
.format(
"{0}"
, i + 1));
system.threading.thread.sleep(10);
}
}
}
|
輸出路徑結構
輸出文件內容: