示例#1
0
    /// <summary>
    /// Add breadcrumb by providing Breadcrumb object
    /// </summary>
    /// <param name="breadcrumb">Breadcrumb</param>
    public void AddBreadcrumb(Breadcrumb breadcrumb)
    {
        string json           = breadcrumb.ToJson();
        long   breadcrumbSize = Encoding.UTF8.GetBytes(json).Length;

        // might have to async this entire piece - since order is not important and this might block a render thread
        // it might not even be worth it to lock on this - although linkedlist is not threadsafe it might be better to use a ConcurrentQueue: https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1?view=net-5.0
        // note that this cannot be answered without doing meaningful tests.
        lock (syncLock)
        {
            breadcrumbsByteSize += breadcrumbSize;

            // pop breadcrumbs until there's enough space
            while (breadcrumbsByteSize > maxFileSize)
            {
                string toBePurged = breadcrumbs.First.Value;
                breadcrumbs.RemoveFirst();
                breadcrumbsByteSize -= Encoding.UTF8.GetBytes(toBePurged).Length;
            }

            breadcrumbs.AddLast(json);

            // if a write has been sheduled, the previous record will be written when it triggers, so don't schedule another
            if (autoWrite && !writeScheduled)
            {
                writeScheduled = true;
                Task.Delay(waitBeforeWrite).ContinueWith(t =>
                {
                    this.Write();
                });
            }
        }
    }