【问题标题】:Colon in AppDomain causing problems with log4net.Util.PatternStringAppDomain 中的冒号导致 log4net.Util.PatternString 出现问题
【发布时间】:2013-02-19 12:07:32
【问题描述】:

我正在使用 ReSharper 7.1.1、NUnit 2.6.0 和 log4net 1.2.10。

在我的 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 提交拉取请求 - 尽管我不确定在这种情况下哪个是“错误”?

谢谢!

【问题讨论】:

    标签: nunit resharper log4net appdomain gallio


    【解决方案1】:

    这对我有用:

    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);
            }
        }
    }
    

    【讨论】:

    • 这对我有用,谢谢。我对代码做了一个小的更正,使文件成为覆盖(不是虚拟的)。
    【解决方案2】:

    我可以修改 log4net.Util.PatternString 以去掉冒号吗?

    这可能不是一个好主意,因为这最终可能会在其他地方删除冒号。

    您可以下载 log4net 源代码并在 file 属性上添加一些验证,但如果您不想这样做,您可以实现自己的 Appender 覆盖 File 属性,类似于

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

    如果这完全验证了名称而不是仅仅检查冒号,显然会更好。 (原本以为是去掉所有 Path.GetInvalidFileNameChars() 和 Path.GetInvalidPathChars() 字符的问题,但没有那么简单,所以我把它留给读者练习。)

    缺点是任何使用你的类的东西都需要能够找到它所包含的程序集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-11
      • 2021-05-16
      • 2015-09-07
      • 2012-11-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      相关资源
      最近更新 更多