2013-02-19 76 views
2

我使用ReSharper 7.1.1,NUnit 2.6.0和log4net 1.2.10。冒用AppDomain導致log4net.Util.PatternString出現問題

在我的log4net的配置我有一個RollingFileAppender進行:

<appender name="file" type="log4net.Appender.RollingFileAppender"> 
    <file type="log4net.Util.PatternString" value="%appdomain.log" /> 
    <appendToFile value="true" /> 
    <rollingStyle value="Size" /> 
    <maxSizeRollBackups value="0" /> 
    <maximumFileSize value="5MB" /> 
    <staticLogFileName value="true" /> 
    <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%utcdate{ISO8601} %-5level - %message%newline" /> 
    </layout> 
    <threshold value="ALL" /> 
</appender> 

我收到以下錯誤,當我的單元測試代碼運行:

log4net:ERROR XmlHierarchyConfigurator: Could not create Appender [file] of type [log4net.Appender.RollingFileAppender]. Reported error follows. 
System.NotSupportedException: The given path's format is not supported. 
    at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath) 
    at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath) 
    at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) 
    at System.IO.Path.GetFullPath(String path) 
    at log4net.Util.SystemInfo.ConvertToFullPath(String path) 
    at log4net.Appender.RollingFileAppender.ActivateOptions() 
    at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement) 

這樣做的原因是,log4net的%appdomain值取自AppDomain.CurrentDomain.FriendlyName值,即:

IsolatedAppDomainHost: MyProject.Tests 

因爲這個AppDomain的名稱包含一個冒號,這是無法將其轉換爲一個文件名,即%appdomain.log成爲IsolatedAppDomainHost: MyProject.Tests.log

我正在尋找的解決方法,提出了一些建議:

  • 我可以覆蓋在AppDomain值不知何故,只爲單元測試項目?
  • 我可以修改log4net.Util.PatternString以便去掉冒號嗎?
  • 我可以配置ReSharper測試運行器以避免此問題嗎?
  • 這是否已在我使用的任何工具的更新版本中修復?在其他地方我找不到這個問題。

如果不是,我可以嘗試向Gallio或log4net提交pull請求 - 雖然我不確定在這種情況下哪個「出錯」?

謝謝!

回答

2

這爲我工作:

public class SafeRollingFileAppender : log4net.Appender.RollingFileAppender 
{ 
    public override string File 
    { 
     get { return base.File; } 
     set 
     { 
      //remove that pesky colon 
      string newValue = value.Replace(":", ""); 

      //now do some general purpose cleanup 
      string dir = Path.GetDirectoryName(newValue); 
      string file = Path.GetFileName(newValue); 

      string dirRegexSearch = new string(Path.GetInvalidPathChars()); 
      Regex dr = new Regex(string.Format("[{0}]", Regex.Escape(dirRegexSearch))); 
      string newDir = dr.Replace(dir, ""); 

      string fileRegexSearch = new string(Path.GetInvalidFileNameChars()); 
      Regex fr = new Regex(string.Format("[{0}]", Regex.Escape(fileRegexSearch))); 
      string newFile = fr.Replace(file, ""); 

      base.File = Path.Combine(newDir, newFile); 
     } 
    } 
} 
+0

這對我的作品,謝謝。我對代碼做了一個小修改,使文件成爲覆蓋(而不是虛擬)。 – tjrobinson 2013-07-31 14:10:59

2

我可以修改log4net.Util.PatternString,使其去掉冒號嗎?

可能不是一個好主意,因爲這可能最終會在其他地方剝離冒號。

你可以下載log4net的源代碼,並添加文件的屬性一些驗證,但如果你不想這樣做,你可以實現自己的Appender將覆蓋文件屬性,像

public class SaferRollingFileAppender : log4net.Appender.RollingFileAppender 
{  
    virtual public string File 
    { 
     get { return base.File ; } 
     set { base.File = value.Replace(":",""); } 
    } 
} 

如果這完全驗證了名稱而不僅僅是檢查冒號,顯然會更好。 (原本以爲這是刪除所有Path.GetInvalidFileNameChars()和Path.GetInvalidPathChars()字符的問題,但它不那麼簡單,所以我把它作爲讀者的練習。)

下降就是說,任何使用你的類的東西都需要能夠找到它所包含的程序集。

相關問題