Dreamine.Logging 1.0.2
Dreamine.Logging 프로젝트의 API와 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
InMemoryLogStore.cs
이 파일의 문서화 페이지로 가기
1using System;
2using System.Collections.Generic;
3using System.Linq;
6
8{
26 {
35 private readonly object _syncRoot = new();
44 private readonly Queue<DreamineLogEntry> _entries;
53 private readonly int _capacity;
54
63 public event EventHandler<DreamineLogEntry>? LogAdded;
64
74 : this(1000)
75 {
76 }
77
94 public InMemoryLogStore(int capacity)
95 {
96 _capacity = capacity <= 0 ? 1000 : capacity;
97 _entries = new Queue<DreamineLogEntry>(_capacity);
98 }
99
116 public IReadOnlyList<DreamineLogEntry> GetEntries()
117 {
118 lock (_syncRoot)
119 {
120 return _entries.ToArray();
121 }
122 }
123
148 public void Add(DreamineLogEntry entry)
149 {
150 ArgumentNullException.ThrowIfNull(entry);
151
152 lock (_syncRoot)
153 {
154 _entries.Enqueue(entry);
155
156 while (_entries.Count > _capacity)
157 {
158 _entries.Dequeue();
159 }
160 }
161
162 // Fire outside the lock to avoid reentrancy/deadlock if a handler
163 // calls back into the store.
164 LogAdded?.Invoke(this, entry);
165 }
166
175 public void Clear()
176 {
177 lock (_syncRoot)
178 {
179 _entries.Clear();
180 }
181 }
182
207 public void Write(DreamineLogEntry entry)
208 {
209 Add(entry);
210 }
211 }
212}
readonly Queue< DreamineLogEntry > _entries
IReadOnlyList< DreamineLogEntry > GetEntries()
EventHandler< DreamineLogEntry >? LogAdded