System.ArgumentOutOfRangeException log4net

本文关键字:log4net ArgumentOutOfRangeException System | 更新日期: 2023-09-27 17:58:17

我随机得到这个异常。从下面的堆栈跟踪中,我可以理解它源自log4net功能。

LogExceptionValue  - 
System.ArgumentOutOfRangeException: capacity was less than the current size.
Parameter name: value
at System.Collections.ArrayList.set_Capacity(Int32 value)
at System.Collections.ArrayList.Add(Object value)
at log4net.Util.LogReceivedEventHandler.Invoke(Object source, LogReceivedEventArgs e)
at log4net.Util.LogLog.OnLogReceived(Type source, String prefix, String   message, Exception exception)
at log4net.Config.XmlConfigurator.InternalConfigure(ILoggerRepository  repository)
at log4net.Config.XmlConfigurator.Configure(ILoggerRepository repository)
at log4net.Config.XmlConfigurator.Configure()
at Loggers.LoggerBase.LogMessage(LogInformation logInformation)
at LookupByReasonCode(String reasonCode)

Log4Net 代码

 /// <summary>
    ///  Logs message based on logger.
    /// </summary>
    /// <param name="logInformation">Log Information </param>
    protected void LogMessage(LogInformation logInformation)
    {
        this.log = LogManager.GetLogger(logInformation.Logger);
        log4net.Config.XmlConfigurator.Configure();
        if (!string.IsNullOrEmpty(logInformation.Request))
        {
            ThreadContext.Properties["request"] = logInformation.Request;
        }
        if (!string.IsNullOrEmpty(logInformation.Response))
        {
            ThreadContext.Properties["response"] = logInformation.Response;
        }
        if (!string.IsNullOrEmpty(logInformation.ResponseCode))
        {
            ThreadContext.Properties["responsecode"] = logInformation.ResponseCode;
        }
        if (!string.IsNullOrEmpty(logInformation.Keys))
        {
            ThreadContext.Properties["keys"] = logInformation.Keys;
        }
        //// Logs exception
        this.Log(logInformation.Message, logInformation.LogLevel, logInformation.Exception);
    }

任何帮助都将不胜感激。

System.ArgumentOutOfRangeException log4net

如果您间歇性地得到这个,很可能是因为您正在在每个日志调用上加载log4net配置,并且两个线程正在尝试同时加载它。

XmlConfigurator.Configure();移到启动程序中,只调用一次。