nunitresharperlog4netappdomaingallio

Colon in AppDomain causing problems with log4net.Util.PatternString


I'm using ReSharper 7.1.1, NUnit 2.6.0 and log4net 1.2.10.

In my log4net configuration I have a 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>

I get the following error when my unit test code runs:

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)

The reason for this is that the log4net %appdomain value is taken from the AppDomain.CurrentDomain.FriendlyName value, which is:

IsolatedAppDomainHost: MyProject.Tests

Because this AppDomain name contains a colon, it's unable to convert it to a filename, i.e. %appdomain.log becomes IsolatedAppDomainHost: MyProject.Tests.log

I'm looking for some suggestions for a workaround:

If not, I can try and submit a pull request to either Gallio or log4net - though I'm not sure which is "at fault" in this case?

Thanks!


Solution

  • This worked for me:

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