• log

20220308151625577

  • code
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
136
137
138
139
140
141
using System;
using log4net;

namespace MapGIS.GMSystem.Plugin
{
public class Log
{
public static void Info(string msg)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
int line = st.GetFrame(0).GetFileLineNumber();
string file = st.GetFrame(0).GetFileName();
string method = st.GetFrame(0).GetMethod().Name;
string level = System.Reflection.MethodBase.GetCurrentMethod().Name.ToUpper();

string log = getLog(level, msg, file, method, line);

ILog logger = LogManager.GetLogger("logger");
logger.Info(log);
}
public static void Error(string msg)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
int line = st.GetFrame(0).GetFileLineNumber();
string file = st.GetFrame(0).GetFileName();
string method = st.GetFrame(0).GetMethod().Name;
string level = System.Reflection.MethodBase.GetCurrentMethod().Name.ToUpper();

string log = getLog(level, msg, file, method, line);

ILog logger = LogManager.GetLogger("logger");
logger.Error(log);
}
public static void Debug(string msg)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
int line = st.GetFrame(0).GetFileLineNumber();
string file = st.GetFrame(0).GetFileName();
string method = st.GetFrame(0).GetMethod().Name;
string level = System.Reflection.MethodBase.GetCurrentMethod().Name.ToUpper();

string log = getLog(level, msg, file, method, line);

ILog logger = LogManager.GetLogger("logger");
logger.Debug(log);
}
public static void Warn(string msg)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
int line = st.GetFrame(0).GetFileLineNumber();
string file = st.GetFrame(0).GetFileName();
string method = st.GetFrame(0).GetMethod().Name;
string level = System.Reflection.MethodBase.GetCurrentMethod().Name.ToUpper();

string log = getLog(level, msg, file, method, line);

ILog logger = LogManager.GetLogger("logger");
logger.Warn(log);
}

public static void Info(Exception ex)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
int line = st.GetFrame(0).GetFileLineNumber();
string file = st.GetFrame(0).GetFileName();
string method = st.GetFrame(0).GetMethod().Name;
string level = System.Reflection.MethodBase.GetCurrentMethod().Name.ToUpper();

string log = getLog(level, ex, file, method, line);

ILog logger = LogManager.GetLogger("logger");
logger.Info(log);
}
public static void Error(Exception ex)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
int line = st.GetFrame(0).GetFileLineNumber();
string file = st.GetFrame(0).GetFileName();
string method = st.GetFrame(0).GetMethod().Name;
string level = System.Reflection.MethodBase.GetCurrentMethod().Name.ToUpper();

string log = getLog(level, ex, file, method, line);

ILog logger = LogManager.GetLogger("logger");
logger.Error(log);
}
public static void Debug(Exception ex)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
int line = st.GetFrame(0).GetFileLineNumber();
string file = st.GetFrame(0).GetFileName();
string method = st.GetFrame(0).GetMethod().Name;
string level = System.Reflection.MethodBase.GetCurrentMethod().Name.ToUpper();

string log = getLog(level, ex, file, method, line);

ILog logger = LogManager.GetLogger("logger");
logger.Debug(log);
}
public static void Warn(Exception ex)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
int line = st.GetFrame(0).GetFileLineNumber();
string file = st.GetFrame(0).GetFileName();
string method = st.GetFrame(0).GetMethod().Name;
string level = System.Reflection.MethodBase.GetCurrentMethod().Name.ToUpper();

string log = getLog(level, ex, file, method, line);

ILog logger = LogManager.GetLogger("logger");
logger.Warn(log);
}

static string getLog(string level, string msg, string file, string method, int line)
{
return $"<HR COLOR={getColor(level.ToUpper())} SIZE=3>日志时间:{DateTime.Now:G} <BR>日志级别:{level.ToUpper()} <BR>日志信息:{msg} <BR>位置信息:{file} -> {method}() <BR> -> {line}<HR Size=2>";
//return $"<HR COLOR={getColor(level.ToUpper())}>日志时间:{DateTime.Now:G} <BR>日志级别:{level.ToUpper()} <BR>日志信息:{msg} <BR>位置信息:{file} <BR>方 法 名:{method}() <BR>行 号:{line}<HR Size=2>";
}

static string getLog(string level, Exception ex, string file, string method, int line)
{
return $"<HR COLOR={getColor(level.ToUpper())} SIZE=3>日志时间:{DateTime.Now:G} <BR>日志级别:{level.ToUpper()} <BR>日志信息:{ex.GetType().FullName}: {ex.Message} <BR>位置信息:{file} -> {method}() -> {line}<HR Size=2>";
//return $"<HR COLOR={getColor(level.ToUpper())}>日志时间:{DateTime.Now:G} <BR>日志级别:{level.ToUpper()} <BR>日志信息:{ex.GetType().FullName}: {ex.Message} <BR>位置信息:{file} <BR>方 法 名:{method}() <BR>行 号:{line}<HR Size=2>";
}
static string getColor(string level)
{
switch (level)
{
case "INFO":
return "Green";
case "DEBUG":
return "Blue";
case "WARN":
return "Orange";
case "ERROR":
return "Red";
}
return "Black";
}
}
}
  • log4net.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
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Level的级别,由高到低 -->
<!-- None > Fatal > ERROR > WARN > DEBUG > INFO > ALL-->
<!-- 解释:如果level是ERROR,则在cs文件里面调用log4net的info()方法,则不会写入到日志文件中-->
<log4net>
<!--错误日志类-->
<logger name="logger">
<!--日志类的名字-->
<level value="ALL" />
<!--定义记录的日志级别-->
<appender-ref ref="LoggerAppender" />
<!--记录到哪个介质中去-->
</logger>
<!--错误日志附加介质-->
<appender name="LoggerAppender" type="log4net.Appender.RollingFileAppender">
<!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->
<param name="File" value="\Package\GMSystem\LogShNet\" />
<!--日志输出到exe程序这个相对目录下-->
<param name="AppendToFile" value="false" />
<!--输出的日志不会覆盖以前的信息-->
<param name="MaxSizeRollBackups" value="100" />
<!--备份文件的个数-->
<param name="MaxFileSize" value="1024" />
<!--当个日志文件的最大大小-->
<param name="StaticLogFileName" value="false" />
<!--是否使用静态文件名-->
<param name="DatePattern" value="yyyyMMdd&quot;.html&quot;" />
<!--日志文件名-->
<param name="RollingStyle" value="Date" />
<!--文件创建的方式,这里是以Date方式创建-->
<!--错误日志布局-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
</log4net>
</configuration>
  • AssemblyInfo.cs
1
2
//加载log4net配置文件
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]

“config”, Watch = true)]

1
2

]