|
Hi,
I finally had some time to do some catching up. I manged to get the logging working in both partycraft and my service by adding en event
I added the following code to PartyCraft.cs:
//veg 01-03-2012
public static event EventHandler<LogEventArgs> OnLog;
and changed the Log methods like:
//veg 01-03-2012
public static void Log(string format, params object[] args)
{
Log(String.Format(format, args));
}
public static void Log(string Message)
{
try
{
if (Settings == null || Message == null)
return;
string prepend = "[" + DateTime.Now.ToString("HH:mm:ss") + "] ";
if (Settings["server.debugenabled"] == "true")
{
StackTrace trace = new StackTrace();
prepend += trace.GetFrame(1).GetMethod().DeclaringType.Name + "." + trace.GetFrame(1).GetMethod().Name + ": ";
}
//veg 01-03-2012
if (OnLog != null)
OnLog(null, new LogEventArgs(prepend, Message));
if (LogToFile)
{
logStream.WriteLine(prepend + Message);
logStream.Flush();
}
}
catch
{
}
}
Note that i removed the Debug.WriteLine in this method and moved it into Program.cs like:
/// <summary>
/// Run PartyCraft
/// </summary>
static void Main(string[] args)
{
PartyCraft.SettingsDirectory = Directory.GetCurrentDirectory();
//veg 01-03-2012
PartyCraft.OnLog += new EventHandler<LogEventArgs>(PartyCraft_OnLog);
foreach (string arg in args)
{
if (arg.StartsWith("-sd:"))
PartyCraft.SettingsDirectory = arg.Substring(4); // TODO: More command line options
}
PartyCraft.Run();
}
//veg 01-03-2012
static void PartyCraft_OnLog(object sender, LogEventArgs e)
{
Console.WriteLine(e.prepend + e.Message);
}
In my server code I can now simply so my own eventlog logging.
Note that another change to PartyCraft was made (to prevent
my service from running into the Run methods main loop:
//veg: 01-03-2012 - Added loop
public static void Run(Boolean loop = true)
{
...
Log("Server started successfully!");
//veg: 01-03-2012 - Be able to skip this when running a service.
if (!loop)
{
return;
}
...
///Console code
IMHO doe sthe consoleinput code not belong in PartyCraft (if it where
an assembly) but in program.cs. The above fix i made is good
enough for me for the moment.
Finally the LogEventArgs class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PartyCraftServer
{
public class LogEventArgs : EventArgs
{
public LogEventArgs(string prepend, string Message)
{
this.prepend = prepend;
this.Message = Message;
}
public string prepend;
public string Message;
}
}
The result is now that the 'normal'logging is no longer part of
PartyCraft but of the program.cs. This allows for turning
PartyCraft into an assembly and use Program.cs as a small
demo/commandline version.
I can also subcribe to the other vents and do some additional
logging there.
I also managed to get my server running directly from Visual Studio
(without installing a service) so i should (with some further
tweaking) be able to use this as both a service and commandline version.
Because of the changed in the logging i no longer need a copy of
PartyCraft.cs customized for my service but can use PartyCraft
simply as an Assembly.
I only ran into problems (again) with your internal logging.
IMHO the main porogram should be able to specify this logging
for both PartyCraft and LubMineCraft in order to prevent acess
violations. LibMineCraft does not expose a LogFileName property nor
does it read the Settings.xml.
I also noticed that my java client can no longer login
(after displaying Loading World it stops with a connection
timeout).
It will also not read any of my worlds (the mcr fiels had an
unknown compressin methods and the latest 1.2.3 server
I downloaded has *.mca files instead of *.mcr) But i gess
thats more a LibMinecraft issue.
wvd_vegt
|