Log4netHelper.cs
3.87 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.Diagnostics;
using System.Reflection;
using log4net;
using log4net.Repository;
namespace Hh.Mes.Common.log
{
/// <summary>
///Log4
/// </summary>
public class Log4NetHelper
{
/// <summary>
/// log4net 仓储
/// </summary>
public ILoggerRepository Repository { get; set; }
private static volatile Log4NetHelper _instance = null;
private static readonly object LockHelper = new object();
/// <summary> Gets the instance
/// </summary>
public static Log4NetHelper Instance
{
get
{
if (_instance == null)
{
lock (LockHelper)
{
if (_instance == null)
{
_instance = new Log4NetHelper();
}
}
}
return _instance;
}
}
private Log4NetHelper()
{
}
/// <summary>
/// 获取日志初始化器
/// </summary>
/// <returns></returns>
private ILog Init(string type)
{
return LogManager.GetLogger(Repository.Name, type);
}
public void Error(string message)
{
StackTrace trace = new StackTrace();
var className = trace.GetFrame(1).GetMethod().DeclaringType;
//获取方法名称
MethodBase method = trace.GetFrame(1).GetMethod();
var type = "className:" + className.Namespace + "\r\n\rMethodName:" + method.Name;
//记录日志
WriteLog(LogLevel.Error, message, type);
}
public void Warning(string message)
{
StackTrace trace = new StackTrace();
var className = trace.GetFrame(1).GetMethod().DeclaringType;
MethodBase method = trace.GetFrame(1).GetMethod();
var type = "className:" + className.Namespace + "\r\n\rMethodName:" + method.Name;
WriteLog(LogLevel.Warning, message, type);
}
public void Info(string message)
{
StackTrace trace = new StackTrace();
var className = trace.GetFrame(1).GetMethod().DeclaringType;
MethodBase method = trace.GetFrame(1).GetMethod();
var type = "className:" + className.Namespace + "\r\n\rMethodName:" + method.Name;
WriteLog(LogLevel.Info, message, type);
}
public void Debug(string message)
{
StackTrace trace = new StackTrace();
var className = trace.GetFrame(1).GetMethod().DeclaringType;
MethodBase method = trace.GetFrame(1).GetMethod();
var type = "className:" + className.Namespace + "\r\n\rMethodName:" + method.Name;
WriteLog(LogLevel.Debug, message, type);
}
/// <summary>
/// 写日志
/// </summary>
/// <param name="logLevel">日志等级</param>
/// <param name="message">日志信息</param>
/// <param name="type">类名 方法名</param>
private void WriteLog(LogLevel logLevel, string message, string type)
{
ILog log = Init(type);
switch (logLevel)
{
case LogLevel.Debug:
log.Debug(message);
break;
case LogLevel.Error:
log.Error(message);
break;
case LogLevel.Info:
log.Info(message);
break;
case LogLevel.Warning:
log.Warn(message);
break;
}
}
}
/// <summary>
/// 日志等级
/// </summary>
public enum LogLevel
{
Error,
Debug,
Warning,
Info
}
}