1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
log4net;
5
[assembly: log4net.Config.XmlConfigurator(Watch
=
true
)]
6
namespace
The9web.Common
7
{
8
public
class
LogHelper
9
{
10
///
<summary>
11
///
静态类
12
///
</summary>
13
private
LogHelper() { }
14
private
const
string
LOG_REPOSITORY
=
"
Default
"
;
//
this should likely be set in the web config.
15
private
static
ILog m_log;
16
///
<summary>
17
///
初始化日志系统
18
///
在系统运行开始初始化
19
///
Global.asax Application_Start内
20
///
</summary>
21
public
static
void
Init()
22
{
23
log4net.Config.XmlConfigurator.Configure();
24
}
25
///
<summary>
26
///
写入日志
27
///
</summary>
28
///
<param name="message">
日志信息
</param>
29
///
<param name="messageType">
日志类型
</param>
30
public
static
void
Write(
string
message, LogMessageType messageType)
31
{
32
DoLog(message, messageType,
null
, Type.GetType(
"
System.Object
"
));
33
}
34
///
<summary>
35
///
写入日志
36
///
</summary>
37
///
<param name="message">
日志信息
</param>
38
///
<param name="messageType">
日志类型
</param>
39
///
<param name="type"></param>
40
public
static
void
Write(
string
message, LogMessageType messageType, Type type)
41
{
42
DoLog(message, messageType,
null
, type);
43
}
44
///
<summary>
45
///
写入日志
46
///
</summary>
47
///
<param name="message">
日志信息
</param>
48
///
<param name="messageType">
日志类型
</param>
49
///
<param name="ex">
异常
</param>
50
public
static
void
Write(
string
message, LogMessageType messageType, Exception ex)
51
{
52
DoLog(message, messageType, ex, Type.GetType(
"
System.Object
"
));
53
}
54
///
<summary>
55
///
写入日志
56
///
</summary>
57
///
<param name="message">
日志信息
</param>
58
///
<param name="messageType">
日志类型
</param>
59
///
<param name="ex">
异常
</param>
60
///
<param name="type"></param>
61
public
static
void
Write(
string
message, LogMessageType messageType, Exception ex, Type type)
62
{
63
DoLog(message, messageType, ex, type);
64
}
65
///
<summary>
66
///
断言
67
///
</summary>
68
///
<param name="condition">
条件
</param>
69
///
<param name="message">
日志信息
</param>
70
public
static
void
Assert(
bool
condition,
string
message)
71
{
72
Assert(condition, message, Type.GetType(
"
System.Object
"
));
73
}
74
///
<summary>
75
///
断言
76
///
</summary>
77
///
<param name="condition">
条件
</param>
78
///
<param name="message">
日志信息
</param>
79
///
<param name="type">
日志类型
</param>
80
public
static
void
Assert(
bool
condition,
string
message, Type type)
81
{
82
if
(condition
==
false
)
83
Write(message, LogMessageType.Info);
84
}
85
///
<summary>
86
///
保存日志
87
///
</summary>
88
///
<param name="message">
日志信息
</param>
89
///
<param name="messageType">
日志类型
</param>
90
///
<param name="ex">
异常
</param>
91
///
<param name="type">
日志类型
</param>
92
private
static
void
DoLog(
string
message, LogMessageType messageType, Exception ex,
93
Type type)
94
{
95
m_log
=
LogManager.GetLogger(type);
96
switch
(messageType)
97
{
98
case
LogMessageType.Debug:
99
LogHelper.m_log.Debug(message, ex);
100
break
;
101
case
LogMessageType.Info:
102
LogHelper.m_log.Info(message, ex);
103
break
;
104
case
LogMessageType.Warn:
105
LogHelper.m_log.Warn(message, ex);
106
break
;
107
case
LogMessageType.Error:
108
LogHelper.m_log.Error(message, ex);
109
break
;
110
case
LogMessageType.Fatal:
111
LogHelper.m_log.Fatal(message, ex);
112
break
;
113
}
114
}
115
///
<summary>
116
///
日志类型
117
///
</summary>
118
public
enum
LogMessageType
119
{
120
///
<summary>
121
///
调试
122
///
</summary>
123
Debug,
124
///
<summary>
125
///
信息
126
///
</summary>
127
Info,
128
///
<summary>
129
///
警告
130
///
</summary>
131
Warn,
132
///
<summary>
133
///
错误
134
///
</summary>
135
Error,
136
///
<summary>
137
///
致命错误
138
///
</summary>
139
Fatal
140
}
141
}
142
}
143