1+ // GameFrameX 组织下的以及组织衍生的项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
2+ //
3+ // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE 文件。
4+ //
5+ // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
6+
7+ using System . Text ;
8+ using GameFrameX . Foundation . Json ;
9+ using GameFrameX . Foundation . Logger ;
10+ using GameFrameX . NetWork . Abstractions ;
11+ using GameFrameX . NetWork . Messages ;
12+ using GameFrameX . Utility . Extensions ;
13+
14+ namespace GameFrameX . NetWork ;
15+
16+ /// <summary>
17+ /// 消息对象日志帮助类
18+ /// </summary>
19+ public static class MessageObjectLoggerHelper
20+ {
21+ /// <summary>
22+ /// 格式化网络消息为可读的字符串格式
23+ /// </summary>
24+ /// <param name="messageId">消息ID</param>
25+ /// <param name="operationType">操作类型</param>
26+ /// <param name="uniqueId">唯一标识ID</param>
27+ /// <param name="messageObject">网络消息对象</param>
28+ /// <returns>格式化后的消息字符串</returns>
29+ public static string FormatMessage ( int messageId , MessageOperationType operationType , int uniqueId , INetworkMessage messageObject )
30+ {
31+ try
32+ {
33+ if ( LogOptions . Default . IsConsole )
34+ {
35+ // 使用StringBuilder构建格式化的控制台输出
36+ var stringBuilder = new StringBuilder ( ) ;
37+ stringBuilder . Clear ( ) ;
38+ stringBuilder . AppendLine ( ) ;
39+ // 向下的箭头
40+ stringBuilder . AppendLine ( $ "{ '\u2193 ' . RepeatChar ( 140 ) } ") ;
41+ // 消息的头部信息
42+ // 消息类型
43+ stringBuilder . Append ( $ "---MessageType:[{ messageObject . GetType ( ) . Name . CenterAlignedText ( 30 ) } ]") ;
44+ // 消息ID
45+ stringBuilder . Append ( $ "--MsgId:[{ messageId . ToString ( ) . CenterAlignedText ( 11 ) } ]({ MessageIdUtility . GetMainId ( messageId ) . ToString ( ) . CenterAlignedText ( 6 ) } ,{ MessageIdUtility . GetSubId ( messageId ) . ToString ( ) . CenterAlignedText ( 6 ) } )") ;
46+ // 操作类型
47+ stringBuilder . Append ( $ "--OpType:[{ operationType . ToString ( ) . CenterAlignedText ( 20 ) } ]") ;
48+ // 唯一ID
49+ stringBuilder . Append ( $ "--UniqueId:[{ uniqueId . ToString ( ) . CenterAlignedText ( 13 ) } ]---") ;
50+ // 消息的内容 分割
51+ stringBuilder . AppendLine ( ) ;
52+ // 消息内容
53+ stringBuilder . AppendLine ( $ "{ messageObject . ToJsonString ( ) } ") ;
54+ // 向上的箭头
55+ stringBuilder . AppendLine ( $ "{ '\u2191 ' . RepeatChar ( 140 ) } ") ;
56+ stringBuilder . AppendLine ( ) ;
57+ // 从缓存中获取并释放StringBuilder的内容
58+ return StringBuilderCache . GetStringAndRelease ( stringBuilder ) ;
59+ }
60+
61+ // 非控制台输出模式下,将消息序列化为JSON格式
62+ var messageObjectLogObject = new MessageObjectLogObject ( messageObject . GetType ( ) . Name , messageId , operationType , uniqueId , messageObject ) ;
63+ var json = JsonHelper . Serialize ( messageObjectLogObject ) ;
64+ return json ;
65+ }
66+ catch ( Exception e )
67+ {
68+ LogHelper . Error ( e ) ;
69+ }
70+
71+ return string . Empty ;
72+ }
73+ }
0 commit comments