Nunit AppDomain中的冒号导致log4net.Util.PatternString出现问题

Nunit AppDomain中的冒号导致log4net.Util.PatternString出现问题,nunit,resharper,log4net,appdomain,gallio,Nunit,Resharper,Log4net,Appdomain,Gallio,我正在使用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" /> <append

我正在使用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%appdomain值取自appdomain.CurrentDomain.FriendlyName值,即:

IsolatedAppDomainHost: MyProject.Tests
由于此AppDomain名称包含冒号,因此无法将其转换为文件名,即%AppDomain.log变为IsolatedAppDomainHost:MyProject.Tests.log

我正在寻找一些解决方法的建议:

我是否可以以某种方式覆盖AppDomain值,仅用于单元测试项目? 我可以修改log4net.Util.PatternString,使其去掉冒号吗? 我可以配置ReSharper测试运行程序以避免此问题吗? 我正在使用的工具的更新版本中是否已修复了此问题?我在别处找不到有人提到这个问题。 如果没有,我可以尝试向Gallio或log4net提交一个pull请求——尽管我不确定在这种情况下哪个是错误的

谢谢

我可以修改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字符的问题,但它并没有那么简单,所以我把它留给读者作为练习

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

我可以修改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字符的问题,但它并没有那么简单,所以我把它留给读者作为练习

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

这对我来说很有用:

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);
        }
    }
}
这对我很有用:

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

这对我有用,谢谢。我对代码做了一个小的修改,使文件成为一个覆盖而不是虚拟的。这对我来说很有用,谢谢。我对代码做了一个小的修改,使文件成为覆盖而不是虚拟的。