huorswords / Microsoft.Extensions.Logging.Log4Net.AspNetCore

Allows to configure Log4net as Microsoft Extensions Logging handler on any ASP.NET Core application. Original code proposal by @anuraj --> https://dotnetthoughts.net/how-to-use-log4net-with-aspnetcore-for-logging/
Apache License 2.0
249 stars 123 forks source link

Need Example Logging Facade Use Outside of ASP #130

Closed JasonAtDrumBrae closed 1 year ago

JasonAtDrumBrae commented 1 year ago

I believe its possible to use this purely as a façade for logging, so no Startup class or JSON config. Just a Program.cs or in NUnit3, SetUp.cs. How would we create a LoggerFactory instance with the Log4Net.confog file at a non default location?

JasonAtDrumBrae commented 1 year ago

namespace TestMicrosoftExtensions { using Microsoft.Extensions.Logging; using NUnit.Framework;

/// <summary>
/// The setup fixture.
/// </summary>
[SetUpFixture]
public class SetUpFixture
{
    /// <summary>
    /// The test suite log.
    /// </summary>
    private readonly ILogger<SetUpFixture> m_clsLog =
        PLoggerFactory.CreateLogger<SetUpFixture>();

    /// <summary>
    /// Initializes static members of the <see cref="SetUpFixture" /> class.
    /// </summary>
    static SetUpFixture()
    {
        PLoggerFactory = LoggerFactory.Create(
            clsBuilder => clsBuilder.AddLog4Net(@".\Logging\Log4Net.config")
                .SetMinimumLevel(LogLevel.Debug));
    }

    /// <summary>
    /// Gets the Logger Factory instance.
    /// </summary>
    public static ILoggerFactory PLoggerFactory { get; }

    /// <summary>
    /// The fixture setup.
    /// </summary>
    [OneTimeSetUp]
    public void OneTimeSetUp()
    {
        m_clsLog.LogInformation("OneTimeSetUp()");
    }

    /// <summary>
    /// The fixture tear down.
    /// </summary>
    [OneTimeTearDown]
    public void OneTimeTearDown()
    {
        m_clsLog.LogInformation("OneTimeTearDown()");
    }
}

}

huorswords commented 1 year ago

Hello @JasonNewellAtAllegro.

This repository is not specific in how to use log4net outside Microsoft.Extensions.Logging system. Maybe I didn't understand you well, but I think that you should open same question in the log4net repository instead here.

I'm going to close this one. Sorry for inconveniences.