Эх сурвалжийг харах

根据不同的AppType和AppId,log打印到不同的文件中

tanghai 9 жил өмнө
parent
commit
99a0005ea8

+ 6 - 4
Server/App/NLog.config

@@ -1,17 +1,19 @@
 <?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">
+	<variable name="appType" value="" />
+	<variable name="appId" value="" />
 	<targets>
 		<target name="debug" xsi:type="File"
-			fileName="${basedir}/Log.Debug.txt"
+			fileName="${basedir}/Logs/Log-${var:appType}-${var:appId}-Debug.txt"
 			deleteOldFileOnStartup="true"
-			layout="${longdate} ${threadid:padCharacter=0:padding=4:fixedLength=true} ${message}" />
+			layout="${longdate} ${var:appType} ${var:appId} ${message}" />
 	</targets>
 	<targets>
 		<target name="error" xsi:type="File"
-			fileName="${basedir}/Log.Error.txt"
+			fileName="${basedir}/Logs/Log-${var:appType}-${var:appId}-Error.txt"
 			deleteOldFileOnStartup="false"
-			layout="${longdate} ${threadid:padCharacter=0:padding=4:fixedLength=true} ${message}" />
+			layout="${longdate} ${var:appType} ${var:appId} ${message}" />
 	</targets>
 	<rules>
 		<logger name="*" minlevel="Debug" writeTo="debug" />

+ 6 - 3
Server/App/Program.cs

@@ -1,6 +1,7 @@
 using System;
 using Base;
 using Model;
+using NLog;
 using Object = Base.Object;
 
 namespace App
@@ -11,8 +12,6 @@ namespace App
 		{
 			try
 			{
-				Log.Info("server start........................");
-
 				BsonClassMapRegister.Register();
 
 				Object.ObjectManager.Register("Base", typeof(Game).Assembly);
@@ -20,7 +19,11 @@ namespace App
 				Object.ObjectManager.Register("Controller", DllHelper.GetController());
 
 				StartConfig startConfig = Game.Scene.AddComponent<StartConfigComponent, string[]>(args).MyConfig;
-				
+				LogManager.Configuration.Variables["appType"] = startConfig.Options.AppType;
+				LogManager.Configuration.Variables["appId"] = startConfig.Options.Id.ToString("D4");
+
+				Log.Info("server start........................");
+
 				Game.Scene.AddComponent<EventComponent>();
 				Game.Scene.AddComponent<TimerComponent>();
 

+ 4 - 0
Server/App/Server.App.csproj

@@ -37,6 +37,10 @@
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\Lib\MongoDB.Bson.dll</HintPath>
     </Reference>
+    <Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\Lib\NLog.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
   </ItemGroup>

+ 1 - 1
Server/Base/Log.cs

@@ -5,7 +5,7 @@ namespace Base
 {
 	public static class Log
 	{
-		private static readonly ILog globalLog = new NLogAdapter(new StackInfoDecorater());
+		private static readonly ILog globalLog = new NLogAdapter();
 
 		public static Dictionary<long, Action<LogType, string>> Callback { get; } = new Dictionary<long, Action<LogType, string>>();
 

+ 19 - 12
Server/Model/Component/AppManagerComponent.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Linq;
@@ -37,7 +38,7 @@ namespace Model
 
 
 #if __MonoCS__
-				const string exe = @"mono";
+				const string exe = @"/usr/local/bin/mono";
 				string arguments = $"App.exe --id={startConfig.Options.Id} --appType={startConfig.Options.AppType}";
 				const string workDir = @"../Server/Bin/Debug";
 #else
@@ -45,18 +46,24 @@ namespace Model
 				string arguments = $"--id={startConfig.Options.Id} --appType={startConfig.Options.AppType}";
 				const string workDir = @"..\Server\Bin\Debug";
 #endif
-				Log.Debug($"{startConfig.Options.Id} {MongoHelper.ToJson(startConfig)}"); 
-				ProcessStartInfo info = new ProcessStartInfo
+				try
 				{
-					FileName = exe,
-					Arguments = arguments,
-					CreateNoWindow = true,
-					UseShellExecute = true,
-					WorkingDirectory = workDir
-				};
+					ProcessStartInfo info = new ProcessStartInfo
+					{
+						FileName = exe,
+						Arguments = arguments,
+						CreateNoWindow = true,
+						UseShellExecute = true,
+						WorkingDirectory = workDir
+					};
 
-				Process process = Process.Start(info);
-				this.processes.Add(process.Id, process);
+					Process process = Process.Start(info);
+					this.processes.Add(process.Id, process);
+				}
+				catch (Exception e)
+				{
+					Log.Error(e.ToString());
+				}
 			}
 		}
     }

+ 9 - 2
Unity/Assets/Plugins/Base/Network/TNet/TSocket.cs

@@ -30,8 +30,15 @@ namespace Base
 
 		public TSocket(TPoller poller, string host, int port): this(poller)
 		{
-			this.Bind(host, port);
-			this.Listen(100);
+			try
+			{
+				this.Bind(host, port);
+				this.Listen(100);
+			}
+			catch (Exception e)
+			{
+				throw new Exception($"socket bind error: {host} {port}", e);
+			}
 		}
 		
 		public Socket Socket