动态加载StackExchange.Exceptional

时间:2018-11-26 13:53:08

标签: c# asp.net dynamic dll stackexchange.exceptional

对于我公司的一个项目,我正在制作一个必须安装到许多不同网站的软件包。我们希望从错误记录器中获取错误(如果已安装)。并且目前愿意支持Elmah和Exceptional。

但是我的老板也只希望为3种不同的可能性分发1个包裹,这些包裹是:

  1. 已安装异常记录器的网站
  2. 已安装StackExchange.Exceptional的网站
  3. 已安装Elmah的网站

为使这些成为可能,我试图动态加载这些记录程序包的dll,并调用所需的方法或获取参数。

例如,通过执行以下操作,这对于Elmah来说是完全正常的:

Assembly assembly = Assembly.LoadFile(HostingEnvironment.MapPath("~/bin/Elmah.dll"));

Type type = assembly.GetType("Elmah.ErrorLog");

object[] parameters = new object[1];
parameters[0] = null;

var customResult = type.InvokeMember("GetDefault", BindingFlags.InvokeMethod, null, type, parameters);

Type myType = customResult.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

string result = "";

foreach (PropertyInfo prop in props)
{
    if (prop.Name == "Name")
        result = prop.GetValue(customResult, null).ToString();
}

现在只是获取默认ErrorLog的名称

然后进入“例外”并尝试类似的方法:

Assembly assembly = Assembly.LoadFile(HostingEnvironment.MapPath("~/bin/StackExchange.Exceptional.dll"));

Type type = assembly.GetType("StackExchange.Exceptional.ExceptionalModule");

object objectInstance = Activator.CreateInstance(type);

PropertyInfo info = type.GetProperty("ErrorStore");
foreach(PropertyInfo property in type.GetProperties())
{
    LogHelper.Info<Exceptional>(property.Name + ": " + property.GetValue(objectInstance, null));
}

这段代码是尝试获取使用的默认ErrorStore的ErrorStore的名称。带有一些测试日志等等。

然而,它必须立即使用“ ErrorStore”类的“ Default”参数执行任何操作,该参数应返回默认的ErrorStore。我总是会抛出异常:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentOutOfRangeException: ErrorStore 'type' must be specified
Parameternaam: settings
bij StackExchange.Exceptional.ErrorStore.GetFromSettings(ErrorStoreSettings settings)
bij StackExchange.Exceptional.ErrorStore.GetErrorStoreFromConfig()
bij StackExchange.Exceptional.ErrorStore.get_Default()
bij StackExchange.Exceptional.ExceptionalModule.get_ErrorStore()
--- Einde van intern uitzonderingsstackpad ---
bij System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
bij System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,   Object[] parameters, Object[] arguments)
bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
bij System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
bij System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
bij h5ysr_package.Exceptions.Exceptional.ExceptionalErrors()
bij h5ysr_package.DataCollectorSender.<StartCollection>d__3.MoveNext()

这使我进入引发错误的代码部分:

if (settings.Type.IsNullOrEmpty())
        throw new ArgumentOutOfRangeException(nameof(settings), "ErrorStore 'type' must be specified");

这是Exceptional软件包的一部分,因此我可以通过代码进行访问。

有没有办法确保在动态加载StackExchange.Exceptional dll时指定类型?还是只是不可行?

0 个答案:

没有答案