© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
51 replies
Cracker

❔ ✅ How to implement pool tracking in concurrent list ?

I am trying to add tracking to my logging service. (Web API project)
Once track count is matched, I disbound the ConcurrentBag and send list to logger service.
I need review in this approach where I don't want to lose log data since this method is called in every request.

private readonly ConcurrentBag<object> _objects = new ConcurrentBag<object>();

public void Log<T>(T data) where T : class
{
    try
    {
        _objects.Add(data);
        if (_objects.Count >= _logOption.TrackCount)
        {
            Task.Factory.StartNew(async () =>
            {
                var dataList = new List<object>();
                for (int i = 0; i < _logOption.TrackCount; i++)
                {
                    if (_objects.TryTake(out object item))

                    {
                        dataList.Add(item);
                    }
                }
                await SendToKafkaApi(dataList);
            });
        }
    }
    catch (Exception ex)
    {
        LogOnFile(ex);
    }
}
private readonly ConcurrentBag<object> _objects = new ConcurrentBag<object>();

public void Log<T>(T data) where T : class
{
    try
    {
        _objects.Add(data);
        if (_objects.Count >= _logOption.TrackCount)
        {
            Task.Factory.StartNew(async () =>
            {
                var dataList = new List<object>();
                for (int i = 0; i < _logOption.TrackCount; i++)
                {
                    if (_objects.TryTake(out object item))

                    {
                        dataList.Add(item);
                    }
                }
                await SendToKafkaApi(dataList);
            });
        }
    }
    catch (Exception ex)
    {
        LogOnFile(ex);
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

How to pool SQLite connections
C#CC# / help
13mo ago
Concurrent collection type similar to Dictionary<TKey, List<TValue>>
C#CC# / help
7mo ago
How to implement Authentication in Blazor Web Assembly?
C#CC# / help
8mo ago