Skip to content

Commit

Permalink
修复结构体对象每次获取都被复制一份导致写入日志失败
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Sep 18, 2024
1 parent f860e25 commit 9b51266
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/dotnetCampus.Logger/LoggerInterpolatedStringHandler.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,37 @@ public LoggerInterpolatedStringHandler(int literalLength, int formattedCount)
_handler = new DefaultInterpolatedStringHandler(literalLength, formattedCount);
}

private readonly DefaultInterpolatedStringHandler _handler;
private DefaultInterpolatedStringHandler _handler;

public void AppendLiteral(string s) => _handler.AppendLiteral(s);
public void AppendLiteral(string s)
{
ref var handle = ref _handler;
handle.AppendLiteral(s);
}

public void AppendFormatted<T>(T value) => _handler.AppendFormatted(value);
public void AppendFormatted<T>(T value)
{
ref var handle = ref _handler;
handle.AppendFormatted(value);
}

public void AppendFormatted<T>(T value, string format) => _handler.AppendFormatted(value, format);
public void AppendFormatted<T>(T value, string format)
{
ref var handle = ref _handler;
handle.AppendFormatted(value, format);
}

public string ToStringAndClear() => _handler.ToStringAndClear();
public string ToStringAndClear()
{
ref var handle = ref _handler;
return handle.ToStringAndClear();
}

public override string ToString() => _handler.ToString();
public override string ToString()
{
ref var handle = ref _handler;
return handle.ToString();
}

/// <summary>
/// 废弃掉此字符串
Expand All @@ -30,7 +50,8 @@ public void Discard()
{
// 这里的 ToStringAndClear 其实只是取其 Clear 的功能
// 暂时先使用 DefaultInterpolatedStringHandler 提供的能力,后续再考虑是否需要优化
_handler.ToStringAndClear();
ref var handle = ref _handler;
handle.ToStringAndClear();
}
}

Expand Down

0 comments on commit 9b51266

Please sign in to comment.