Writing Log Entries
Writing log entries in your assembly is a critical part of development, as it allows you to capture important information about the execution of your code, which can be invaluable for debugging and monitoring.
One of the many properties of the CalculationModuleBase base class that your module inherits from is the Log property. This property provides access to the logging functionality, which you can use to write log entries. This section will cover the basics of using the Log property to write log entries in your modules.
Because the Log property is set internally by the gSharp/Testbed host after it creates an instance of your module, you cannot use it in the constructor of your module. Attempting to do so will result in a NullReferenceException.
Use the Initialise() method override instead.
CalculationModuleBase.Log Property
Namespace: GHost.GSharp.Calculation
Assembly: GHost.GSharp.Calculation.dll
Type: ILogger
public ILogger Log { get; }
The Log property is of type ILogger, which provides methods for writing log entries. You can use this property to write log entries from anywhere within your module's code (except the constructor, as noted above), or pass it to other classes that require logging functionality.
public class TankVolume : ModuleBase
{
public override void Initialise()
{
// A log entry in its simplest form
Log.Info("The TankVolume module has been initialized.");
}
}
The output of this entry would be displayed in the testbed console as:
15:06:33 [INFO] The TankVolume module has been initialized.
The example above demonstrates how to write a simple log entry in the Initialise() method of a module. The Info method is used to write the message The TankVolume module has been initialized. to the log with a log level of Info.
There are like methods for all log levels, which you can use to indicate the severity of the log entry.
Log.Trace("This is a trace log entry.");
Log.Debug("This is a debug log entry.");
Log.Info("This is an info log entry.");
Log.Warn("This is a warning log entry.");
Log.Error("This is an error log entry.");
Log.Fatal("This is a fatal log entry.");
See the Log Levels page for a description of the above log levels and how to use them.
Each of these methods have multiple overloads that provide additional logging parameters. See the ILogger documentation for more details.
Log Codes
One important aspect of writing log entries is the use of log codes. A log code is a short, unique identifier for a specific log message that makes it easy to search for and filter related entries. They are not enforced but they are recommended because a single search on a code will quickly show every occurrence of that message.
The Log property provides overloads for all logging methods that allow you to include a log code as the first parameter. Using LogInfo as an example, it has the following additional overload:
void LogInfo(string logCode, string message, params object[] args);
The LogInfo overload in the above example has an optional params object[] args parameter. See the Structured Logging section for information on how this parameter is used.
Laying aside the args parameter for the moment, this method would be called as follows:
Log.Info("ModInit", "The TankVolume module has been initialized.");
When a log entry contains a log code it can be filtered in the gSharp Server Log tab's log viewer by entering the code in the Log Code filter field.
This will show only log entries that contain that code, making it easier to find relevant log messages when diagnosing issues.
It's ok to use abbreviated or mnemonic codes, and shorter codes rather than longer ones are encouraged, provided they remain unique and are used consistently.
Logging Exceptions
When an Exception is caught in your module code, it is often useful to log its details. The simplest way to do this is:
try
{
// Some code that may throw an exception
}
catch (Exception ex)
{
Log.Error(ex);
}
or, as recommeded, with a log code:
Log.Error("ModInitErr", ex);
Passing the Exception to the logging method will allow gSharp to capture and log the exception's message, stack trace, and any inner exceptions, which can be crucial for diagnosing issues.
Only the the Error and Fatal logging methods also have overloads that accept an Exception parameter. The Warn, Info, Debug and Trace methods do not.
Structured Logging
Structured logging allows you to attach structured data to your log entries, making it easier to query and analyze the logs. Instead of just writing a message with embedded values, you can (with minimal effort) attach named properties to the log entry that represent those values.
For a detailed description of structured logging and how to use it in your gSharp modules, see the Structured Logging page.