如何从Web.config修改异常处理值

时间:2015-01-05 20:10:54

标签: c# exception enterprise-library

我开始使用异常处理,在我的情况下,我使用配置管理器,使用Replace Handler创建Null Policie,在这种情况下,替换为此异常的消息是下一个:&#34 ;存在一些空值:" ...我的web.config:

<exceptionHandling>
    <exceptionPolicies>
      <add name="Null Policies">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="Exists some null values" replaceExceptionType="System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

在我的一段代码中,我知道抛出一个NullReferenceException我想添加抛出此异常的值,并附加到之前在配置上实现的消息:

try
 {
    string theValue = anotherThing //this piece of code throw a NullReferenceException 
 }
 catch(Exception ex)
 {
    if (exManager.HandleException(ex,"Null Policies")) throw; //add 'theValue' to the new Exception
    return "";
 }

研究我发现Configuration类可以访问web.config ...而且我不知道如何访问这个特定的XML部分 - &gt; exceptionPolicies&lt; - 并修改属性,如:exceptionMessage =&#34;存在一些空值&#34; + theValue;

希望你能帮助我......

0 个答案:

没有答案