薛之猫大王
17 hours ago da45ccae4c4b03fa50308b442a04ccfd3de160e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// GameApp.cs
// Create:
//      2019-10-29
// Description:
//      日记服务实现类,管理日记的输出,使用log4net 可以自己配置日记输出形式。
// Author:
//      薛林强 <545626463@qq.com>
//
// Copyright (c) 2026 虚幻骑士科技
 
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
using log4net;
using log4net.Appender;
using log4net.Config;
using UnityEngine;
using UnityEngine.Networking;
using log4net.Util;
using System.Collections.Generic;
 
namespace Skyunion
{
    internal class LogService : Module, ILogService
    {
        private IAssetService mAssetService;
        private bool m_bConfigLoad = false;
        private bool m_bInUnityLog = false;
        private ILog m_unityLogger;
        private Dictionary<string, ILog> mDictoryLogger = new Dictionary<string, ILog>();
        private ILog GetLogger(string loggerName)
        {
            ILog logger;
            if (!mDictoryLogger.TryGetValue(loggerName, out logger))
            {
                logger = LogManager.GetLogger(loggerName);
                mDictoryLogger.Add(loggerName, logger);
            }
            return logger;
        }
 
        private void InitLog4Net()
        {
            // 可以自己修改日记的写入方式
            // http://logging.apache.org/log4net/release/config-examples.html
            string configPath = Path.Combine(Application.streamingAssetsPath, "log4net.xml");
            CoreUtils.LoadFileAsync(configPath, (byteData) =>
            {
                if (byteData != null)
                {
                    CoreUtils.Decrypt(byteData);
                    UnityEngine.Debug.Log($"load file {configPath} successed! {Time.realtimeSinceStartup}");
                    XmlDocument document = new XmlDocument();
                    document.Load(new MemoryStream(byteData));
                    XmlConfigurator.Configure(document.DocumentElement);
                    m_unityLogger = LogManager.GetLogger("Unity");
                    m_bConfigLoad = true;
                }
                else
                {
                    UnityEngine.Debug.LogWarning($"load file {configPath} failure!");
                }
 
                if (m_bConfigLoad)
                {
                    UnityEngine.Application.logMessageReceivedThreaded += ThreadLog;
                }
                OnInitialized();
            });
        }
        private void LogLog_LogReceived(object source, LogReceivedEventArgs e)
        {
            UnityEngine.Debug.Log(e.LogLog.Message);
            if (e.LogLog.Exception != null)
                UnityEngine.Debug.Log(e.LogLog.Exception.ToString());
        }
        private void ThreadLog(string condition, string stackTrace, LogType type)
        {
            if (m_bInUnityLog)
                return;
 
            m_bInUnityLog = true;
            if (UnityEngine.Debug.unityLogger.IsLogTypeAllowed(type))
            {
                switch (type)
                {
                    case LogType.Error:
                        m_unityLogger.Error(condition + stackTrace);
                        break;
                    case LogType.Assert:
                        m_unityLogger.Warn(condition + stackTrace);
                        break;
                    case LogType.Warning:
                        m_unityLogger.Warn(condition + stackTrace);
                        break;
                    case LogType.Log:
                        m_unityLogger.Info(condition + stackTrace);
                        break;
                    case LogType.Exception:
                        m_unityLogger.Error(condition + stackTrace);
                        break;
                }
            }
            m_bInUnityLog = false;
        }
 
        #region 实现 IModule
        public override void BeforeInit()
        {
            mAssetService = mPluginManager.FindModule<IAssetService>();
#if !UNITY_EDITOR && UNITY_ANDROID
            //UnityEngine.Debug.unityLogger.filterLogType = LogType.Error;
#endif
        }
 
        public override void Init()
        {
            // 如果你的Log4Net无法正常工作,请取消一下注释
            //LogLog.InternalDebugging = true;
            //LogLog.QuietMode = false;
            //LogLog.LogReceived += LogLog_LogReceived;
            RollingFileAppender.SetLogFileDir(Application.temporaryCachePath);
            UnityEngine.Debug.Log("Log4netPath:" + Application.temporaryCachePath);
            InitLog4Net();
        }
 
 
        public override void Shut()
        {
            if (m_bConfigLoad)
            {
                Application.logMessageReceivedThreaded -= ThreadLog;
            }
            LogManager.Shutdown();
        }
#endregion 实现 IModule
 
#region 实现 ILogService
        public void SetLogDir(string logDir)
        {
            UnityRollingFileAppender.SetLogFileDir(Application.temporaryCachePath);
            if (m_bConfigLoad)
                InitLog4Net();
        }
        public void Debug(string message)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Log))
                return;
            m_unityLogger.Debug(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.Log(message);
            m_bInUnityLog = false;
        }
        public void Info(string message)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Log))
                return;
            m_unityLogger.Info(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.Log(message);
            m_bInUnityLog = false;
        }
        public void Warn(string message)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Warning))
                return;
            m_unityLogger.Warn(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.LogWarning(message);
            m_bInUnityLog = false;
        }
        public void Error(string message)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Error))
                return;
            m_unityLogger.Error(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.LogError(message);
            m_bInUnityLog = false;
        }
        public void Fatal(string message)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Error))
                return;
            m_unityLogger.Error(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.LogError(message);
            m_bInUnityLog = false;
        }
        public void Debug(string message, string loggerName)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Log))
                return;
            GetLogger(loggerName).Debug(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.Log(message);
            m_bInUnityLog = false;
        }
        public void Info(string message, string loggerName)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Log))
                return;
            GetLogger(loggerName).Info(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.Log(message);
            m_bInUnityLog = false;
        }
        public void Warn(string message, string loggerName)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Warning))
                return;
            GetLogger(loggerName).Warn(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.LogWarning(message);
            m_bInUnityLog = false;
        }
        public void Error(string message, string loggerName)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Error))
                return;
            GetLogger(loggerName).Error(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.LogError(message);
            m_bInUnityLog = false;
        }
        public void Fatal(string message, string loggerName)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Error))
                return;
            GetLogger(loggerName).Error(message);
            m_bInUnityLog = true;
            UnityEngine.Debug.LogError(message);
            m_bInUnityLog = false;
        }
 
        public void Debug(string message, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Log))
                return;
            m_unityLogger.Debug(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.Log(message);
            m_bInUnityLog = false;
        }
        public void Info(string message, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Log))
                return;
            m_unityLogger.Info(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.Log(message);
            m_bInUnityLog = false;
        }
        public void Warn(string message, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Warning))
                return;
            m_unityLogger.Warn(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.LogWarning(message);
            m_bInUnityLog = false;
        }
        public void Error(string message, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Error))
                return;
            m_unityLogger.Error(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.LogError(message);
            m_bInUnityLog = false;
        }
        public void Fatal(string message, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Error))
                return;
            m_unityLogger.Error(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.LogError(message);
            m_bInUnityLog = false;
        }
        public void Debug(string message, string loggerName, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Log))
                return;
            GetLogger(loggerName).Debug(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.Log(message);
            m_bInUnityLog = false;
        }
        public void Info(string message, string loggerName, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Log))
                return;
            GetLogger(loggerName).Info(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.Log(message);
            m_bInUnityLog = false;
        }
        public void Warn(string message, string loggerName, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Warning))
                return;
            GetLogger(loggerName).Warn(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.LogWarning(message);
            m_bInUnityLog = false;
        }
        public void Error(string message, string loggerName, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Error))
                return;
            GetLogger(loggerName).Error(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.LogError(message);
            m_bInUnityLog = false;
        }
        public void Fatal(string message, string loggerName, Color color)
        {
            if (!UnityEngine.Debug.unityLogger.IsLogTypeAllowed(LogType.Error))
                return;
            GetLogger(loggerName).Error(message);
            m_bInUnityLog = true;
            message = string.Format("<b><color=#{0:X2}{1:X2}{2:X2}>{3}</color></b>\n", (byte)(color.r * 255), (byte)(color.g * 255), (byte)(color.b * 255), message);
            UnityEngine.Debug.LogError(message);
            m_bInUnityLog = false;
        }
#endregion 实现 ILogService
    }
}