首页 > 解决方案 > 如何从 .net core 2.2 中的 log4net 配置文件中获取 appender

问题描述

我在 asp.net core 2.0 中实现 log4net AsyncBufferingForwardingAppender,但我猜它不支持。
我已经在核心 2.0 中实现了 log4net RollingFileAppender,它使用 log4.net 配置成功运行。

我正在尝试以下代码:
// sLoggerName = "BasicLogger"

internal static ILog Initialize(string sLoggerName)
{
    ILog objlogger = null;
    try
    {
        #region Log4net_Configuration
        XmlDocument log4netConfig = new XmlDocument();
        log4netConfig.Load(File.OpenRead("log4net.config"));
        var repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(),
                   typeof(log4net.Repository.Hierarchy.Hierarchy));
        log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);
        #endregion


        objlogger = log4net.LogManager
                        .GetLogger(log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(),
                        typeof(log4net.Repository.Hierarchy.Hierarchy)).Name, sLoggerName);

        // Iterating through each appender
        object[] objAppenders = objlogger.Logger.Repository.GetAppenders();

        foreach (object item in objAppenders)
        {
            var objFile = item as AppenderSkeleton; 
            if (objFile != null)
            {
                objFile.ActivateOptions();
            }
        }

        // Checking logger
        if (objlogger.Logger.Name != sLoggerName)
        {
            objlogger = null;
        }
    }
    catch (System.Exception)
    {
        // Handle exception
    }
    return objlogger;
}  

我的 log4net.config 文件配置如下:

<?xml version="1.0"?>
  <log4net>
    <appender name="Basic" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Logs/Basic.log"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <param name="AppendToFile" value="true"/>
      <param name="Threshold" value="INFO" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd" />
      <maxSizeRollBackups value="300" />
      <maximumFileSize value="1MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date{yyyy/MM/dd HH:mm:ss,fff}&#9;[%-5p]&#9;[%3t]&#9;%m%n" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="INFO" />
      </filter>
    </appender>

    <appender name="BasicAsyncBufferingForwarder" type="Easy.Logger.AsyncBufferingForwardingAppender">
      <lossy value="false" />
      <bufferSize value="500" />
      <idleTime value="3000" />
      <fix value="Message, ThreadName, Exception" />
      <appender-ref ref="Basic" />
    </appender>

    <logger name="BasicLogger">
      <level value="ALL" />
      <appender-ref ref="BasicAsyncBufferingForwarder" />
    </logger>

  </log4net>  

objAppenders 的计数始终为 0。
我是否缺少编码?谢谢

标签: asp.net-core-2.0log4net

解决方案


请重新检查您的类型字段...

它必须是 type="Easy.Logger.AsyncBufferingForwardingAppender, XXX"

XXX = "程序集名称"

请参阅以下链接 https://github.com/NimaAra/Easy.Logger


推荐阅读