C#创建一个封装log4net简单类的单元测试

log4net.config

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="log4net"
      type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <log4net>

    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="AdoNetAppender" />
    </root>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern"
           value="%d [%c] %-5p %n- %m%n%n"/>
      </layout>
    </appender>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <file value="logs\log.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="1MB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" 
               value="%utcdate [%c] %-5p - %m   [%exception]%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="FATAL" />
      </filter>
    </appender>
  </log4net>

</configuration>

SPT.Helper
Mylogger 基于log4net简单封装的类

 public static class MyLogger
    {
        private static ILog _logger;
        private static ILog Logger
        {
            get
            {
                if (_logger == null)
                {
                    //Set log configure file to .\log4net.Config
                    log4net.Config.XmlConfigurator.Configure(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.Config"));
                    //Set logger name for current application
                    string loggerName = AppDomain.CurrentDomain.FriendlyName;
                    loggerName = loggerName.Replace(".exe", "");
                    _logger = log4net.LogManager.GetLogger(loggerName);
                }
                return _logger;
            }
        }
        public static void Debug(string msg)
        {
            Logger.Debug(msg);
        }

        public static void Info(string msg)
        {
            Logger.Info(msg);
        }

        public static void Warn(string msg)
        {
            Logger.Warn(msg);
        }

        public static void Error(string msg, Exception ex)
        {
            Logger.Error(msg, ex);
        }
    }

UnitTest.Project

引用SPT.Helper的引用,对MyLogger进行单元测试

 [TestClass]
    public class MyLoggerTest
    {
        [TestMethod]
        public void DebugTest()
        {
            MyLogger.Debug("123");
        }
    }

现在DebugTest()可以Run通过,但是在本地找不到输出的log.txt文件,请问应该怎么配置才能在UnitTest项目中输入对应的log文件,或者是这样的类应该怎么写单元测试。求教!

1个回答

http://www.cnblogs.com/jys509/p/4569874.html

 <!--日志路径-->
      <param name= "File" value= "D:\App_Log\"/>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐