ScriptDevCenter
Wanderer
hi,
i was switching through the code of the latest SVN (Server and Scripts) and have seen some really strange pieces of code:
as an example there is A LOT of blocks like
that (bad) practice has found the way into custom scripts already, since the existinging script code is/can/should be used as a "guide" on how to implement things.
exceptions are there for a reason. exceptions should not be swallowed like this.
i dont want to rise the finger without giving an alternative to that practice:
using logger and logging api's.
after searching a while, i found log4net as a wide featured customizeable (provides settings via xml, features like loglevel and several appenders for console logging, file logging etc are included) logging framework.
it can simply be used as e.g. a component in Server.Core.
there is a somehow hand written "logging api" already implemented in Core to log the commands used in game.
but there is no
useablility in the system. instead all (except the command logging) is done with
and then the console.out is set to write to file, or to the windows event log or stay as console output.
so i'd like to suggest providing a logging api from Core, that can be accessed everywhere to use it everywhere.
once it is included, the swallowed exceptions can be handled from
to (info? or debug because was not mentioned before anyway.. but on some level say whats happened )
the access is easy and can take place in the scripts / custom scripts as well using just
Core.Logger.[LEVEL]("log");
there are countless advantages using a logger api (easy exception propagation is just one), you can add or modify the "appender" by the config file to log to file AND console... eehm.. countless advantages
here are the steps taken to use the log4net framework (could be some other framework too.. just using some framework to give levelable logging to custom scripters from the "Core hand")
in Main.cs adding:
in Data\Assemblies.cfg adding:
last but not least, a configuration file for the logger (has to be named as the binary with ".config" extension e.g. RunUO.exe.config):
links:
log4net home
some conversionPattern info
would be nice if this starts a discussion
greetings
scriptdevcenter
i was switching through the code of the latest SVN (Server and Scripts) and have seen some really strange pieces of code:
as an example there is A LOT of blocks like
Rich (BB code):
try
{
. . . program . . .
}
catch
{
}
that (bad) practice has found the way into custom scripts already, since the existinging script code is/can/should be used as a "guide" on how to implement things.
exceptions are there for a reason. exceptions should not be swallowed like this.
i dont want to rise the finger without giving an alternative to that practice:
using logger and logging api's.
after searching a while, i found log4net as a wide featured customizeable (provides settings via xml, features like loglevel and several appenders for console logging, file logging etc are included) logging framework.
it can simply be used as e.g. a component in Server.Core.
there is a somehow hand written "logging api" already implemented in Core to log the commands used in game.
but there is no
Code:
Core.Logger.Debug("some debug information, printed if loglevel is set to 'debug'.");
Code:
Console.WriteLine("some information, always ever printed.");
so i'd like to suggest providing a logging api from Core, that can be accessed everywhere to use it everywhere.
once it is included, the swallowed exceptions can be handled from
Rich (BB code):
catch
{
}
Rich (BB code):
catch (Exception e)
{
Core.Logger.Info("was swallowed first, so just for information", e);
}
the access is easy and can take place in the scripts / custom scripts as well using just
Core.Logger.[LEVEL]("log");
there are countless advantages using a logger api (easy exception propagation is just one), you can add or modify the "appender" by the config file to log to file AND console... eehm.. countless advantages
here are the steps taken to use the log4net framework (could be some other framework too.. just using some framework to give levelable logging to custom scripters from the "Core hand")
in Main.cs adding:
Rich (BB code):
using System.Runtime;
using log4net;
[assembly: log4net.Config.XmlConfigurator(Watch = true)] // notice changes on the log config file while running
namespace Server
{
public delegate void Slice();
public static class Core
{
public static ILog Logger { get { return m_logger != null ? m_logger : (m_logger = LogManager.GetLogger("NameOfTheLogger")); } }
in Data\Assemblies.cfg adding:
Rich (BB code):
System.Windows.Forms.dll
log4net.dll
last but not least, a configuration file for the logger (has to be named as the binary with ".config" extension e.g. RunUO.exe.config):
Rich (BB code):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Register a section handler for the log4net section -->
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define some output appenders -->
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-5level] [%location] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="INFO" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
</log4net>
</configuration>
links:
log4net home
some conversionPattern info
would be nice if this starts a discussion
greetings
scriptdevcenter