1

I want to get custom data from the log4net.Core.LoggingEvent object inside Append method of my custom log4net appender.

public sealed class SimpleAppender : AppenderSkeleton
{
    protected override void Append(LoggingEvent loggingEvent)
    {
        // How can I receive my data?
    }
}

I know that log4net.Core.LoggingEvent contains Properties property. It has type log4net.Util.PropertiesDictionary. A logger or an appender may attach additional properties to specific events, but I'm not sure that it is what the way I'm looking for. I need to get testId here. I created that testId in the method marked [TestInitialize] attribute.

public class UnitTest1
{
    public TestContext TestContext { get; set; }

    [TestInitialize]
    public void Initialize()
    {
        Guid testId = Guid.NewGuid();
    }

    [TestMethod]
    public void TestMethod1()
    {
        ApplicationLogger.LogInfo("TestMethod1 has started.");
    }       
}

ApplicationLogger class is a simple log4netlogger wrapper which prints result into console. I know that I can do something like this.

ApplicationLogger.LogInfo("Test message", testId);

public static class ApplicationLogger
{
    private static readonly log4net.ILog log4netLogger;

    public static void LogInfo(Guid testId, string infoMessage)
    {
        ApplicationLogger.log4netLogger.Info(new LogMessageModel
        {
            Text = infoMessage,
            TestId = testId
        });
    }
}

But I don't want to set testId into LogInfo() method every time I need to log something. Is there another more more elegant way? Thanks in advance.

ikaikastine
  • 601
  • 8
  • 22
Joseph Katzman
  • 1,959
  • 6
  • 21
  • 47
  • Add a static Initialize() function to ApplicationLogger that accepts testId as a parameter and call ApplicationLogger.Initialize(testId) from UnitTest1.Initialize()? – hsirah May 02 '18 at 16:57

1 Answers1

0

Add a static Initialize() function to ApplicationLogger that accepts testId as a parameter and call ApplicationLogger.Initialize(testId) from UnitTest1.Initialize().

hsirah
  • 337
  • 1
  • 10