Skip to content

Commit

Permalink
Merge pull request #14 from dotnet-campus/t/walterlv/api-break
Browse files Browse the repository at this point in the history
[API Break] 优化 Log 的 debug/trace 方法的使用体验
  • Loading branch information
lindexi authored Jun 24, 2024
2 parents 2f19dd6 + 36018e4 commit 87cb9d1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 43 deletions.
26 changes: 14 additions & 12 deletions samples/LoggerSample.LoggerDependentLibrary/DllReferenceTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ public class DllReferenceTarget
{
public static void CollectLogs()
{
Log.Trace.Trace("[DllReference] Log.Trace.Trace");
Log.Trace.Debug("[DllReference] Log.Trace.Debug");
Log.Trace.Info("[DllReference] Log.Trace.Info");
Log.Trace.Warn("[DllReference] Log.Trace.Warn");
Log.Trace.Error("[DllReference] Log.Trace.Error");
Log.Trace.Fatal("[DllReference] Log.Trace.Fatal");
Log.TraceLogger.Trace("[DllReference] Log.Trace.Trace");
Log.TraceLogger.Debug("[DllReference] Log.Trace.Debug");
Log.TraceLogger.Info("[DllReference] Log.Trace.Info");
Log.TraceLogger.Warn("[DllReference] Log.Trace.Warn");
Log.TraceLogger.Error("[DllReference] Log.Trace.Error");
Log.TraceLogger.Fatal("[DllReference] Log.Trace.Fatal");

Log.Debug.Trace("[DllReference] Log.Debug.Trace");
Log.Debug.Debug("[DllReference] Log.Debug.Debug");
Log.Debug.Info("[DllReference] Log.Debug.Info");
Log.Debug.Warn("[DllReference] Log.Debug.Warn");
Log.Debug.Error("[DllReference] Log.Debug.Error");
Log.Debug.Fatal("[DllReference] Log.Debug.Fatal");
Log.DebugLogger.Trace("[DllReference] Log.Debug.Trace");
Log.DebugLogger.Debug("[DllReference] Log.Debug.Debug");
Log.DebugLogger.Info("[DllReference] Log.Debug.Info");
Log.DebugLogger.Warn("[DllReference] Log.Debug.Warn");
Log.DebugLogger.Error("[DllReference] Log.Debug.Error");
Log.DebugLogger.Fatal("[DllReference] Log.Debug.Fatal");

Log.Trace("[DllReference] Log.Trace");
Log.Debug("[DllReference] Log.Debug");
Log.Info("[DllReference] Log.Info");
Log.Warn("[DllReference] Log.Warn");
Log.Error("[DllReference] Log.Error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ public static class SourceReferenceTarget
{
public static void CollectLogs()
{
Log.Trace.Trace("[SourceReference] Log.Trace.Trace");
Log.Trace.Debug("[SourceReference] Log.Trace.Debug");
Log.Trace.Info("[SourceReference] Log.Trace.Info");
Log.Trace.Warn("[SourceReference] Log.Trace.Warn");
Log.Trace.Error("[SourceReference] Log.Trace.Error");
Log.Trace.Fatal("[SourceReference] Log.Trace.Fatal");
Log.TraceLogger.Trace("[SourceReference] Log.Trace.Trace");
Log.TraceLogger.Debug("[SourceReference] Log.Trace.Debug");
Log.TraceLogger.Info("[SourceReference] Log.Trace.Info");
Log.TraceLogger.Warn("[SourceReference] Log.Trace.Warn");
Log.TraceLogger.Error("[SourceReference] Log.Trace.Error");
Log.TraceLogger.Fatal("[SourceReference] Log.Trace.Fatal");

Log.Debug.Trace("[SourceReference] Log.Debug.Trace");
Log.Debug.Debug("[SourceReference] Log.Debug.Debug");
Log.Debug.Info("[SourceReference] Log.Debug.Info");
Log.Debug.Warn("[SourceReference] Log.Debug.Warn");
Log.Debug.Error("[SourceReference] Log.Debug.Error");
Log.Debug.Fatal("[SourceReference] Log.Debug.Fatal");
Log.DebugLogger.Trace("[SourceReference] Log.Debug.Trace");
Log.DebugLogger.Debug("[SourceReference] Log.Debug.Debug");
Log.DebugLogger.Info("[SourceReference] Log.Debug.Info");
Log.DebugLogger.Warn("[SourceReference] Log.Debug.Warn");
Log.DebugLogger.Error("[SourceReference] Log.Debug.Error");
Log.DebugLogger.Fatal("[SourceReference] Log.Debug.Fatal");

Log.Trace("[SourceReference] Log.Trace");
Log.Debug("[SourceReference] Log.Debug");
Log.Info("[SourceReference] Log.Info");
Log.Warn("[SourceReference] Log.Warn");
Log.Error("[SourceReference] Log.Error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ public class SourceReferenceTarget
{
public static void CollectLogs()
{
Log.Trace.Trace("[SourceReference] Log.Trace.Trace");
Log.Trace.Debug("[SourceReference] Log.Trace.Debug");
Log.Trace.Info("[SourceReference] Log.Trace.Info");
Log.Trace.Warn("[SourceReference] Log.Trace.Warn");
Log.Trace.Error("[SourceReference] Log.Trace.Error");
Log.Trace.Fatal("[SourceReference] Log.Trace.Fatal");
Log.TraceLogger.Trace("[SourceReference] Log.Trace.Trace");
Log.TraceLogger.Debug("[SourceReference] Log.Trace.Debug");
Log.TraceLogger.Info("[SourceReference] Log.Trace.Info");
Log.TraceLogger.Warn("[SourceReference] Log.Trace.Warn");
Log.TraceLogger.Error("[SourceReference] Log.Trace.Error");
Log.TraceLogger.Fatal("[SourceReference] Log.Trace.Fatal");

Log.Debug.Trace("[SourceReference] Log.Debug.Trace");
Log.Debug.Debug("[SourceReference] Log.Debug.Debug");
Log.Debug.Info("[SourceReference] Log.Debug.Info");
Log.Debug.Warn("[SourceReference] Log.Debug.Warn");
Log.Debug.Error("[SourceReference] Log.Debug.Error");
Log.Debug.Fatal("[SourceReference] Log.Debug.Fatal");
Log.DebugLogger.Trace("[SourceReference] Log.Debug.Trace");
Log.DebugLogger.Debug("[SourceReference] Log.Debug.Debug");
Log.DebugLogger.Info("[SourceReference] Log.Debug.Info");
Log.DebugLogger.Warn("[SourceReference] Log.Debug.Warn");
Log.DebugLogger.Error("[SourceReference] Log.Debug.Error");
Log.DebugLogger.Fatal("[SourceReference] Log.Debug.Fatal");

Log.Trace("[SourceReference] Log.Trace");
Log.Debug("[SourceReference] Log.Debug");
Log.Info("[SourceReference] Log.Info");
Log.Warn("[SourceReference] Log.Warn");
Log.Error("[SourceReference] Log.Error");
Expand Down
42 changes: 35 additions & 7 deletions src/dotnetCampus.Logger/Log.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace dotnetCampus.Logging;
public static partial class Log
{
private static ILogger _current;
private static DebugLogger _debug;
private static TraceLogger _trace;

static Log()
{
Expand All @@ -25,7 +27,7 @@ static Log()
public static ILogger Current
{
get => _current;
[MemberNotNull(nameof(_current), nameof(Debug), nameof(Trace))]
[MemberNotNull(nameof(_current), nameof(_debug), nameof(_trace))]
private set
{
if (value == null)
Expand All @@ -35,14 +37,14 @@ private set

if (Equals(_current, value))
{
Debug ??= new DebugLogger(value);
Trace ??= new TraceLogger(value);
_debug ??= new DebugLogger(value);
_trace ??= new TraceLogger(value);
return;
}

_current = value;
Debug = new DebugLogger(value);
Trace = new TraceLogger(value);
_debug = new DebugLogger(value);
_trace = new TraceLogger(value);
}
}

Expand All @@ -52,7 +54,7 @@ private set
/// <remarks>
/// 请注意,所有通过此记录器记录的日志仅在以 debug 配置编译时才会被记录,并且在非 debug 编译后对此记录器的调用都会被编译器优化掉。
/// </remarks>
public static DebugLogger Debug { get; private set; }
public static DebugLogger DebugLogger => _debug;

/// <summary>
/// 获取仅在 TRACE 条件编译符被定义时才会记录的日志记录器。
Expand All @@ -61,7 +63,33 @@ private set
/// <remarks>
/// 请注意,所有通过此记录器记录的日志仅在定义了 TRACE 条件编译符时才会被记录,并且在未定义 TRACE 条件编译符后对此记录器的调用都会被编译器优化掉。
/// </remarks>
public static TraceLogger Trace { get; private set; }
public static TraceLogger TraceLogger => _trace;

/// <summary>
/// 记录跟踪日志。
/// </summary>
/// <param name="message">要记录的消息。</param>
/// <remarks>
/// 如果开启了跟踪级别的日志记录,此方法产生的日志在生产环境中也依然会被记录。<br/>
/// 如果希望仅在 TRACE 条件编译符被定义时记录日志,请使用 <see cref="TraceLogger"/>。
/// </remarks>
public static void Trace(string message)
{
Current.Log(LogLevel.Trace, default, message, null, (s, ex) => message);
}

/// <summary>
/// 记录调试日志。
/// </summary>
/// <param name="message">要记录的消息。</param>
/// <remarks>
/// 如果开启了调试级别的日志记录,此方法产生的日志在生产环境中也依然会被记录。<br/>
/// 如果希望仅在 debug 配置下记录日志,请使用 <see cref="DebugLogger"/>。
/// </remarks>
public static void Debug(string message)
{
Current.Log(LogLevel.Debug, default, message, null, (s, ex) => message);
}

/// <summary>
/// 记录信息日志。
Expand Down

0 comments on commit 87cb9d1

Please sign in to comment.