We provide a simple and yet powerful logging mechanism that is used by all Nexus Libraries and SDK:s. It works with any logging framework without being dependent on any of them.
Create a log message
When you want to log something, you use the static methods of the Nexus.Link.Libraries.Core.Logging.Log
class.
Example:
IQueue queue = null;
try {
queue = Connect(_queueName);
Log.LogInformation($"Connected to queue {queue.Name}");
} catch (Exception e)
{
Log.LogError($"Could not connect to the queue {queue.Name}.", e)
throw;
}
Severity level
This is how we categorize our logs depending on severity level.
Severity level | Description |
---|---|
Verbose | Typically used for messages that are for debugging purposes. Example: Detailed steps within a API request. |
Information | Information about an important, normal event. Example 1: The application has started successfully. Example 2: An API request has been responded to (successful or not). |
Warning | The task is doing fine, but we found something out of the ordinary. |
Error | The task could not be fulfilled, but the application still works. Example: A database was temporarily unavailable |
Critical | An error that affects more than the current task. The application can't continue to work properly. Example: A storage device has reached it's storage limit. |
For a REST API, the "task" referred in the table corresponds to one incoming HTTP request, from start to end.
Information collected
When you use our log methods the following information is collected:
- Location of the log call; the line of code, the source file, the current method
- The current time
- The current StackTrace (if the severity level is Error or Critical)
- The message and an optional exception
Pick a logger
You can set FulcrumApplication.Setup.SynchronousFastLogger
to a logger of your choice. The logger must implement the interface Nexus.Link.Libraries.Core.Logging.ISyncLogger
. Example of an implementation that logs to the console:
public class ConsoleLogger : ISyncLogger
{
/// <inheritdoc />
public void LogSync(LogRecord logRecord)
{
Console.WriteLine(logRecord.ToLogString());
}
}
As you can guess from the setting, your logger should be fast and synchronous. If it isn't, see "Asynchronous logging" below.
The LogRecord
contains the collected information (see above) and ToLogString
is the default way to present the collected information.
You can also set FulcrumApplication.Setup.FallbackLogger
. The logger must implement the interface Nexus.Link.Libraries.Core.Logging.IFallbackLogger
. It is called as a last resort when the normal logging fails.
In the same NuGet package as the logging functionality you will find two ready-to-use loggers; ConsoleLogger and TraceSourceLogger. They both implement ISyncLogger
as well as IFallbackLogger
.
Asynchronous logger
If you have a slow or asynchronous logger, you must add a queue that will take care of the log messages in the background. You do this by implementing IAsyncLogger
(instead of ISyncLogger
) and set FulcrumApplication.Setup.SynchronousFastLogger
to use the Nexus.Link.Libraries.Core.Logging.QueueToAsyncLogger
as follows:
FulcrumApplication.Setup.SynchronousFastLogger = new QueueToAsyncLogger(<your logger>);
All messages will be queued on a memory queue and sent one by one to your logger in a background thread. Before your logger is called, the context is restored to the context that was current when the log message was created, so you will have access to all its values, such as CorrelationId
.
Batch logger
There is another convenient aid to your logging; Nexus.Link.Libraries.Core.Logging.BatchLogger
.