Procházet zdrojové kódy

整理Log类,机器人跟Server日志打到对应的文件中

tanghai před 4 roky
rodič
revize
3871eca3ec

+ 4 - 2
Robot/App/Program.cs

@@ -29,8 +29,10 @@ namespace ET
                         .WithParsed(o => { options = o; });
 
                 Game.Options = options;
-				
-                LogManager.Configuration.Variables["appIdFormat"] = $"{Game.Scene.Id:0000}";
+
+                Game.ILog = new NLogger(Game.Options.AppType.ToString());
+                
+                LogManager.Configuration.Variables["appIdFormat"] = $"{Game.Options.Process:000000}";
 				
                 Log.Info($"server start........................ {Game.Scene.Id}");
 

+ 106 - 31
Server/App/NLog.config

@@ -1,57 +1,132 @@
 <?xml version="1.0" encoding="utf-8" ?>
 
 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-	<targets>
-		<target name="debug" xsi:type="File"
-				openFileCacheTimeout="3600"
-				fileName="${basedir}/../Logs/Log-${var:appIdFormat}-${date:universalTime=true:format=yyyyMMdd}-Debug.log"
+	<targets async="true">
+		<target name="ServerDebug" xsi:type="File"
+				openFileCacheTimeout="10"
+				keepFileOpen="true"
+				archiveNumbering="Date"
+				archiveEvery="Hour"
+				archiveDateFormat="yyyyMMddHH"
+				archiveFileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.{#}.Debug.log"
+				fileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Debug.log"
 				deleteOldFileOnStartup="false"
 				layout="${longdate} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" />
 	</targets>
 
-	<targets>
-		<target name="info" xsi:type="File"
-				openFileCacheTimeout="3600"
-				fileName="${basedir}/../Logs/Log-${var:appIdFormat}-${date:universalTime=true:format=yyyyMMdd}-Info.log"
+	<targets async="true">
+		<target name="ServerInfo" xsi:type="File"
+				bufferSize="10240"
+				openFileCacheTimeout="30"
+				keepFileOpen="true"
+				archiveNumbering="Date"
+				archiveEvery="Hour"
+				archiveDateFormat="yyyyMMddHH"
+				archiveFileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.{#}.Info.log"
+				fileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Info.log"
 				deleteOldFileOnStartup="false"
-				layout="${longdate} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" />
+				layout="${longdate} ${message}" />
 	</targets>
 
-	<targets>
-		<target name="error" xsi:type="File"
-				openFileCacheTimeout="3600"
-				fileName="${basedir}/../Logs/Log-${var:appIdFormat}-${date:universalTime=true:format=yyyyMMdd}-Error.log"
+	<targets async="true">
+		<target name="ServerWarn" xsi:type="File"
+				bufferSize="10240"
+				openFileCacheTimeout="30"
+				keepFileOpen="true"
+				archiveNumbering="Date"
+				archiveEvery="Hour"
+				archiveDateFormat="yyyyMMddHH"
+				archiveFileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.{#}.Warn.log"
+				fileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Warn.log"
 				deleteOldFileOnStartup="false"
-				layout="${longdate} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" />
+				layout="${longdate} ${message}" />
+	</targets>
+
+	<targets async="true">
+		<target name="ServerError" xsi:type="File"
+				openFileCacheTimeout="10"
+				keepFileOpen="true"
+				archiveNumbering="Date"
+				archiveEvery="Hour"
+				archiveDateFormat="yyyyMMddHH"
+				archiveFileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.{#}.Error.log"
+				fileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Error.log"
+				deleteOldFileOnStartup="false"
+				layout="${longdate} ${message}" />
+	</targets>
+
+	<targets async="true">
+		<target name="ErrorConsole" xsi:type="Console" layout="${longdate} ${message}" />
 	</targets>
 
-	<targets>
-		<target name="errorConsole" xsi:type="Console"
+	<targets async="true">
+		<target name="RobotDebug" xsi:type="File"
+				openFileCacheTimeout="10"
+				keepFileOpen="true"
+				archiveNumbering="Date"
+				archiveEvery="Hour"
+				archiveDateFormat="yyyyMMddHH"
+				archiveFileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.{#}.Debug.log"
+				fileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Debug.log"
+				deleteOldFileOnStartup="false"
 				layout="${longdate} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" />
 	</targets>
 
-	<targets>
-		<target name="warn" xsi:type="File"
-				openFileCacheTimeout="3600"
-				fileName="${basedir}/../Logs/Log-${var:appIdFormat}-${date:universalTime=true:format=yyyyMMdd}-Warning.log"
+	<targets async="true">
+		<target name="RobotInfo" xsi:type="File"
+				bufferSize="10240"
+				openFileCacheTimeout="30"
+				keepFileOpen="true"
+				archiveNumbering="Date"
+				archiveEvery="Hour"
+				archiveDateFormat="yyyyMMddHH"
+				archiveFileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.{#}.Info.log"
+				fileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Info.log"
 				deleteOldFileOnStartup="false"
 				layout="${longdate} ${message}" />
 	</targets>
 
-	<targets>
-		<target name="fatal" xsi:type="File"
-				openFileCacheTimeout="3600"
-				fileName="${basedir}/../Logs/Log-${var:appIdFormat}-${date:universalTime=true:format=yyyyMMdd}-Fatal.log"
+	<targets async="true">
+		<target name="RobotWarn" xsi:type="File"
+				bufferSize="10240"
+				openFileCacheTimeout="30"
+				keepFileOpen="true"
+				archiveNumbering="Date"
+				archiveEvery="Hour"
+				archiveDateFormat="yyyyMMddHH"
+				archiveFileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.{#}.Warn.log"
+				fileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Warn.log"
 				deleteOldFileOnStartup="false"
-				layout="${longdate} ${message} ${newline} ${stacktrace:format=Raw:topFrames=10:skipFrames=2}" />
+				layout="${longdate} ${message}" />
+	</targets>
+
+	<targets async="true">
+		<target name="RobotError" xsi:type="File"
+				openFileCacheTimeout="10"
+				keepFileOpen="true"
+				archiveNumbering="Date"
+				archiveEvery="Hour"
+				archiveDateFormat="yyyyMMddHH"
+				archiveFileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.{#}.Error.log"
+				fileName="${basedir}/../Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Error.log"
+				deleteOldFileOnStartup="false"
+				layout="${longdate} ${message}" />
+	</targets>
+
+	<targets async="true">
+		<target name="RobotConsole" xsi:type="Console" layout="${message}" />
 	</targets>
 
 	<rules>
-		<logger name="*" minlevel="Trace" maxlevel="Fatal" writeTo="debug" />
-		<logger name="*" minlevel="Info" maxlevel="Info" writeTo="info" />
-		<logger name="*" minlevel="Warn" maxlevel="Warn" writeTo="warn" />
-		<logger name="*" minlevel="Error" maxlevel="Error" writeTo="error" />
-		<logger name="*" minlevel="Trace" maxlevel="Error" writeTo="errorConsole" />
-		<logger name="*" minlevel="Fatal" maxlevel="Fatal" writeTo="fatal" />
+		<logger ruleName="ServerDebug" name="Server" minlevel="Trace" maxlevel="Error" writeTo="ServerDebug" />
+		<logger ruleName="ServerInfo" name="Server" minlevel="Info" maxlevel="Info" writeTo="ServerInfo" />
+		<logger ruleName="ServerWarn" name="Server" minlevel="Warn" maxlevel="Warn" writeTo="ServerWarn" />
+		<logger ruleName="ServerError" name="Server" minlevel="Error" maxlevel="Error" writeTo="ServerError" />
+		<logger ruleName="ErrorConsole" name="Server" minlevel="Warn" maxlevel="Error" writeTo="ErrorConsole" />
+		<logger ruleName="RobotDebug" name="Robot" minlevel="Trace" maxlevel="Error" writeTo="RobotDebug" />
+		<logger ruleName="RobotInfo" name="Robot" minlevel="Trace" maxlevel="Error" writeTo="RobotInfo" />
+		<logger ruleName="RobotWarn" name="Robot" minlevel="Warn" maxlevel="Warn" writeTo="RobotWarn" />
+		<logger ruleName="RobotError" name="Robot" minlevel="Error" maxlevel="Error" writeTo="RobotError" />
+		<logger ruleName="RobotConsole" name="Robot" minlevel="Warn" maxlevel="Error" writeTo="RobotConsole" />
 	</rules>
 </nlog>

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 416 - 199
Server/App/NLog.xsd


+ 3 - 2
Server/App/Program.cs

@@ -29,8 +29,9 @@ namespace ET
 						.WithParsed(o => { options = o; });
 
 				Game.Options = options;
-				
-				LogManager.Configuration.Variables["appIdFormat"] = $"{Game.Scene.Id:0000}";
+
+				Game.ILog = new NLogger(Game.Options.AppType.ToString());
+				LogManager.Configuration.Variables["appIdFormat"] = $"{Game.Options.Process:000000}";
 				
 				Log.Info($"server start........................ {Game.Scene.Id}");
 

+ 1 - 1
Server/App/Server.App.csproj

@@ -25,7 +25,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="NLog" Version="4.5.4" />
+    <PackageReference Include="NLog" Version="4.7.10" />
   </ItemGroup>
 
   <ItemGroup>

+ 2 - 0
Unity/Assets/Model/Core/Entity/Game.cs

@@ -32,6 +32,8 @@ namespace ET
 
         public static Options Options;
 
+        public static ILog ILog;
+
         public static List<Action> FrameFinishCallback = new List<Action>();
 
         public static void Update()

+ 21 - 35
Unity/Assets/Model/Core/Log/Log.cs

@@ -3,7 +3,7 @@ using System.Diagnostics;
 using System.IO;
 using System.Net;
 
-#if NOT_CLIENT
+#if NOT_UNITY
 using NLog;
 #endif
 
@@ -11,19 +11,15 @@ namespace ET
 {
     public static class Log
     {
-        public const int TraceLevel = 1;
-        public const int DebugLevel = 2;
-        public const int InfoLevel = 3;
-        public const int WarningLevel = 4;
-        
-        public static ILog ILog { get; }
+        private const int TraceLevel = 1;
+        private const int DebugLevel = 2;
+        private const int InfoLevel = 3;
+        private const int WarningLevel = 4;
 
         static Log()
         {
-#if NOT_UNITY
-            ILog = new NLogger("Server");
-#else
-            ILog = new UnityLogger();
+#if !NOT_UNITY
+            Game.ILog = new UnityLogger();
 #endif
         }
 
@@ -37,18 +33,14 @@ namespace ET
             return Game.Options.LogLevel <= level;
         }
         
-        public static Action<string, object[]> DebugCallback;
-        public static Action<string> ErrorCallback;
-
         public static void Trace(string msg)
         {
             if (!CheckLogLevel(DebugLevel))
             {
                 return;
             }
-            DebugCallback?.Invoke(msg, null);
             StackTrace st = new StackTrace(1, true);
-            ILog.Trace($"{msg}\n{st}");
+            Game.ILog.Trace($"{msg}\n{st}");
         }
 
         public static void Debug(string msg)
@@ -57,8 +49,7 @@ namespace ET
             {
                 return;
             }
-            DebugCallback?.Invoke(msg, null);
-            ILog.Debug(msg);
+            Game.ILog.Debug(msg);
         }
 
         public static void Info(string msg)
@@ -67,7 +58,7 @@ namespace ET
             {
                 return;
             }
-            ILog.Info(msg);
+            Game.ILog.Info(msg);
         }
 
         public static void TraceInfo(string msg)
@@ -77,7 +68,7 @@ namespace ET
                 return;
             }
             StackTrace st = new StackTrace(1, true);
-            ILog.Trace($"{msg}\n{st}");
+            Game.ILog.Trace($"{msg}\n{st}");
         }
 
         public static void Warning(string msg)
@@ -87,21 +78,19 @@ namespace ET
                 return;
             }
 
-            ILog.Warning(msg);
+            Game.ILog.Warning(msg);
         }
 
         public static void Error(string msg)
         {
             StackTrace st = new StackTrace(1, true);
-            ErrorCallback?.Invoke($"{msg}\n{st}");
-            ILog.Error($"{msg}\n{st}");
+            Game.ILog.Error($"{msg}\n{st}");
         }
 
         public static void Error(Exception e)
         {
             string str = e.ToString();
-            ErrorCallback?.Invoke(str);
-            ILog.Error(str);
+            Game.ILog.Error(str);
         }
 
         public static void Trace(string message, params object[] args)
@@ -110,9 +99,8 @@ namespace ET
             {
                 return;
             }
-            DebugCallback?.Invoke(message, args);
             StackTrace st = new StackTrace(1, true);
-            ILog.Trace($"{string.Format(message, args)}\n{st}");
+            Game.ILog.Trace($"{string.Format(message, args)}\n{st}");
         }
 
         public static void Warning(string message, params object[] args)
@@ -121,7 +109,7 @@ namespace ET
             {
                 return;
             }
-            ILog.Warning(string.Format(message, args));
+            Game.ILog.Warning(string.Format(message, args));
         }
 
         public static void Info(string message, params object[] args)
@@ -130,7 +118,7 @@ namespace ET
             {
                 return;
             }
-            ILog.Info(string.Format(message, args));
+            Game.ILog.Info(string.Format(message, args));
         }
 
         public static void Debug(string message, params object[] args)
@@ -139,8 +127,7 @@ namespace ET
             {
                 return;
             }
-            DebugCallback?.Invoke(message, args);
-            ILog.Debug(string.Format(message, args));
+            Game.ILog.Debug(string.Format(message, args));
 
         }
 
@@ -148,8 +135,7 @@ namespace ET
         {
             StackTrace st = new StackTrace(1, true);
             string s = string.Format(message, args) + '\n' + st;
-            ErrorCallback?.Invoke(s);
-            ILog.Error(s);
+            Game.ILog.Error(s);
         }
         
         public static void Console(string message)
@@ -158,7 +144,7 @@ namespace ET
             {
                 System.Console.WriteLine(message);
             }
-            ILog.Debug(message);
+            Game.ILog.Debug(message);
         }
         
         public static void Console(string message, params object[] args)
@@ -168,7 +154,7 @@ namespace ET
             {
                 System.Console.WriteLine(s);
             }
-            ILog.Debug(s);
+            Game.ILog.Debug(s);
         }
     }
 }

+ 2 - 2
Unity/Assets/Model/Module/Message/OpcodeHelper.cs

@@ -37,7 +37,7 @@ namespace ET
                 return;
             }
             
-            Log.ILog.Debug("zone: {0} {1}", zone, message);
+            Game.ILog.Debug("zone: {0} {1}", zone, message);
         }
         
         public static void LogMsg(ushort opcode, long actorId, object message)
@@ -47,7 +47,7 @@ namespace ET
                 return;
             }
             
-            Log.ILog.Debug("actorId: {0} {1}", actorId, message);
+            Game.ILog.Debug("actorId: {0} {1}", actorId, message);
         }
     }
 }

+ 1 - 1
Unity/Assets/ModelView/Demo/Entry.cs

@@ -22,7 +22,7 @@ namespace ET
 					{
 						continue;
 					}
-					Game.EventSystem.Add(assembly);	
+					Game.EventSystem.Add(assembly);
 				}
 				
 				ProtobufHelper.Init();

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů