如何从.net Core 2.2中的log4net配置文件获取添加程序

时间:2019-05-07 06:36:52

标签: asp.net-core-2.0 log4net

我正在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。
我是否缺少编码? 谢谢

1 个答案:

答案 0 :(得分:2)

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

它必须为type =“ Easy.Logger.AsyncBufferingForwardingAppender,XXX”

XXX =“程序集名称”

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