forked from beetlex-io/ec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
193 lines (160 loc) · 6.1 KB
/
Utils.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EC
{
class Utils
{
static Utils()
{
log4net.Config.XmlConfigurator.Configure();
}
private static List<System.Reflection.Assembly> mAssemblies =null;
public static void LoadAssembly(Action<System.Reflection.Assembly> handler)
{
if (mAssemblies == null)
{
mAssemblies = new List<System.Reflection.Assembly>();
foreach (string dll in System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll"))
{
System.IO.FileInfo file = new System.IO.FileInfo(dll);
try
{
mAssemblies.Add(System.Reflection.Assembly.LoadFile(dll));
"load {0} assembly success".Log4Info(file.Name);
}
catch (Exception e_)
{
"load {0} assembly error".Log4Error(e_, file.Name);
}
}
foreach (string dll in System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.exe"))
{
System.IO.FileInfo file = new System.IO.FileInfo(dll);
try
{
mAssemblies.Add(System.Reflection.Assembly.LoadFile(dll));
"load {0} assembly success".Log4Info(file.Name);
}
catch (Exception e_)
{
"load {0} assembly error".Log4Error(e_, file.Name);
}
}
}
foreach (System.Reflection.Assembly a in mAssemblies)
{
handler(a);
}
}
private static Implement.TypeMapper mTypeMapper = null;
public static Implement.TypeMapper GetMessageMapping()
{
if (mTypeMapper == null)
{
mTypeMapper = new Implement.TypeMapper();
LoadAssembly(a =>
{
foreach (Type type in a.GetTypes())
{
MessageIDAttribute[] msgid = IKende.IKendeCore.GetTypeAttributes<MessageIDAttribute>(type, false);
if (msgid.Length > 0)
{
mTypeMapper.Register(msgid[0].ID, type);
Type lstType = Type.GetType("System.Collections.Generic.List`1");
"{0} register to {1}".Log4Info(type, msgid[0].ID);
if (lstType != null)
{
Type gLstType = lstType.MakeGenericType(type);
mTypeMapper.Register((short)(0 - msgid[0].ID), gLstType);
"{0} register to {1}".Log4Info(gLstType, msgid[0].ID);
}
}
foreach (TypeMappingAttribute tm in IKende.IKendeCore.GetTypeAttributes<TypeMappingAttribute>(type, false))
{
mTypeMapper.Register(tm.MessageID, tm.Type);
}
}
});
}
return mTypeMapper;
}
}
public static class StringExtensions
{
private static log4net.ILog Log()
{
return log4net.LogManager.GetLogger("EC");
}
public static void Log4Error(this String str)
{
Log().Error(str);
}
public static void Log4Fatal(this String str, Exception e)
{
Log().Fatal(str, e);
}
public static void Log4Fatal(this String str, params object[] parameters)
{
Log4Fatal(string.Format(str, parameters));
}
public static void Log4Fatal(this String str, Exception e, params object[] parameters)
{
Log4Fatal(string.Format(str, parameters), e);
}
public static void Log4Error(this String str, Exception e)
{
Log().Error(str, e);
}
public static void Log4Error(this String str, params object[] parameters)
{
Log4Error(string.Format(str, parameters));
}
public static void Log4Error(this String str, Exception e, params object[] parameters)
{
Log4Error(string.Format(str, parameters), e);
}
public static void Log4Info(this string str)
{
Log().Info(str);
}
public static void Log4Info(this string str, params object[] parameters)
{
Log4Info(string.Format(str, parameters));
}
public static void Log4Debug(this string str)
{
Log().Debug(str);
}
public static void Log4Debug(this string str, params object[] parameters)
{
Log4Debug(string.Format(str, parameters));
}
public static void Log4Warn(this string str)
{
Log().Warn(str);
}
public static void Log4Warn(this string str, params object[] parameters)
{
Log4Warn(string.Format(str, parameters));
}
public static void ThrowError<E>(this string str, Exception innerError = null) where E : Exception
{
Exception error;
if (innerError == null)
error = (Exception)Activator.CreateInstance(typeof(E), str);
else
error = (Exception)Activator.CreateInstance(typeof(E), str, innerError);
throw error;
}
public static void ThrowError<E>(this string str, params object[] parameters) where E : Exception
{
ThrowError<E>(string.Format(str, parameters));
}
public static void ThrowError<E>(this string str, Exception innerError = null, params object[] parameters) where E : Exception
{
ThrowError<E>(string.Format(str, parameters), innerError);
}
}
}