Skip to content

Commit d9edbbc

Browse files
committed
fix: 调整日志方法
1 parent af4f722 commit d9edbbc

File tree

7 files changed

+65
-34
lines changed

7 files changed

+65
-34
lines changed

framework/src/Bing.Logging/Bing/Logging/Core/LogEventContext.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public LogEventContext SetExtraProperty(string name, object value)
108108

109109
#endregion
110110

111-
#region CallerInfo
111+
#region CallerInfo(调用者信息)
112112

113113
/// <summary>
114114
/// 调用者信息
@@ -124,9 +124,7 @@ public LogEventContext SetExtraProperty(string name, object value)
124124
public LogEventContext SetCallerInfo(string memberName = "", string sourceFilePath = "", int sourceLineNumber = 0)
125125
{
126126
if (!string.IsNullOrWhiteSpace(memberName) || !string.IsNullOrWhiteSpace(sourceFilePath) || sourceLineNumber > 0)
127-
{
128127
_callerInfo = new LogCallerInfo(memberName, sourceFilePath, sourceLineNumber);
129-
}
130128
return this;
131129
}
132130

framework/src/Bing.Logging/Bing/Logging/Core/LogEventDescriptor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,20 @@ public LogEventDescriptor()
1818
/// 日志事件上下文
1919
/// </summary>
2020
public LogEventContext Context { get; }
21+
22+
/// <summary>
23+
/// 日志跟踪标识
24+
/// </summary>
25+
public string TraceId { get; set; }
26+
27+
/// <summary>
28+
/// 日志跟踪名称
29+
/// </summary>
30+
public string TraceName { get; set; }
31+
32+
/// <summary>
33+
/// 业务跟踪标识
34+
/// </summary>
35+
public string BusinessTraceId { get; set; }
2136
}
2237
}

framework/src/Bing.Logging/Bing/Logging/ILog.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.CompilerServices;
3+
using Bing.Logging.Core;
34
using Microsoft.Extensions.Logging;
45

56
namespace Bing.Logging
@@ -37,17 +38,10 @@ public interface ILog
3738
ILog Property(string propertyName, string propertyValue);
3839

3940
/// <summary>
40-
/// 设置扩展属性
41+
/// 设置日志事件描述符
4142
/// </summary>
42-
/// <param name="propertyName">属性名</param>
43-
/// <param name="propertyValue">属性值</param>
44-
ILog ExtraProperty(string propertyName, object propertyValue);
45-
46-
/// <summary>
47-
/// 设置标签
48-
/// </summary>
49-
/// <param name="tags">标签</param>
50-
ILog Tags(params string[] tags);
43+
/// <param name="action">操作</param>
44+
ILog Set(Action<LogEventDescriptor> action);
5145

5246
/// <summary>
5347
/// 设置日志状态对象

framework/src/Bing.Logging/Bing/Logging/ILogExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,28 @@ public static ILog Line(this ILog log)
8383
log.Message(Environment.NewLine);
8484
return log;
8585
}
86+
87+
/// <summary>
88+
/// 设置扩展属性
89+
/// </summary>
90+
/// <param name="log">日志</param>
91+
/// <param name="propertyName">属性名</param>
92+
/// <param name="propertyValue">属性值</param>
93+
public static ILog ExtraProperty(this ILog log, string propertyName, object propertyValue) =>
94+
log.Set(x => x.Context.SetExtraProperty(propertyName, propertyValue));
95+
96+
/// <summary>
97+
/// 设置标签列表
98+
/// </summary>
99+
/// <param name="log">日志</param>
100+
/// <param name="tags">标签列表</param>
101+
public static ILog Tags(this ILog log, params string[] tags) => log.Set(x => x.Context.SetTags(tags));
102+
103+
/// <summary>
104+
/// 设置标签
105+
/// </summary>
106+
/// <param name="log">日志</param>
107+
/// <param name="tag">标签</param>
108+
public static ILog Tag(this ILog log, string tag) => log.Set(x => x.Context.SetTags(tag));
86109
}
87110
}

framework/src/Bing.Logging/Bing/Logging/Log.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,17 @@ public virtual ILog Property(string propertyName, string propertyValue)
143143

144144
#endregion
145145

146-
#region ExtraProperty(设置扩展属性)
146+
#region Set(设置日志事件描述符)
147147

148148
/// <summary>
149-
/// 设置扩展属性
149+
/// 设置日志事件描述符
150150
/// </summary>
151-
/// <param name="propertyName">属性名</param>
152-
/// <param name="propertyValue">属性值</param>
153-
public ILog ExtraProperty(string propertyName, object propertyValue)
151+
/// <param name="action">操作</param>
152+
public ILog Set(Action<LogEventDescriptor> action)
154153
{
155-
CurrentDescriptor.Context.SetExtraProperty(propertyName, propertyValue);
156-
return this;
157-
}
158-
159-
#endregion
160-
161-
#region Tags(设置标签)
162-
163-
/// <inheritdoc />
164-
public virtual ILog Tags(params string[] tags)
165-
{
166-
CurrentDescriptor.Context.SetTags(tags);
154+
if (action == null)
155+
throw new ArgumentNullException(nameof(action));
156+
action(CurrentDescriptor);
167157
return this;
168158
}
169159

framework/src/Bing.Logging/Bing/Logging/Log`.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.CompilerServices;
3+
using Bing.Logging.Core;
34
using Microsoft.Extensions.Logging;
45

56
namespace Bing.Logging
@@ -41,10 +42,7 @@ public Log(ILogFactory factory)
4142
public virtual ILog Property(string propertyName, string propertyValue) => _log.Property(propertyName, propertyValue);
4243

4344
/// <inheritdoc />
44-
public virtual ILog ExtraProperty(string propertyName, object propertyValue) => _log.ExtraProperty(propertyName, propertyValue);
45-
46-
/// <inheritdoc />
47-
public virtual ILog Tags(params string[] tags) => _log.Tags(tags);
45+
public virtual ILog Set(Action<LogEventDescriptor> action) => _log.Set(action);
4846

4947
/// <inheritdoc />
5048
public virtual ILog State(object state) => _log.State(state);

framework/src/Bing.Logging/Bing/Logging/NullLog.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Bing.Logging.Core;
23
using Microsoft.Extensions.Logging;
34

45
namespace Bing.Logging
@@ -40,6 +41,12 @@ public class NullLog<TCategoryName> : ILog<TCategoryName>
4041
/// <param name="propertyValue">属性值</param>
4142
public ILog ExtraProperty(string propertyName, object propertyValue) => this;
4243

44+
/// <summary>
45+
/// 设置日志事件描述符
46+
/// </summary>
47+
/// <param name="action">操作</param>
48+
public ILog Set(Action<LogEventDescriptor> action) => this;
49+
4350
/// <summary>
4451
/// 设置标签
4552
/// </summary>
@@ -140,6 +147,12 @@ public class NullLog : ILog
140147
/// <param name="propertyValue">属性值</param>
141148
public ILog ExtraProperty(string propertyName, object propertyValue) => this;
142149

150+
/// <summary>
151+
/// 设置日志事件描述符
152+
/// </summary>
153+
/// <param name="action">操作</param>
154+
public ILog Set(Action<LogEventDescriptor> action) => this;
155+
143156
/// <summary>
144157
/// 设置标签
145158
/// </summary>

0 commit comments

Comments
 (0)