added nlog logging (todo: custom logger for packets)

This commit is contained in:
Tahir Akhlaq 2016-06-14 05:09:30 +01:00
parent c5516511b0
commit ed0a0a58f7
36 changed files with 5430 additions and 310 deletions

View file

@ -59,7 +59,7 @@ namespace FFXIVClassic_Lobby_Server
socket.Send(packetBytes);
}
catch(Exception e)
{ Program.Log.Error(String.Format("Weird case, socket was d/ced: {0}", e)); }
{ Program.Log.Error("Weird case, socket was d/ced: {0}", e); }
}
}

View file

@ -9,9 +9,6 @@ namespace FFXIVClassic_Lobby_Server
public static String OPTIONS_BINDIP;
public static String OPTIONS_PORT;
public static bool OPTIONS_TIMESTAMP = false;
public static String OPTIONS_LOGPATH;
public static String OPTIONS_LOGFILE;
public static String OPTIONS_LOGLEVEL;
public static String DATABASE_HOST;
public static String DATABASE_PORT;
@ -36,9 +33,6 @@ namespace FFXIVClassic_Lobby_Server
ConfigConstants.OPTIONS_BINDIP = configIni.GetValue("General", "server_ip", "127.0.0.1");
ConfigConstants.OPTIONS_PORT = configIni.GetValue("General", "server_port", "54994");
ConfigConstants.OPTIONS_TIMESTAMP = configIni.GetValue("General", "showtimestamp", "true").ToLower().Equals("true");
ConfigConstants.OPTIONS_LOGPATH = configIni.GetValue("General", "log_path", "./logs/");
ConfigConstants.OPTIONS_LOGFILE = configIni.GetValue("General", "log_file_name", String.Format("lobby_{0}_{1}.log", OPTIONS_BINDIP, OPTIONS_PORT));
ConfigConstants.OPTIONS_LOGLEVEL = configIni.GetValue("General", "log_level", "127");
ConfigConstants.DATABASE_HOST = configIni.GetValue("Database", "host", "");
ConfigConstants.DATABASE_PORT = configIni.GetValue("Database", "port", "");
@ -47,7 +41,7 @@ namespace FFXIVClassic_Lobby_Server
ConfigConstants.DATABASE_PASSWORD = configIni.GetValue("Database", "password", "");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(String.Format("[OK]"));
Console.WriteLine("[OK]");
Console.ForegroundColor = ConsoleColor.Gray;
return true;

View file

@ -100,7 +100,7 @@ namespace FFXIVClassic_Lobby_Server
conn.Dispose();
}
Program.Log.Sql(String.Format("CID={0} created on 'characters' table.", cid));
Program.Log.Debug("[SQL] CID={0} created on 'characters' table.", cid);
}
return alreadyExists;
@ -242,7 +242,7 @@ namespace FFXIVClassic_Lobby_Server
}
Program.Log.Sql(String.Format("CID={0} state updated to active(2).", cid));
Program.Log.Debug("[SQL] CID={0} state updated to active(2).", cid);
}
public static bool renameCharacter(uint userId, uint characterId, uint serverId, String newName)
@ -286,7 +286,7 @@ namespace FFXIVClassic_Lobby_Server
conn.Dispose();
}
Program.Log.Sql(String.Format("CID={0} name updated to \"{1}\".", characterId, newName));
Program.Log.Debug("[SQL] CID={0} name updated to \"{1}\".", characterId, newName);
return false;
}
@ -320,7 +320,7 @@ namespace FFXIVClassic_Lobby_Server
}
}
Program.Log.Sql(String.Format("CID={0} deleted.", characterId));
Program.Log.Debug("[SQL] CID={0} deleted.", characterId);
}
public static List<World> getServers()

View file

@ -65,6 +65,10 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.3.4\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@ -105,6 +109,12 @@
<Compile Include="utils\CharacterCreatorUtils.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="NLog.xsd">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>

View file

@ -0,0 +1,45 @@
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" >
<!-- optional, add some variabeles
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets async="true">
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<target xsi:type="ColoredConsole" name="console" layout="[${longdate}] [${uppercase:${level}}] ${message}" />
<target xsi:type="File" name="file" fileName="${basedir}/logs/${shortdate}/lobby.log" layout="[${longdate}] [${uppercase:${level}}] ${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name='*' minlevel='Trace' writeTo='file' />
<logger name='*' minlevel='Trace' writeTo='console' />
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>

File diff suppressed because it is too large Load diff

View file

@ -53,7 +53,7 @@ namespace FFXIVClassic_Lobby_Server
case 0x0F:
//Mod Retainers
default:
Program.Log.Debug(String.Format("Unknown command 0x{0:X} received.", subpacket.gameMessage.opcode));
Program.Log.Debug("Unknown command 0x{0:X} received.", subpacket.gameMessage.opcode);
break;
}
}
@ -67,7 +67,7 @@ namespace FFXIVClassic_Lobby_Server
byte[] blowfishKey = GenerateKey(securityHandshake.ticketPhrase, securityHandshake.clientNumber);
client.blowfish = new Blowfish(blowfishKey);
Program.Log.Info(String.Format("SecCNum: 0x{0:X}", securityHandshake.clientNumber));
Program.Log.Info("SecCNum: 0x{0:X}", securityHandshake.clientNumber);
//Respond with acknowledgment
BasePacket outgoingPacket = new BasePacket(HardCoded_Packets.g_secureConnectionAcknowledgment);
@ -81,8 +81,8 @@ namespace FFXIVClassic_Lobby_Server
SessionPacket sessionPacket = new SessionPacket(packet.data);
String clientVersion = sessionPacket.version;
Program.Log.Info(String.Format("Got acknowledgment for secure session."));
Program.Log.Info(String.Format("CLIENT VERSION: {0}", clientVersion));
Program.Log.Info("Got acknowledgment for secure session.");
Program.Log.Info("CLIENT VERSION: {0}", clientVersion);
uint userId = Database.getUserIdFromSession(sessionPacket.session);
client.currentUserId = userId;
@ -96,11 +96,11 @@ namespace FFXIVClassic_Lobby_Server
BasePacket.encryptPacket(client.blowfish, errorBasePacket);
client.queuePacket(errorBasePacket);
Program.Log.Info(String.Format("Invalid session, kicking..."));
Program.Log.Info("Invalid session, kicking...");
return;
}
Program.Log.Info(String.Format("USER ID: {0}", userId));
Program.Log.Info("USER ID: {0}", userId);
List<Account> accountList = new List<Account>();
Account defaultAccount = new Account();
@ -115,7 +115,7 @@ namespace FFXIVClassic_Lobby_Server
private void ProcessGetCharacters(ClientConnection client, SubPacket packet)
{
Program.Log.Info(String.Format("{0} => Get characters", client.currentUserId == 0 ? client.getAddress() : "User " + client.currentUserId));
Program.Log.Info("{0} => Get characters", client.currentUserId == 0 ? client.getAddress() : "User " + client.currentUserId);
sendWorldList(client, packet);
sendImportList(client, packet);
@ -128,7 +128,7 @@ namespace FFXIVClassic_Lobby_Server
{
SelectCharacterPacket selectCharRequest = new SelectCharacterPacket(packet.data);
Program.Log.Info(String.Format("{0} => Select character id {1}", client.currentUserId == 0 ? client.getAddress() : "User " + client.currentUserId, selectCharRequest.characterId));
Program.Log.Info("{0} => Select character id {1}", client.currentUserId == 0 ? client.getAddress() : "User " + client.currentUserId, selectCharRequest.characterId);
Character chara = Database.getCharacter(client.currentUserId, selectCharRequest.characterId);
World world = null;
@ -187,7 +187,7 @@ namespace FFXIVClassic_Lobby_Server
BasePacket.encryptPacket(client.blowfish, basePacket);
client.queuePacket(basePacket);
Program.Log.Info(String.Format("User {0} => Error; invalid server id: \"{1}\"", client.currentUserId, worldId));
Program.Log.Info("User {0} => Error; invalid server id: \"{1}\"", client.currentUserId, worldId);
return;
}
@ -207,7 +207,7 @@ namespace FFXIVClassic_Lobby_Server
BasePacket.encryptPacket(client.blowfish, basePacket);
client.queuePacket(basePacket);
Program.Log.Info(String.Format("User {0} => Error; name taken: \"{1}\"", client.currentUserId, charaReq.characterName));
Program.Log.Info("User {0} => Error; name taken: \"{1}\"", client.currentUserId, charaReq.characterName);
return;
}
else
@ -219,7 +219,7 @@ namespace FFXIVClassic_Lobby_Server
client.newCharaName = name;
}
Program.Log.Info(String.Format("User {0} => Character reserved \"{1}\"", client.currentUserId, name));
Program.Log.Info("User {0} => Character reserved \"{1}\"", client.currentUserId, name);
break;
case 0x02://Make
CharaInfo info = CharaInfo.getFromNewCharRequest(charaReq.characterInfoEncoded);
@ -272,7 +272,7 @@ namespace FFXIVClassic_Lobby_Server
cid = client.newCharaCid;
name = client.newCharaName;
Program.Log.Info(String.Format("User {0} => Character created \"{1}\"", client.currentUserId, name));
Program.Log.Info("User {0} => Character created \"{1}\"", client.currentUserId, name);
break;
case 0x03://Rename
@ -286,20 +286,20 @@ namespace FFXIVClassic_Lobby_Server
BasePacket.encryptPacket(client.blowfish, basePacket);
client.queuePacket(basePacket);
Program.Log.Info(String.Format("User {0} => Error; name taken: \"{1}\"", client.currentUserId, charaReq.characterName));
Program.Log.Info("User {0} => Error; name taken: \"{1}\"", client.currentUserId, charaReq.characterName);
return;
}
Program.Log.Info(String.Format("User {0} => Character renamed \"{1}\"", client.currentUserId, name));
Program.Log.Info("User {0} => Character renamed \"{1}\"", client.currentUserId, name);
break;
case 0x04://Delete
Database.deleteCharacter(charaReq.characterId, charaReq.characterName);
Program.Log.Info(String.Format("User {0} => Character deleted \"{1}\"", client.currentUserId, name));
Program.Log.Info("User {0} => Character deleted \"{1}\"", client.currentUserId, name);
break;
case 0x06://Rename Retainer
Program.Log.Info(String.Format("User {0} => Retainer renamed \"{1}\"", client.currentUserId, name));
Program.Log.Info("User {0} => Retainer renamed \"{1}\"", client.currentUserId, name);
break;
}

View file

@ -4,12 +4,12 @@ using System.Threading;
using MySql.Data.MySqlClient;
using System.Reflection;
using FFXIVClassic.Common;
using NLog;
namespace FFXIVClassic_Lobby_Server
{
class Program
{
public static Log Log;
public static Logger Log;
static void Main(string[] args)
{
@ -24,20 +24,7 @@ namespace FFXIVClassic_Lobby_Server
if (!ConfigConstants.load())
startServer = false;
Log = new Log(ConfigConstants.OPTIONS_LOGPATH, ConfigConstants.OPTIONS_LOGFILE, Int32.Parse(ConfigConstants.OPTIONS_LOGLEVEL));
Thread thread = new Thread(() =>
{
while (true)
{
if (Log.LogQueue.Count > 0)
{
var message = Program.Log.LogQueue.Dequeue();
Program.Log.WriteMessage(message.Item1, message.Item2);
}
}
});
thread.Start();
Log = LogManager.GetCurrentClassLogger();
Program.Log.Info("--------FFXIV 1.0 Lobby Server--------");
@ -47,7 +34,7 @@ namespace FFXIVClassic_Lobby_Server
Program.Log.Info("Version: " + vers.ToString());
//Test DB Connection
Program.Log.Info(String.Format("Testing DB connection to \"{0}\"... ", ConfigConstants.DATABASE_HOST));
Program.Log.Info("Testing DB connection to \"{0}\"... ", ConfigConstants.DATABASE_HOST);
using (MySqlConnection conn = new MySqlConnection(String.Format("Server={0}; Port={1}; Database={2}; UID={3}; Password={4}", ConfigConstants.DATABASE_HOST, ConfigConstants.DATABASE_PORT, ConfigConstants.DATABASE_NAME, ConfigConstants.DATABASE_USERNAME, ConfigConstants.DATABASE_PASSWORD)))
{
try
@ -55,7 +42,7 @@ namespace FFXIVClassic_Lobby_Server
conn.Open();
conn.Close();
Program.Log.Status("[OK]");
Program.Log.Info("[OK]");
}
catch (MySqlException e)
{

View file

@ -5,6 +5,7 @@ using System.Net.Sockets;
using System.Threading;
using FFXIVClassic_Lobby_Server.packets;
using FFXIVClassic.Common;
using NLog;
namespace FFXIVClassic_Lobby_Server
{
@ -25,7 +26,7 @@ namespace FFXIVClassic_Lobby_Server
private void socketCleanup()
{
Program.Log.Debug(String.Format("Cleanup thread started; it will run every {0} seconds.", CLEANUP_THREAD_SLEEP_TIME));
Program.Log.Info("Cleanup thread started; it will run every {0} seconds.", CLEANUP_THREAD_SLEEP_TIME);
while (!killCleanupThread)
{
int count = 0;
@ -40,7 +41,7 @@ namespace FFXIVClassic_Lobby_Server
}
}
if (count != 0)
Program.Log.Status(String.Format("{0} connections were cleaned up.", count));
Program.Log.Info("{0} connections were cleaned up.", count);
Thread.Sleep(CLEANUP_THREAD_SLEEP_TIME*1000);
}
}
@ -80,7 +81,7 @@ namespace FFXIVClassic_Lobby_Server
}
Console.ForegroundColor = ConsoleColor.White;
Program.Log.Debug(String.Format("Lobby Server has started @ {0}:{1}", (mServerSocket.LocalEndPoint as IPEndPoint).Address, (mServerSocket.LocalEndPoint as IPEndPoint).Port));
Program.Log.Debug("Lobby Server has started @ {0}:{1}", (mServerSocket.LocalEndPoint as IPEndPoint).Address, (mServerSocket.LocalEndPoint as IPEndPoint).Port);
Console.ForegroundColor = ConsoleColor.Gray;
mProcessor = new PacketProcessor();
@ -105,7 +106,7 @@ namespace FFXIVClassic_Lobby_Server
conn.socket.BeginReceive(conn.buffer, 0, conn.buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), conn);
//Queue the accept of the next incomming connection
mServerSocket.BeginAccept(new AsyncCallback(acceptCallback), mServerSocket);
Program.Log.Status(String.Format("Connection {0}:{1} has connected.", (conn.socket.RemoteEndPoint as IPEndPoint).Address, (conn.socket.RemoteEndPoint as IPEndPoint).Port));
Program.Log.Info("Connection {0}:{1} has connected.", (conn.socket.RemoteEndPoint as IPEndPoint).Address, (conn.socket.RemoteEndPoint as IPEndPoint).Port);
}
catch (SocketException)
{
@ -179,7 +180,7 @@ namespace FFXIVClassic_Lobby_Server
}
else
{
Program.Log.Status(String.Format("{0} has disconnected.", conn.currentUserId == 0 ? conn.getAddress() : "User " + conn.currentUserId));
Program.Log.Info("{0} has disconnected.", conn.currentUserId == 0 ? conn.getAddress() : "User " + conn.currentUserId);
lock (mConnectionList)
{
@ -192,7 +193,7 @@ namespace FFXIVClassic_Lobby_Server
{
if (conn.socket != null)
{
Program.Log.Status(String.Format("{0} has disconnected.", conn.currentUserId == 0 ? conn.getAddress() : "User " + conn.currentUserId));
Program.Log.Info("{0} has disconnected.", conn.currentUserId == 0 ? conn.getAddress() : "User " + conn.currentUserId);
lock (mConnectionList)
{

View file

@ -1,7 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Cyotek.CircularBuffer" version="1.0.0.0" targetFramework="net45" />
<package id="Dapper" version="1.42" targetFramework="net45" />
<package id="MySql.Data" version="6.9.7" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Cyotek.CircularBuffer" version="1.0.0.0" targetFramework="net45" />
<package id="Dapper" version="1.42" targetFramework="net45" />
<package id="MySql.Data" version="6.9.7" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<package id="NLog" version="4.3.4" targetFramework="net45" />
<package id="NLog.Config" version="4.3.4" targetFramework="net45" />
<package id="NLog.Schema" version="4.3.4" targetFramework="net45" />
</packages>

View file

@ -336,7 +336,7 @@ namespace FFXIVClassic_Lobby_Server.packets
#if DEBUG
Console.BackgroundColor = ConsoleColor.DarkYellow;
Program.Log.Debug(String.Format("IsAuth: {0} Size: 0x{1:X}, NumSubpackets: {2}{3}{4}", header.isAuthenticated, header.packetSize, header.numSubpackets, Environment.NewLine, Utils.ByteArrayToHex(getHeaderBytes())));
Program.Log.Debug("IsAuth: {0} Size: 0x{1:X}, NumSubpackets: {2}{3}{4}", header.isAuthenticated, header.packetSize, header.numSubpackets, Environment.NewLine, Utils.ByteArrayToHex(getHeaderBytes()));
foreach (SubPacket sub in getSubpackets())
{

View file

@ -140,20 +140,14 @@ namespace FFXIVClassic_Lobby_Server.packets
public void debugPrintSubPacket()
{
#if DEBUG
Console.BackgroundColor = ConsoleColor.DarkRed;
Program.Log.Debug(String.Format("Size: 0x{0:X}{1}{2}", header.subpacketSize, Environment.NewLine, Utils.ByteArrayToHex(getHeaderBytes())));
Program.Log.Debug("Size: 0x{0:X}{1}{2}", header.subpacketSize, Environment.NewLine, Utils.ByteArrayToHex(getHeaderBytes()));
if (header.type == 0x03)
{
Program.Log.Debug(String.Format("Opcode: 0x{0:X}{1}{2}", gameMessage.opcode, Environment.NewLine, Utils.ByteArrayToHex(getGameMessageBytes(), SUBPACKET_SIZE)));
Program.Log.Debug("Opcode: 0x{0:X}{1}{2}", gameMessage.opcode, Environment.NewLine, Utils.ByteArrayToHex(getGameMessageBytes(), SUBPACKET_SIZE));
}
Console.BackgroundColor = ConsoleColor.DarkMagenta;
Program.Log.Debug(String.Format("Data: {0}{1}", Environment.NewLine, Utils.ByteArrayToHex(data, SUBPACKET_SIZE + GAMEMESSAGE_SIZE)));
Console.BackgroundColor = ConsoleColor.Black;
Program.Log.Debug("Data: {0}{1}", Environment.NewLine, Utils.ByteArrayToHex(data, SUBPACKET_SIZE + GAMEMESSAGE_SIZE));
#endif
}