System.ArgumentOutOfRangeException参数名称:System.Text.StringBuilder.ToString()处的chunkLength [英] System.ArgumentOutOfRangeException Parameter name: chunkLength at System.Text.StringBuilder.ToString()

查看:442
本文介绍了System.ArgumentOutOfRangeException参数名称:System.Text.StringBuilder.ToString()处的chunkLength的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在日志文件中遇到了以下异常情况.

I have got below exception in a log file.

System.ArgumentOutOfRangeException:索引超出范围.必须为非负数并且小于集合的大小.参数名称:System.Text.StringBuilder.ToString()

我相信这是因为字符串生成器不是线程安全的.但是我迷失了如何使我的字符串生成器在下面的递归函数中安全运行.

I believe this is because of string Builder which is not a thread safe. But I am stumbled on how to make my string builder to thread safe in below recursive function.

public static class StringExtensions
{
    /// <summary>
    /// The log key builder
    /// </summary>
    private static StringBuilder logKeyBuilder;

    public static string ConcatLogKeyWithExceptionMessage<T>(this T entity, string configuredLogKeys, bool logOnlySingleKey, string exceptionMessage, bool firstInvocation = true) where T : class
    {
        logKeyBuilder = logKeyBuilder ?? new StringBuilder();

        if (entity != null)
        {
            var objType = entity.GetType();
            var properties = objType.GetProperties();
            foreach (var property in properties)
            {
                var propValue = property.GetValue(entity, null);
                var elems = propValue as IList;
                if (elems != null)
                {
                    foreach (var item in elems)
                    {
                        {
                            ConcatLogKeyWithExceptionMessage(item, configuredLogKeys, logOnlySingleKey, exceptionMessage, false);
                        }
                    }
                }
                else
                {
                    // This will not cut-off System.Collections because of the first check
                    if (property.PropertyType.Assembly == objType.Assembly)
                    {
                        ConcatLogKeyWithExceptionMessage(propValue, configuredLogKeys, logOnlySingleKey, exceptionMessage, false);
                    }
                    else
                    {
                        configuredKeysArray = configuredKeysArray ?? (!string.IsNullOrEmpty(configuredLogKeys) ? configuredLogKeys.Split(',') : new string[0]);

                        foreach (var configLogKey in configuredKeysArray)
                        {
                            if (string.Compare(configLogKey.Trim(), property.Name.Trim(), StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                configuredKeysArray = configuredKeysArray.Where(x => x != configLogKey).ToArray();
                                logKeyBuilder.Append(property.Name);
                                logKeyBuilder.Append(" ");
                                logKeyBuilder.Append("-");
                                logKeyBuilder.Append(" ");
                                logKeyBuilder.Append(property.GetValue(entity));
                                logKeyBuilder.Append(" ");
                                if (logOnlySingleKey)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        logKeyBuilder = firstInvocation ? logKeyBuilder.Append(exceptionMessage) : logKeyBuilder;
        return logKeyBuilder.ToString();
    }
}

推荐答案

使用锁:

private static object lockObject = new object();
public static string ConcatLogKeyWithExceptionMessage<T>(this T entity, string configuredLogKeys, bool logOnlySingleKey, string exceptionMessage, bool firstInvocation = true) where T : class
{
    lock(lockObject )
    {
        // rest of your code here
    }
}

或将您的静态字段移动为方法内的局部变量.

Or move your static field to be a local variable inside the method.

这篇关于System.ArgumentOutOfRangeException参数名称:System.Text.StringBuilder.ToString()处的chunkLength的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆