【发布时间】:2013-12-10 12:10:57
【问题描述】:
我创建了一个非常基本的日志记录块和异常处理块。在我的代码中,我去测试异常处理(我知道日志记录块有效):
public void RunScriptClean()
{
try
{
throw new FileNotFoundException();
}
catch (FileNotFoundException ex)
{
var b = ExceptionPolicy.HandleException(ex, "Logging Policy");
if (b)
throw;
}
}
但是,在 catch 块的第一行,我得到了这个冗长的异常并且我的应用程序崩溃了:
Exception occured: The current build operating (build key Build Key [Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, Logging Policy]) failed: The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' cannot be resolved. Please verify the spelling is correct or that the full type name is provided. (Strategy type ConfiguredObjectStrategy, index 2).
当它说类型无法解析时,我完全不知道它指的是什么。我添加了对 Microsoft.Practices.EnterpriseLibrary.Common/ExceptionHandling/Logging 和 Ms.Practices.ObjectBuilder2 的引用。这一类的顶部包含using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling。
添加了查看我的 AppConfig 文件的配置工具的屏幕截图:
我确定我遗漏了一些基本的东西,但很难找到 EAB 4.1 的教程——CodeProject 有很多原始版本,但我无法制作太多...
编辑我尝试创建一个新的格式化程序并将其命名为TextExceptionFormatter,但这并没有改变任何东西。不确定我的 Logging Handler 上的 FormatterType 属性是否与该节点相关联。
以及来自 App.config 的实际 XML 块:
<exceptionHandling>
<exceptionPolicies>
<add name="Logging Policy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" name="Exception">
<exceptionHandlers>
<add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Logging Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
我发现了这个 SO 问题:Cannot resolve type runtime error after registering the Enterprise Library into the GAC,但即使在更改了 fullName 属性的版本段之后,我的应用程序的行为仍然相同。
【问题讨论】:
-
该错误消息和屏幕截图都告诉您您的参考有问题。异常消息包含
PublicKeyToken = %token%' which is obviously not right. And your config appears to contain an exception of its own, look at theTitle` 属性值。 -
我只是用那个替换了 token 的值,抱歉——它很长而且看起来很随意。也许我错了!就“标题”而言,我真的不确定那里属于什么,我认为它只是被用作日志文件中条目的标题。
Title. This is the title of the log entry. The default value is Enterprise Library Exception Handling. You can change the title if you want to.- MSDN。而Token值就是截图中的Formatter Type。 -
我对标题的看法是,从屏幕截图来看,它似乎在尝试解决该问题时抛出了异常。就个人而言,我从不使用配置工具,我只是查看实际的 app.config。你能发布相关的细节吗?
-
好的,已编辑以包含实际的 XML
-
你为什么不使用最新版本的应用程序块,6.0?
标签: c# winforms exception-handling .net-3.5 enterprise-library