Permalink
Browse files

added log4net

  • Loading branch information...
1 parent 2781a80 commit 183a6aeee7664ceadc97a1da5336542cbfa2e4e3 @lformella committed May 23, 2012
@@ -19,12 +19,15 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
+using log4net;
using XG.Core;
namespace XG.Client.Web
{
public class FileLoaderWeb
- {
+ {
+ private static readonly ILog myLog = LogManager.GetLogger(typeof(FileLoaderWeb));
+
private Dictionary<string, string> myDicStr;
private Dictionary<string, byte[]> myDicByt;
@@ -68,7 +71,7 @@ public string LoadFile(string aFile, string[] aLanguages)
}
catch (Exception ex)
{
- this.Log("LoadFile(" + aFile + ") " + XGHelper.GetExceptionMessage(ex), LogLevel.Exception);
+ myLog.Fatal("LoadFile(" + aFile + ")", ex);
}
#endif
}
@@ -112,20 +115,11 @@ public byte[] LoadImage(string aFile)
}
catch (Exception ex)
{
- this.Log("LoadImage(" + aFile + ") " + XGHelper.GetExceptionMessage(ex), LogLevel.Exception);
+ myLog.Fatal("LoadImage(" + aFile + ")", ex);
}
#endif
}
return null;
}
-
- #region LOG
-
- private void Log(string aData, LogLevel aLevel)
- {
- XGHelper.Log("FileLoaderWeb." + aData, aLevel);
- }
-
- #endregion
}
}
@@ -105,5 +105,6 @@
<ProjectReference type="Project" localcopy="True" refto="XG.Client" />
<ProjectReference type="Project" localcopy="True" refto="XG.Core" />
<ProjectReference type="Gac" localcopy="True" refto="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
+ <ProjectReference specificVersion="False" type="Assembly" localcopy="True" refto="../XG.Lib/log4net.dll" />
</References>
</Project>
View
@@ -39,18 +39,6 @@ public enum FilePartState : byte
Broken
}
- [Flags]
- public enum LogLevel : byte
- {
- Traffic,
- Info,
- Notice,
- Warning,
- Error,
- Exception,
- None
- }
-
[Flags]
public enum TCPServerResponse : byte
{
@@ -415,41 +403,5 @@ public static string ShrinkFileName(string aName, Int64 aSize)
}
#endregion
-
- #region LOGGING
-
- private static object LogLock = new object();
- public static LogLevel LogLevel = LogLevel.Warning;
- public static void Log(string aData, LogLevel aLevel)
- {
- if ((Int16)aLevel >= (Int16)LogLevel)
- {
- lock (LogLock)
- {
- Console.Write(DateTime.Now.ToString() + " ");
- switch (aLevel)
- {
- case LogLevel.Traffic: Console.Write("TRAFFIC "); break;
- case LogLevel.Info: Console.Write("INFO "); break;
- case LogLevel.Notice: Console.Write("NOTICE "); break;
- case LogLevel.Warning: Console.Write("WARNING "); break;
- case LogLevel.Error: Console.Write("ERROR "); break;
- case LogLevel.Exception: Console.Write("EXCEPTION"); break;
- }
- Console.WriteLine(" " + aData);
- }
- }
- }
-
- public static string GetExceptionMessage(Exception aException)
- {
-#if DEBUG
- return aException.ToString();
-#else
- return aException.Message;
-#endif
- }
-
- #endregion
}
}
View
Binary file not shown.
@@ -20,6 +20,7 @@
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
+using log4net;
using XG.Core;
namespace XG.Server.Backend.File
@@ -28,6 +29,8 @@ public class FileBackend : IServerPlugin
{
#region VARIABLES
+ private static readonly ILog myLog = LogManager.GetLogger(typeof(FileBackend));
+
private ServerRunner myRunner;
private BinaryFormatter myFormatter = new BinaryFormatter();
@@ -36,7 +39,6 @@ public class FileBackend : IServerPlugin
private bool isSaveFile = false;
private object mySaveFileLock = new object();
-
private object mySaveSearchLock = new object();
#endregion
@@ -169,11 +171,11 @@ private void Save(object aObj, string aFile)
try { System.IO.File.Move(aFile, aFile + ".bak"); }
catch (Exception) { };
System.IO.File.Move(aFile + ".new", aFile);
- this.Log("Save(" + aFile + ")", LogLevel.Info);
+ myLog.Debug("Save(" + aFile + ")");
}
catch (Exception ex)
{
- this.Log("Save(" + aFile + ") : " + XGHelper.GetExceptionMessage(ex), LogLevel.Exception);
+ myLog.Fatal("Save(" + aFile + ")", ex);
}
}
@@ -192,22 +194,22 @@ private object Load(string aFile)
Stream streamRead = System.IO.File.OpenRead(aFile);
obj = this.myFormatter.Deserialize(streamRead);
streamRead.Close();
- this.Log("Load(" + aFile + ")", LogLevel.Info);
+ myLog.Debug("Load(" + aFile + ")");
}
catch (Exception ex)
{
- this.Log("Load(" + aFile + ") : " + XGHelper.GetExceptionMessage(ex), LogLevel.Exception);
+ myLog.Fatal("Load(" + aFile + ")" , ex);
// try to load the backup
try
{
Stream streamRead = System.IO.File.OpenRead(aFile + ".bak");
obj = this.myFormatter.Deserialize(streamRead);
streamRead.Close();
- this.Log("Load(" + aFile + ".bak)", LogLevel.Info);
+ myLog.Debug("Load(" + aFile + ".bak)");
}
catch (Exception)
{
- this.Log("Load(" + aFile + ".bak) : " + XGHelper.GetExceptionMessage(ex), LogLevel.Exception);
+ myLog.Fatal("Load(" + aFile + ".bak)", ex);
}
}
}
@@ -289,20 +291,6 @@ private void SaveFileDataNow()
}
#endregion
-
- #region LOG
-
- /// <summary>
- /// Calls XGHelper.Log()
- /// </summary>
- /// <param name="aData"></param>
- /// <param name="aLevel"></param>
- private void Log(string aData, LogLevel aLevel)
- {
- XGHelper.Log("FileBackend." + aData, aLevel);
- }
-
- #endregion
}
}
@@ -19,5 +19,6 @@
<References>
<ProjectReference type="Project" localcopy="True" refto="XG.Server" />
<ProjectReference type="Project" localcopy="True" refto="XG.Core" />
+ <ProjectReference specificVersion="False" type="Assembly" localcopy="True" refto="../XG.Lib/log4net.dll" />
</References>
</Project>
@@ -21,13 +21,16 @@
using System.Collections.Generic;
using System.IO;
using HtmlAgilityPack;
+using log4net;
using XG.Core;
using System.Threading;
namespace XG.Server.Backend.MySql
{
public class Importer
{
+ private static readonly ILog myLog = LogManager.GetLogger(typeof(MySqlBackend));
+
private RootObject myRootObject;
public event ObjectObjectDelegate ObjectAddedEvent;
@@ -69,15 +72,15 @@ public void Import(string aFile)
this.myRootObject.AddServer(server);
s = this.GetServer(server);
this.ObjectAddedEvent(this.myRootObject, s);
- Console.WriteLine("-> " + server);
+ myLog.Debug("-> " + server);
}
if(this.GetChannelFromServer(s, channel) == null)
{
s.AddChannel(channel);
XGChannel c = this.GetChannelFromServer(s, channel);
this.ObjectAddedEvent(s, c);
- Console.WriteLine("-> " + server + " - " + channel);
+ myLog.Debug("-> " + server + " - " + channel);
}
//Thread.Sleep(500);
}
@@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
+using log4net;
using MySql.Data.MySqlClient;
using XG.Core;
@@ -27,6 +28,8 @@ public class MySqlBackend : IServerPlugin
{
#region VARIABLES
+ private static readonly ILog myLog = LogManager.GetLogger(typeof(MySqlBackend));
+
private ServerRunner myRunner;
private MySqlConnection myDbConnection;
private Thread myServerThread;
@@ -36,6 +39,8 @@ public class MySqlBackend : IServerPlugin
public MySqlBackend (ServerRunner aParent)
{
+ this.myRunner = aParent;
+
string connectionString = "Server=" + Settings.Instance.MySqlBackendServer + ";Database=" + Settings.Instance.MySqlBackendDatabase + ";User ID=" + Settings.Instance.MySqlBackendUser + ";Password=" + Settings.Instance.MySqlBackendPassword + ";Pooling=false";
try
{
@@ -44,7 +49,7 @@ public MySqlBackend (ServerRunner aParent)
}
catch (Exception ex)
{
- this.Log ("MySqlBackend(" + connectionString + ") : " + XGHelper.GetExceptionMessage (ex), LogLevel.Exception);
+ myLog.Fatal("MySqlBackend(" + connectionString + ") ", ex);
// stop it
this.Stop ();
@@ -62,7 +67,7 @@ public MySqlBackend (ServerRunner aParent)
dic.Add ("guid", Guid.Empty);
foreach(XGServer serv in this.ExecuteQuery ("SELECT * FROM server;", null, typeof(XGServer)))
{
- aParent.RootObject.AddServer(serv);
+ this.myRunner.RootObject.AddServer(serv);
dic["guid"] = serv.Guid.ToString ();
foreach(XGChannel chan in this.ExecuteQuery ("SELECT * FROM channel WHERE ParentGuid = @guid;", dic, typeof(XGChannel)))
@@ -85,9 +90,15 @@ public MySqlBackend (ServerRunner aParent)
#endregion
+ // the file data
+ this.myRunner.Files = new List<XGFile>();
+
+ // previous searches
+ this.myRunner.Searches = new List<string>();
+
/**/
// import routine
- Importer importer = new Importer(aParent.RootObject);
+ Importer importer = new Importer(this.myRunner.RootObject);
importer.ObjectAddedEvent += new ObjectObjectDelegate (myRunner_ObjectAddedEventHandler);
@@ -138,7 +149,7 @@ private void CloseClient ()
}
catch (Exception ex)
{
- this.Log ("CloseClient() : " + XGHelper.GetExceptionMessage (ex), LogLevel.Exception);
+ myLog.Fatal("CloseClient() ", ex);
}
}
@@ -338,17 +349,17 @@ protected void ExecuteNonQuery (string aSql, Dictionary<string, object> aDic)
}
catch (MySqlException ex)
{
- this.Log ("ExecuteQuery(" + ex.Number + ") '" + this.GetSqlString (aSql, aDic) + "' : " + XGHelper.GetExceptionMessage (ex), LogLevel.Exception);
+ myLog.Fatal("ExecuteQuery(" + ex.Number + ") '" + this.GetSqlString (aSql, aDic) + "' ", ex);
}
catch (InvalidOperationException ex)
{
- this.Log ("ExecuteQuery() '" + this.GetSqlString (aSql, aDic) + "' : " + XGHelper.GetExceptionMessage (ex), LogLevel.Exception);
- this.Log ("ExecuteQuery() : stopping server plugin!", LogLevel.Warning);
+ myLog.Fatal("ExecuteQuery() '" + this.GetSqlString (aSql, aDic) + "' ", ex);
+ myLog.Warn("ExecuteQuery() : stopping server plugin!");
this.Stop ();
}
catch (Exception ex)
{
- this.Log ("ExecuteQuery() '" + this.GetSqlString (aSql, aDic) + "' : " + XGHelper.GetExceptionMessage (ex), LogLevel.Exception);
+ myLog.Fatal("ExecuteQuery() '" + this.GetSqlString (aSql, aDic) + "' ", ex);
}
}
}
@@ -389,17 +400,17 @@ protected List<XGObject> ExecuteQuery (string aSql, Dictionary<string, object> a
}
catch (MySqlException ex)
{
- this.Log ("ExecuteReader(" + ex.Number + ") '" + this.GetSqlString (aSql, aDic) + "' : " + XGHelper.GetExceptionMessage (ex), LogLevel.Exception);
+ myLog.Fatal("ExecuteReader(" + ex.Number + ") '" + this.GetSqlString (aSql, aDic) + "' ", ex);
}
catch (InvalidOperationException ex)
{
- this.Log ("ExecuteReader() '" + this.GetSqlString (aSql, aDic) + "' : " + XGHelper.GetExceptionMessage (ex), LogLevel.Exception);
- this.Log ("ExecuteReader() : stopping server plugin!", LogLevel.Warning);
+ myLog.Fatal("ExecuteReader() '" + this.GetSqlString (aSql, aDic) + "' ", ex);
+ myLog.Warn("ExecuteReader() : stopping server plugin!");
this.Stop ();
}
catch (Exception ex)
{
- this.Log ("ExecuteReader() '" + this.GetSqlString (aSql, aDic) + "' : " + XGHelper.GetExceptionMessage (ex), LogLevel.Exception);
+ myLog.Fatal("ExecuteReader() '" + this.GetSqlString (aSql, aDic) + "' ", ex);
}
}
}
@@ -455,19 +466,5 @@ protected string GetSqlString (string aSql, Dictionary<string, object> aDic)
}
#endregion
-
- #region LOG
-
- /// <summary>
- /// Calls XGHelper.Log()
- /// </summary>
- /// <param name="aData"></param>
- /// <param name="aLevel"></param>
- private void Log (string aData, LogLevel aLevel)
- {
- XGHelper.Log ("MySqlBackend." + aData, aLevel);
- }
-
- #endregion
}
}
@@ -32,5 +32,6 @@
<ProjectReference specificVersion="False" type="Assembly" localcopy="True" refto="lib/MySql.Data.dll" />
<ProjectReference type="Gac" localcopy="True" refto="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<ProjectReference specificVersion="False" type="Assembly" localcopy="True" refto="lib/HtmlAgilityPack.dll" />
+ <ProjectReference specificVersion="False" type="Assembly" localcopy="True" refto="../XG.Lib/log4net.dll" />
</References>
</Project>
Oops, something went wrong.

0 comments on commit 183a6ae

Please sign in to comment.