2

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

谢谢!

4

2 回答 2

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() 字符的问题,但没有那么简单,所以我把它留给读者练习。)

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

于 2013-02-19T15:17:01.820 回答
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);
        }
    }
}
于 2013-07-22T22:53:58.623 回答