Skip to content

Commit

Permalink
Remove StringBuilderCache
Browse files Browse the repository at this point in the history
This commit removes the StringBuilderCache,
calculating a capacity for a StringBuilder from
the log component lengths.
  • Loading branch information
russcam committed May 24, 2021
1 parent e62cd1d commit ea47331
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 79 deletions.
65 changes: 0 additions & 65 deletions src/Elastic.Apm/Helpers/StringBuilderCache.cs

This file was deleted.

41 changes: 27 additions & 14 deletions src/Elastic.Apm/Logging/TraceLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using Elastic.Apm.Helpers;

namespace Elastic.Apm.Logging
Expand Down Expand Up @@ -34,25 +35,37 @@ public void Log<TState>(LogLevel level, TState state, Exception e, Func<TState,
if (!IsEnabled(level)) return;

var message = formatter(state, e);
var logLevel = LevelToString(level);

StringBuilder builder;
string exceptionType = null;
var capacity = 51 + message.Length + logLevel.Length;

if (e is null)
builder = new StringBuilder(capacity);
else
{
exceptionType = e.GetType().FullName;
builder = new StringBuilder(capacity + exceptionType.Length + e.Message.Length + e.StackTrace.Length);
}

builder.Append('[')
.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff zzz"))
.Append("][")
.Append(logLevel)
.Append("] - ")
.Append(message);

// default message size is 51 + length of loglevel (max 8), message and exception.
var builder = StringBuilderCache.Acquire(80);
builder.Append('[');
builder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff zzz"));
builder.Append("][");
builder.Append(LevelToString(level));
builder.Append("] - ");
builder.Append(message);
if (e != null)
{
builder.Append("+-> Exception: ");
builder.Append(e.GetType().FullName);
builder.Append(": ");
builder.AppendLine(e.Message);
builder.AppendLine(e.StackTrace);
builder.Append("+-> Exception: ")
.Append(exceptionType)
.Append(": ")
.AppendLine(e.Message)
.AppendLine(e.StackTrace);
}

var logMessage = StringBuilderCache.GetStringAndRelease(builder);
var logMessage = builder.ToString();
for (var i = 0; i < TraceSource.Listeners.Count; i++)
{
var listener = TraceSource.Listeners[i];
Expand Down

0 comments on commit ea47331

Please sign in to comment.