Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging: Add own factory / providers #14

Open
nad-au opened this issue Jul 22, 2021 · 0 comments
Open

Logging: Add own factory / providers #14

nad-au opened this issue Jul 22, 2021 · 0 comments

Comments

@nad-au
Copy link

nad-au commented Jul 22, 2021

Logging doesn't appear to work (at least for me). From what I can see LoggerProvider creates its own LoggerFactory with which there is no access to be able to add providers. If I am mistaken here please forgive me and let me know if there's a way to set-up those providers.

Otherwise can I suggest adding a public static method to inject one's own ILoggerFactory? For example:

using Microsoft.Extensions.Logging;

namespace Banzai.Logging
{
    /// <summary>
    ///     Provides loggers via the static LoggerFactory to the rest of the application
    /// </summary>
    public static class LoggerProvider
    {
        /// <summary>
        ///     Adds existing logger factory.
        /// </summary>
        /// <param name="loggerFactory"></param>
        public static void AddLoggerFactory(ILoggerFactory loggerFactory)
        {
            LoggerFactory = loggerFactory;
        }

        private static ILoggerFactory LoggerFactory { get; set; } = new LoggerFactory();
        

        /// <summary>
        ///     Creates a logger for the given type.
        /// </summary>
        /// <typeparam name="T">The type for which to a logger is created.</typeparam>
        /// <returns>The requested logger.</returns>
        public static ILogger CreateLogger<T>()
        {
            return LoggerFactory.CreateLogger<T>();
        }

        /// <summary>
        ///     Creates a logger for the given instance type.
        /// </summary>
        /// <typeparam name="T">The type for which to a logger is created.</typeparam>
        /// <param name="instance">The instance used to derive a type for which a logger is created.</param>
        /// <returns>The requested logger.</returns>
        public static ILogger CreateLogger<T>(T instance)
        {
            return LoggerFactory.CreateLogger(instance.GetType());
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant