安装扩展包:NServiceBus.Log4Net

Endpoint.cs增加注入

log4net.Config.BasicConfigurator.Configure();
NServiceBus.Logging.LogManager.Use<NServiceBus.Log4Net.Log4NetFactory>();

 

 

 

原理:

NBUS源码:可以看到,注入外部的日志需要用到 Use<T>或者UseFactory(ILoggerFactory loggerFactory)

namespace NServiceBus.Logging
{
    using System;

    public static class LogManager
    {
        static LogManager()
        {
            Use<DefaultFactory>();
        }

        static Lazy<ILoggerFactory> loggerFactory; 

        /// <summary>
        /// Used to inject an instance of <see cref="ILoggerFactory"/> into <see cref="LogManager"/>.
        /// </summary>
        public static T Use<T>() where T : LoggingFactoryDefinition, new()
        {
            var loggingDefinition = new T();

            loggerFactory = new Lazy<ILoggerFactory>(loggingDefinition.GetLoggingFactory); 
            
            return loggingDefinition;
        }

        /// <summary>
        /// An instance of <see cref="ILoggerFactory"/> that will be used to construct <see cref="ILog"/>s for static fields.
        /// </summary>
        /// <remarks>
        /// Replace this instance at application statup to redirect log event to your custom logging library.
        /// </remarks>
        public static void UseFactory(ILoggerFactory loggerFactory)
        {
            if (loggerFactory == null)
            {
                throw new ArgumentNullException("loggerFactory");
            }

            LogManager.loggerFactory = new Lazy<ILoggerFactory>(() => loggerFactory);
        }

…………

 

 

再看扩展包:NServiceBus.Log4Net源码

 

基本思路就是Log4Net的Ilog接口和NBUS的ILog接口转换一波

 

源码:

https://github.com/Particular/NServiceBus.Log4Net/tree/master/src/NServiceBus.Log4Net

相关文章:

  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-23
  • 2022-12-23
  • 2021-07-20
猜你喜欢
  • 2021-12-13
  • 2021-06-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
相关资源
相似解决方案