Log Levels
In gSharp, log levels are used to categorize log entries based on their severity and importance. This allows developers and users to filter logs effectively—critical information is easily accessible, while less important details are excluded unless they are required.
The log levels in gSharp are defined as follows, in order of increasing severity:
| Level | Value | Description |
|---|---|---|
| Trace | 0 | Very detailed or repetitive information. |
| Debug | 1 | Information that is useful for determining the state and flow of the module. |
| Info | 2 | General information about the module's operation. |
| Warn | 3 | Information about unexpected situations that do not cause harm. |
| Error | 4 | Errors that need attention and may cause incorrect behaviour. |
| Fatal | 5 | Severe errors that are considered much more critical than regular errors (see below). |
Log Level Settings
In the Settings tab of the gSharp administration app, users can adjust the minimum log level for each assembly, according to the Value column in the table above. All log entries lower than the minimum level will be excluded from the logs. This allows users to control the verbosity of logs generated by modules in that assembly when they are running under a gSharp server.
For example, if the minimum log level is set to Info, then all log entries at the Info, Warn, Error, and Fatal levels will be logged because they are higher than or equal to Info, while Debug and Trace entries will be excluded because they are lower.
| ⬤ | Trace | (0) | Not Logged ( < Min Level ) | ||
| ⬤ | Debug | (1) | |||
| ⬤ | Info | (2) | Logged ( ≥ Min Level ) | ||
| ⬤ | Warn | (3) | |||
| ⬤ | Error | (4) | |||
| ⬤ | Fatal | (5) |
Level Guidelines
It is recommended to follow these guidelines when choosing log levels for your module development:
| Level | Guideline |
|---|---|
| Trace | For very detailed information that is required for diagnosing a problem. This level is normally reserved for logs that would normally produce a very large set of log entries, such as logging the flow of execution through various functions, the values of variables at a very granular level, or the iterative state of loops. This level is typically only useful for diagnosing specific issues during development, and should be used with caution in production environments due to the potential for generating excessive log entries. |
| Debug | This is the most appropriate place to log messages that show the state and process of a module. For example, logs that indicate that the module has entered a certain function, or that show the values of important variables at certain points in the code. This level is ideal for logging detailed information that is useful during development and debugging, but would likely be too verbose for production use. |
| Info | For general information about the module's operation. Use caution when logging at this level in the module's Execute method, as this will log every time the module is executed, which may lead to excessive logging if the module is executed frequently. |
| Warn | For situations that are unexpected but do not cause the module to fail or function incorrectly. For example, if your module encounters a non-critical issue that may indicate a potential problem, such as a deprecated API being used or a configuration setting that is not optimal. |
| Error | For errors that are either exceptions your module has caught, or other error conditions that your module has detected, such as invalid input, failed operations, or other issues that may cause your module to behave incorrectly. |
| Fatal | For use in the most severe error events and not recommended for use by module developers. Because gSharp automatically captures and logs unhandled exceptions in your modules, there are very few circumstances which would cause the assembly host to actually crash (for example, if your code causes a stack overflow, .Net will force the process to terminate). |
The gSharp platform requires a log level to be specified for every log entry. It is important to choose carefully which level is appropriate for each log message. This is especially true at the Info, Debug, and Trace levels, to ensure that detailed diagnosis information is available, but not constantly generated.
Using log levels consistently and appropriately ensures that, regardless of how logging is configured, the most relevant information will be available when diagnosing issues.
Testbed Settings
The minimal log level settings described above are for users to configure modules in a gSharp server. As a module developer, you may need to adjust the minimum log level to test your log entries during development.
You can indicate the minimum log level for your assembly when configuring the testbed, using the WithMinimumAssemblyLogLevel method on the AssemblyBuilder class. This allows you to control the verbosity of logs generated by your modules when you are testing them in the testbed environment.
The default minimum level is Info.
internal partial class Program
{
private static void BuildTestContext1(AssemblyBuilder builder)
{
builder
.WithMinimumAssemblyLogLevel(LogLevel.Warn)
.AddModule<GSharpTankDemo.TankVolume>(moduleBuilder => moduleBuilder
// ...
);
}
}