47 lines
1.8 KiB
C#
47 lines
1.8 KiB
C#
using EonaCat.LogStack.Core;
|
|
using System.IO;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace EonaCat.LogStack.Boosters;
|
|
|
|
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
|
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
|
|
|
/// <summary>
|
|
/// Automatically captures caller file, line number, and member name via
|
|
/// compiler-generated attributes - zero overhead at the call site.
|
|
///
|
|
/// Adds properties:
|
|
/// caller.member - method / property name
|
|
/// caller.file - source file name (not full path, for privacy)
|
|
/// caller.line - line number
|
|
///
|
|
/// Usage: add to your LogBuilder with <c>.BoostWithCallerInfo()</c>.
|
|
///
|
|
/// Note: because this booster uses compile-time attributes it captures the
|
|
/// logger helper method rather than the end-user call site when the log call
|
|
/// is made through wrapper methods. For precise call-site capture, call
|
|
/// <see cref="Capture"/> directly from the call site.
|
|
/// </summary>
|
|
public sealed class CallerInfoBooster : BoosterBase
|
|
{
|
|
public CallerInfoBooster() : base("CallerInfo") { }
|
|
|
|
public override bool Boost(ref LogEventBuilder builder) => true; // no-op in generic path
|
|
|
|
/// <summary>
|
|
/// Enriches a builder with the actual call-site information.
|
|
/// Call this from your logging helper method so the compiler fills in the arguments.
|
|
/// </summary>
|
|
public static void Capture(
|
|
ref LogEventBuilder builder,
|
|
[CallerMemberName] string member = "",
|
|
[CallerFilePath] string file = "",
|
|
[CallerLineNumber] int line = 0)
|
|
{
|
|
builder.WithProperty("caller.member", member);
|
|
builder.WithProperty("caller.file", Path.GetFileName(file));
|
|
builder.WithProperty("caller.line", line);
|
|
}
|
|
}
|