|
|
@ -5,24 +5,49 @@ |
|
|
// All rights reserved. Licensed under the MIT license.
|
|
|
// All rights reserved. Licensed under the MIT license.
|
|
|
// ==========================================================================
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
|
using System.Collections.Concurrent; |
|
|
|
|
|
using System.Text.Json; |
|
|
using System.Text.Json; |
|
|
|
|
|
using Microsoft.Extensions.ObjectPool; |
|
|
|
|
|
|
|
|
namespace Squidex.Infrastructure.Log |
|
|
namespace Squidex.Infrastructure.Log |
|
|
{ |
|
|
{ |
|
|
public sealed class JsonLogWriterFactory : IObjectWriterFactory |
|
|
public sealed class JsonLogWriterFactory : IObjectWriterFactory |
|
|
{ |
|
|
{ |
|
|
private const int MaxPoolSize = 10; |
|
|
private readonly ObjectPool<JsonLogWriter> pool; |
|
|
private const int MaxCapacity = 5000; |
|
|
|
|
|
private readonly ConcurrentStack<JsonLogWriter> pool = new ConcurrentStack<JsonLogWriter>(); |
|
|
|
|
|
private readonly JsonWriterOptions formatting; |
|
|
|
|
|
private readonly bool formatLine; |
|
|
|
|
|
|
|
|
|
|
|
public JsonLogWriterFactory(bool indended = false, bool formatLine = false) |
|
|
internal sealed class JsonLogWriterPolicy : PooledObjectPolicy<JsonLogWriter> |
|
|
{ |
|
|
{ |
|
|
formatting.Indented = indended; |
|
|
private const int MaxCapacity = 5000; |
|
|
|
|
|
private readonly JsonWriterOptions formatting; |
|
|
|
|
|
private readonly bool formatLine; |
|
|
|
|
|
|
|
|
|
|
|
public JsonLogWriterPolicy(bool indended = false, bool formatLine = false) |
|
|
|
|
|
{ |
|
|
|
|
|
formatting.Indented = indended; |
|
|
|
|
|
|
|
|
|
|
|
this.formatLine = formatLine; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override JsonLogWriter Create() |
|
|
|
|
|
{ |
|
|
|
|
|
return new JsonLogWriter(formatting, formatLine); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override bool Return(JsonLogWriter obj) |
|
|
|
|
|
{ |
|
|
|
|
|
if (obj.BufferSize > MaxCapacity) |
|
|
|
|
|
{ |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
obj.Reset(); |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.formatLine = formatLine; |
|
|
public JsonLogWriterFactory(bool indended = false, bool formatLine = false) |
|
|
|
|
|
{ |
|
|
|
|
|
pool = new DefaultObjectPoolProvider().Create(new JsonLogWriterPolicy(indended, formatLine)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static JsonLogWriterFactory Default() |
|
|
public static JsonLogWriterFactory Default() |
|
|
@ -37,26 +62,12 @@ namespace Squidex.Infrastructure.Log |
|
|
|
|
|
|
|
|
public IObjectWriter Create() |
|
|
public IObjectWriter Create() |
|
|
{ |
|
|
{ |
|
|
if (pool.TryPop(out var writer)) |
|
|
return pool.Get(); |
|
|
{ |
|
|
|
|
|
writer.Reset(); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
writer = new JsonLogWriter(formatting, formatLine); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return writer; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void Release(IObjectWriter writer) |
|
|
public void Release(IObjectWriter writer) |
|
|
{ |
|
|
{ |
|
|
var jsonWriter = (JsonLogWriter)writer; |
|
|
pool.Return((JsonLogWriter)writer); |
|
|
|
|
|
|
|
|
if (pool.Count < MaxPoolSize && jsonWriter.BufferSize < MaxCapacity) |
|
|
|
|
|
{ |
|
|
|
|
|
pool.Push(jsonWriter); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|