2012-02-10 15 views
2

我需要改進app.config文件中的錯誤報告。如何檢測app.config文件中的值語法錯誤?

我有一個很小的測試應用程序,其中包含一個「int」類型的設置。如果我將其在app.config中的值更改爲不是有效整數的值,我希望得到一個可以捕獲和報告的異常。不幸的是,有些東西正在吃掉例外。有沒有一種簡單的方法來防止這種行爲並讓異常傳播出去?

我Settings.Designer.cs(由Visual Studio生成的)文件:

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(
    "Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] 
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { 

    private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 

    public static Settings Default { get { return defaultInstance; } } 

    [global::System.Configuration.UserScopedSettingAttribute()] 
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 
    [global::System.Configuration.DefaultSettingValueAttribute("17")] 
    public int SecondSetting 
    { 
     get { return ((int)(this["SecondSetting"])); } 
     set { this["SecondSetting"] = value; } 
    } 
} 

我的C#測試程序:

static void Main (string[] args) 
{ 
    try 
    { 
     Console.WriteLine(AppConfigTests.Properties.Settings.Default.SecondSetting); 
    } 
    catch (Exception x) 
    { 
     Console.WriteLine(x.ToString()); 
    } 
} 

我App.config文件的相關部分(注意:值不是有效整數):

<userSettings> 
    <AppConfigTests.Properties.Settings> 
     <setting name="SecondSetting" serializeAs="String"> 
      <value>1foo7</value> 
     </setting> 
    </AppConfigTests.Properties.Settings> 
</userSettings> 

System.Format異常正在由System.Number.StringT引發oNumber,但有些東西在捕捉它並將它扔掉,我的catch塊永遠不會進入。在Visual Studio調試器輸出中,我發現「在mscorlib.dll中發生了類型'System.FormatException'的第一次機會異常」,但除了進一步確認引發異常外,沒有任何幫助。

我嘗試在Settings.Designer.cs中的Setting屬性中添加一個IntegerValidatorAttribute,但這並沒有幫助(並且確保.cs不會被重新生成)。

我想在我的main()方法的頂部添加以下代碼,但是這並沒有幫助:

foreach (SettingsProperty sp in AppConfigTests.Properties.Settings.Default.Properties) 
    sp.ThrowOnErrorDeserializing = true; 

我想實現我自己的配置節的,但我希望有一個更簡單的解決方案。

只是爲了重申:我需要一種方式來報告app.config文件中設置值的錯誤。

(如果我打破的App.config XML語法(刪除尖括號,例如),我的catch塊得到一個不錯的例外與所有的細節,我可以問。)

+0

我很好奇你爲此做了什麼,因爲我有類似的問題... – JWiley 2012-11-19 15:15:54

回答

1

你可以做像這樣(假設你所有的app.config重寫設定值):

foreach (SettingsPropertyValue propertyValue in AppConfigTests.Properties.Settings.Default.PropertyValues) { 
    if (propertyValue.UsingDefaultValue) { 
     throw new Exception(propertyValue.Name + " is not deserialized properly."); 
    } 
    } 

如果你正在編寫的Web應用程序,它是激發此事件時反序列化失敗:

try { 
    if (this.IsHostedInAspnet()) { 
    object[] args = new object[] { this.Property, this, ex }; 
    Type type = Type.GetType("System.Web.Management.WebBaseEvent, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true); 
    type.InvokeMember("RaisePropertyDeserializationWebErrorEvent", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, args, CultureInfo.InvariantCulture); 
    } 
} 
catch { 
} 

否則,它只是返回到默認值,因此只有您可以迭代所有值並檢查它們是否未違約。