Skip to content

Commit eb1f90c

Browse files
committed
feat(Agent): 为BaseComponentAgent添加日志功能支持
将BaseComponentAgent改为partial类并新增Logger部分类,实现完整的日志记录功能 包括Debug、Verbose、Information、Warning、Error、Fatal等各级别日志方法 支持带异常和格式化参数的日志记录,方便组件代理进行调试和问题追踪 #47
1 parent e67d873 commit eb1f90c

File tree

2 files changed

+357
-32
lines changed

2 files changed

+357
-32
lines changed
Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
// ==========================================================================================
2+
// GameFrameX 组织及其衍生项目的版权、商标、专利及其他相关权利
3+
// GameFrameX organization and its derivative projects' copyrights, trademarks, patents, and related rights
4+
// 均受中华人民共和国及相关国际法律法规保护。
5+
// are protected by the laws of the People's Republic of China and relevant international regulations.
6+
//
7+
// 使用本项目须严格遵守相应法律法规及开源许可证之规定。
8+
// Usage of this project must strictly comply with applicable laws, regulations, and open-source licenses.
9+
//
10+
// 本项目采用 MIT 许可证与 Apache License 2.0 双许可证分发,
11+
// This project is dual-licensed under the MIT License and Apache License 2.0,
12+
// 完整许可证文本请参见源代码根目录下的 LICENSE 文件。
13+
// please refer to the LICENSE file in the root directory of the source code for the full license text.
14+
//
15+
// 禁止利用本项目实施任何危害国家安全、破坏社会秩序、
16+
// It is prohibited to use this project to engage in any activities that endanger national security, disrupt social order,
17+
// 侵犯他人合法权益等法律法规所禁止的行为!
18+
// or infringe upon the legitimate rights and interests of others, as prohibited by laws and regulations!
19+
// 因基于本项目二次开发所产生的一切法律纠纷与责任,
20+
// Any legal disputes and liabilities arising from secondary development based on this project
21+
// 本项目组织与贡献者概不承担。
22+
// shall be borne solely by the developer; the project organization and contributors assume no responsibility.
23+
//
24+
// GitHub 仓库:https://github.com/GameFrameX
25+
// GitHub Repository: https://github.com/GameFrameX
26+
// Gitee 仓库:https://gitee.com/GameFrameX
27+
// Gitee Repository: https://gitee.com/GameFrameX
28+
// 官方文档:https://gameframex.doc.alianblank.com/
29+
// Official Documentation: https://gameframex.doc.alianblank.com/
30+
// ==========================================================================================
31+
32+
using System;
33+
using GameFrameX.Core.Abstractions.Agent;
34+
using GameFrameX.Core.Components;
35+
using GameFrameX.Foundation.Logger;
36+
using Serilog;
37+
using Serilog.Events;
38+
39+
namespace GameFrameX.Core.Hotfix.Agent;
40+
41+
/// <summary>
42+
/// 基础组件代理类,用于管理组件与Actor之间的交互
43+
/// </summary>
44+
/// <typeparam name="TComponent">具体的组件类型</typeparam>
45+
public abstract partial class BaseComponentAgent<TComponent> : IComponentAgent where TComponent : BaseComponent
46+
{
47+
private ILogger _logger;
48+
49+
/// <summary>
50+
///
51+
/// </summary>
52+
/// <returns></returns>
53+
protected virtual ILogger InitLogger()
54+
{
55+
return default;
56+
}
57+
58+
private ILogger GetLogger()
59+
{
60+
if (_logger == default)
61+
{
62+
_logger = InitLogger() ?? Serilog.Log.Logger;
63+
}
64+
65+
return _logger;
66+
}
67+
68+
/// <summary>
69+
/// Records a debug message with optional format parameters
70+
/// </summary>
71+
/// <param name="msg">The debug message to record / 要记录的调试消息</param>
72+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
73+
/// <remarks>
74+
/// 用于记录调试级别的日志信息,通常在开发和测试阶段使用
75+
/// </remarks>
76+
public void Debug(string msg, params object[] args)
77+
{
78+
GetLogger().Debug(msg, args);
79+
}
80+
81+
/// <summary>
82+
/// Records a debug message with exception and optional format parameters
83+
/// </summary>
84+
/// <param name="exception">The exception to log / 要记录的异常</param>
85+
/// <param name="msg">The debug message to record / 要记录的调试消息</param>
86+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
87+
/// <remarks>
88+
/// 用于记录带有异常信息的调试级别日志
89+
/// </remarks>
90+
public void Debug(Exception exception, string msg, params object[] args)
91+
{
92+
GetLogger().Debug(exception, msg, args);
93+
}
94+
95+
/// <summary>
96+
/// Records a simple debug message without parameters
97+
/// </summary>
98+
/// <param name="msg">The debug message to record / 要记录的调试消息</param>
99+
/// <remarks>
100+
/// 用于记录简单的调试消息,不需要格式化参数
101+
/// </remarks>
102+
public void Debug(string msg)
103+
{
104+
GetLogger().Debug(msg);
105+
}
106+
107+
/// <summary>
108+
/// Records a debug message with exception only
109+
/// </summary>
110+
/// <param name="exception">The exception to log / 要记录的异常</param>
111+
/// <param name="msg">The debug message to record / 要记录的调试消息</param>
112+
/// <remarks>
113+
/// 用于记录带有异常的简单调试消息
114+
/// </remarks>
115+
public void Debug(Exception exception, string msg)
116+
{
117+
GetLogger().Debug(exception, msg);
118+
}
119+
120+
/// <summary>
121+
/// Records a debug message with single parameter
122+
/// </summary>
123+
/// <param name="msg">The debug message template / 调试消息模板</param>
124+
/// <param name="arg">Single parameter for the message / 消息的单个参数</param>
125+
/// <remarks>
126+
/// 用于记录带有单个参数的调试消息
127+
/// </remarks>
128+
public void Debug(string msg, object arg)
129+
{
130+
GetLogger().Debug(msg, arg);
131+
}
132+
133+
/// <summary>
134+
/// Records a debug message with two parameters
135+
/// </summary>
136+
/// <param name="msg">The debug message template / 调试消息模板</param>
137+
/// <param name="arg1">First parameter for the message / 消息的第一个参数</param>
138+
/// <param name="arg2">Second parameter for the message / 消息的第二个参数</param>
139+
/// <remarks>
140+
/// 用于记录带有两个参数的调试消息
141+
/// </remarks>
142+
public void Debug(string msg, object arg1, object arg2)
143+
{
144+
GetLogger().Debug(msg, arg1, arg2);
145+
}
146+
147+
/// <summary>
148+
/// Records a debug message with three parameters
149+
/// </summary>
150+
/// <param name="msg">The debug message template / 调试消息模板</param>
151+
/// <param name="arg1">First parameter for the message / 消息的第一个参数</param>
152+
/// <param name="arg2">Second parameter for the message / 消息的第二个参数</param>
153+
/// <param name="arg3">Third parameter for the message / 消息的第三个参数</param>
154+
/// <remarks>
155+
/// 用于记录带有三个参数的调试消息
156+
/// </remarks>
157+
public void Debug(string msg, object arg1, object arg2, object arg3)
158+
{
159+
GetLogger().Debug(msg, arg1, arg2, arg3);
160+
}
161+
162+
/// <summary>
163+
/// Records verbose messages with optional format parameters
164+
/// </summary>
165+
/// <param name="msg">The verbose message to record / 要记录的详细消息</param>
166+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
167+
/// <remarks>
168+
/// 用于记录详细级别的日志信息,比Debug更详细的信息
169+
/// </remarks>
170+
public void Verbose(string msg, params object[] args)
171+
{
172+
GetLogger().Verbose(msg, args);
173+
}
174+
175+
/// <summary>
176+
/// Records verbose messages with exception and optional format parameters
177+
/// </summary>
178+
/// <param name="exception">The exception to log / 要记录的异常</param>
179+
/// <param name="msg">The verbose message to record / 要记录的详细消息</param>
180+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
181+
/// <remarks>
182+
/// 用于记录带有异常信息的详细级别日志
183+
/// </remarks>
184+
public void Verbose(Exception exception, string msg, params object[] args)
185+
{
186+
GetLogger().Verbose(exception, msg, args);
187+
}
188+
189+
/// <summary>
190+
/// Records information messages with optional format parameters
191+
/// </summary>
192+
/// <param name="msg">The information message to record / 要记录的信息消息</param>
193+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
194+
/// <remarks>
195+
/// 用于记录信息级别的日志,通常用于记录应用程序的正常运行信息
196+
/// </remarks>
197+
public void Information(string msg, params object[] args)
198+
{
199+
GetLogger().Information(msg, args);
200+
}
201+
202+
/// <summary>
203+
/// Records information messages with exception and optional format parameters
204+
/// </summary>
205+
/// <param name="exception">The exception to log / 要记录的异常</param>
206+
/// <param name="msg">The information message to record / 要记录的信息消息</param>
207+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
208+
/// <remarks>
209+
/// 用于记录带有异常信息的信息级别日志
210+
/// </remarks>
211+
public void Information(Exception exception, string msg, params object[] args)
212+
{
213+
GetLogger().Information(exception, msg, args);
214+
}
215+
216+
/// <summary>
217+
/// Records warning messages with optional format parameters
218+
/// </summary>
219+
/// <param name="msg">The warning message to record / 要记录的警告消息</param>
220+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
221+
/// <remarks>
222+
/// 用于记录警告级别的日志,通常用于记录可能的问题或需要注意的情况
223+
/// </remarks>
224+
public void Warning(string msg, params object[] args)
225+
{
226+
GetLogger().Warning(msg, args);
227+
}
228+
229+
/// <summary>
230+
/// Records warning messages with exception and optional format parameters
231+
/// </summary>
232+
/// <param name="exception">The exception to log / 要记录的异常</param>
233+
/// <param name="msg">The warning message to record / 要记录的警告消息</param>
234+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
235+
/// <remarks>
236+
/// 用于记录带有异常信息的警告级别日志
237+
/// </remarks>
238+
public void Warning(Exception exception, string msg, params object[] args)
239+
{
240+
GetLogger().Warning(exception, msg, args);
241+
}
242+
243+
/// <summary>
244+
/// Records error messages with optional format parameters
245+
/// </summary>
246+
/// <param name="msg">The error message to record / 要记录的错误消息</param>
247+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
248+
/// <remarks>
249+
/// 用于记录错误级别的日志,通常用于记录应用程序中的错误情况
250+
/// </remarks>
251+
public void Error(string msg, params object[] args)
252+
{
253+
GetLogger().Error(msg, args);
254+
}
255+
256+
/// <summary>
257+
/// Records error messages with exception and optional format parameters
258+
/// </summary>
259+
/// <param name="exception">The exception to log / 要记录的异常</param>
260+
/// <param name="msg">The error message to record / 要记录的错误消息</param>
261+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
262+
/// <remarks>
263+
/// 用于记录带有异常信息的错误级别日志
264+
/// </remarks>
265+
public void Error(Exception exception, string msg, params object[] args)
266+
{
267+
GetLogger().Error(exception, msg, args);
268+
}
269+
270+
/// <summary>
271+
/// Records fatal messages with optional format parameters
272+
/// </summary>
273+
/// <param name="msg">The fatal message to record / 要记录的致命错误消息</param>
274+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
275+
/// <remarks>
276+
/// 用于记录致命错误级别的日志,通常用于记录导致应用程序无法继续运行的严重错误
277+
/// </remarks>
278+
public void Fatal(string msg, params object[] args)
279+
{
280+
GetLogger().Fatal(msg, args);
281+
}
282+
283+
/// <summary>
284+
/// Records fatal messages with exception and optional format parameters
285+
/// </summary>
286+
/// <param name="exception">The exception to log / 要记录的异常</param>
287+
/// <param name="msg">The fatal message to record / 要记录的致命错误消息</param>
288+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
289+
/// <remarks>
290+
/// 用于记录带有异常信息的致命错误级别日志
291+
/// </remarks>
292+
public void Fatal(Exception exception, string msg, params object[] args)
293+
{
294+
GetLogger().Fatal(exception, msg, args);
295+
}
296+
297+
/// <summary>
298+
/// Records log messages with specified log level and optional format parameters
299+
/// </summary>
300+
/// <param name="level">The log level to use / 要使用的日志级别</param>
301+
/// <param name="msg">The log message to record / 要记录的日志消息</param>
302+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
303+
/// <remarks>
304+
/// 用于记录指定级别的日志消息,允许动态指定日志级别
305+
/// </remarks>
306+
public void Log(LogEventLevel level, string msg, params object[] args)
307+
{
308+
GetLogger().Write(level, msg, args);
309+
}
310+
311+
/// <summary>
312+
/// Records log messages with specified log level, exception and optional format parameters
313+
/// </summary>
314+
/// <param name="level">The log level to use / 要使用的日志级别</param>
315+
/// <param name="exception">The exception to log / 要记录的异常</param>
316+
/// <param name="msg">The log message to record / 要记录的日志消息</param>
317+
/// <param name="args">Optional format parameters for the message / 消息的可选格式参数</param>
318+
/// <remarks>
319+
/// 用于记录带有异常信息的指定级别日志消息
320+
/// </remarks>
321+
public void Log(LogEventLevel level, Exception exception, string msg, params object[] args)
322+
{
323+
GetLogger().Write(level, exception, msg, args);
324+
}
325+
}

0 commit comments

Comments
 (0)