反射加非同步,根據枚舉值來非同步執行方法:
protected delegate void AsyncWriteLogMsgEvent(string message);
protected delegate void AsyncWriteLogExceptionEvent(string message, Exception exception);
internal void Log(LogLevel level, string message)
{
Type thisType = this.GetType();
MethodInfo mi = thisType.GetMethod(SiteExtensions.GetEnumName(level));
if (mi == null || !mi.IsPublic || mi.IsStatic || mi.IsConstructor) return;
AsyncWriteLogMsgEvent writer = m => mi.Invoke(this, new object[] { m });
writer.BeginInvoke(message, null, null);
}
internal void Log(LogLevel level, string message, Exception exception)
{
Type thisType = this.GetType();
MethodInfo mi = thisType.GetMethod(SiteExtensions.GetEnumName(level));
if (mi == null || !mi.IsPublic || mi.IsStatic || mi.IsConstructor) return;
AsyncWriteLogExceptionEvent writer = (m, e) => mi.Invoke(this, new object[] {m, e});
writer.BeginInvoke(message, exception, null, null);
}