如何为每个单独的测试用例将测试用例名称赋予 log4net 日志文件

How to give testcase name to log4net log file for every separate test case

我尝试为我的自动化框架实施 log4net。 我没有写 XML 配置文件。我只使用代码文件来配置它。

 class log4netHelper
{    
        private static ILog _logger;
        private static ConsoleAppender _conAppender;
        private static FileAppender _fileAppender;
        private static RollingFileAppender _rollingFileAppender;
        private static string _layout = "%date{ABSOLUTE} [%class] [%level] [%method] - %message%newline";


        public static string Layout
        {
            set { _layout = value; }
        }

        private static PatternLayout GetPatternLayout()
        {
            var patternLayout = new PatternLayout()
            {
                ConversionPattern = _layout
            };

            patternLayout.ActivateOptions();
            return patternLayout;
        }
        private static ConsoleAppender GetConsoleAppender()
        {
            var consoleAppender = new ConsoleAppender()
            {
                Name = "ConsoleAppender",
                Layout = GetPatternLayout(),
                Threshold = Level.Error
            };
            consoleAppender.ActivateOptions();
            return consoleAppender;
        }
        private static FileAppender GetFileAppender()
        {
            var fileAppender = new FileAppender()
            {
                Name = "FileAppender",
                Layout = GetPatternLayout(),
                Threshold = Level.All,
                AppendToFile = false,
                File = @"C:\FileLogger.log"

            };
            fileAppender.ActivateOptions();
            return fileAppender;
        }
        private static RollingFileAppender GetRollingFileAppender()
        {
            var rollingFileAppender = new RollingFileAppender()
            {
                Name = "RollingFileAppender",
                Layout = GetPatternLayout(),
                Threshold = Level.All,
                AppendToFile = true,
                File = "RollingfileLogger.log",
                MaximumFileSize = "1MB",
                MaxSizeRollBackups = 15


            };
            rollingFileAppender.ActivateOptions();
            return rollingFileAppender;
        }

        public static ILog GetLogger([CallerFilePath]string filename = "")
        {

            if (_conAppender == null)
                _conAppender = GetConsoleAppender();
            if (_fileAppender == null)
                _fileAppender = GetFileAppender();
            if (_rollingFileAppender == null)
                _rollingFileAppender = GetRollingFileAppender();
            BasicConfigurator.Configure(_conAppender, _fileAppender, _rollingFileAppender);

            return LogManager.GetLogger(filename);

        }
    }

我想要每个测试用例 运行 的测试用例名称,而不是 FileLogger.log 文件。 我无法就如何执行此操作提出任何解决方案。 我尝试更改 GetAppender() 中的 File 变量值但无法做到。 还尝试在测试用例中传递参数文件名,但文件仍然是由 FileLogger 的名称创建的。

这只是一个如何使用 TestName 作为日志文件名的概念,但您会有所了解。

using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTest
{
    public static class Log4NetHelper
    {
        private static readonly string _layout = "%date{ABSOLUTE} [%class] [%level] [%method] - %message%newline";
        private static readonly string _appenderName = "FileAppender";

        private static PatternLayout GetPatternLayout()
        {
            PatternLayout patternLayout = new PatternLayout
            {
                ConversionPattern = _layout
            };

            patternLayout.ActivateOptions();

            return patternLayout;
        }

        private static FileAppender GetFileAppender(string fileName)
        {
            var fileAppender = new FileAppender
            {
                Name = _appenderName,
                Layout = GetPatternLayout(),
                Threshold = Level.All,
                AppendToFile = false,
                File = $@"C:\temp\{fileName}.log"
            };

            fileAppender.ActivateOptions();

            return fileAppender;
        }

        public static ILog GetLogger(string filename)
        {
            // Remember to clear old logger
            Logger root = ((Hierarchy)LogManager.GetRepository()).Root;
            root.RemoveAppender(_appenderName);

            BasicConfigurator.Configure(GetFileAppender(filename));

            return LogManager.GetLogger(filename);
        }
    }

    [TestClass]
    public class MyTestClass
    {
        public TestContext TestContext { get; set; }

        private ILog log;

        [TestInitialize]
        public void TestInitialize()
        {
            log = Log4NetHelper.GetLogger($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}");
        }

        [TestMethod]
        public void TestMethod1()
        {
            log.Info("This is my log message from TestMethod1");

            Assert.IsTrue(true);
        }

        [TestMethod]
        public void TestMethod2()
        {
            log.Info("This is my log message from TestMethod2");

            Assert.IsTrue(true);
        }
    }
}

这将在 C:\logs 目录中生成 2 个文件:

  • UnitTest.MyTestClass.TestMethod1.log
  • UnitTest.MyTestClass.TestMethod2.log

每个都有一条日志消息。