754 views
In this article, we will look at the APIs available for logging to the logging framework and applying command messages to the CommerceContext.
The reason for grouping these two subjects together is due to seeing a lot of confusion around these areas when reviewing developers’ code in the field; there is some overlap between them, which is often overlooked.
In Sitecore Commerce, logging is based on Microsoft.Extensions.Logging, and the Sitecore Commerce Engine SDK is setup to utilise the SeriLog diagnostic library for logging.
The CommandMessages are flushed to calling CommerceCommands via the completion of the CommandActivity and are included in the response object of CommandsController APIs.
Logging and command messaging occurs within methods of the CommercePipelineExecutionContext and the CommerceContext.
CommercePipelineExecutionContext
LogInfoIf
public void LogInfoIf(bool conditionResult, string info);
Intuitive enough, the LogInfoIf method will log an Information level entry, info, if the conditionResult is met.
Abort
public override void Abort(string reason, object data);
The Abort method will abort the pipeline and will create an Error level log entry if the reason message doesn’t contain the magic string “Ok|”.
It’s also worth noting that this method is intended to abort the executing pipeline first and foremost, and the log entry is secondary. It is not intended solely for the purpose of logging.
Note: The data object would normally return the current CommercePipelineExecutionContext.
CommerceContext
AddDataMessage
public virtual void AddDataMessage(string messageType, string dataMessage);
The AddDataMessage will add the dataMessage to the command messages. It will also add the dataMessage to the logger at the Information level.
Tip: Avoid setting Debug level messages to avoid spamming your local development logs, which are defaulted to the Information level.
AddMessage
public virtual void AddMessage(CommandMessage message);
AddMessage will add a CommandMessage to the command messages.
The message will not invoke the logger.
AddMessage (Alternate)
public virtual Task<string> AddMessage(string code, string commerceTermKey, object[] args, string defaultMessage = null);
The overloaded AddMessage method’s logging behaviour is as follows:
- Message codes of ValidationError or Warning will add a Warning message to the Logger.
- All exception types will be logged using the LogException method. See LogException for more details.
- An Error will also be logged as an Error.
For the CommandMessages, the localised message will attempted to be retrieved from Sitecore, using the commerce term key provided, and further formatted/interpolated with the args provided.
LogException
public virtual void LogException(string caller, Exception ex);
The LogException method will log an exception as an error with the exception message and stack trace details.
LogExceptionAndMessage
public virtual void LogExceptionAndMessage(string caller, Exception ex);
The LogExceptionAndMessage logs the exception as per the LogException method, however the exception message will be added to the CommandMessages at the Error level in addition.
Logger
public ILogger Logger { get; }
The Logger exposes the can be utilised to add the standard log-level entries to it, being:
- LogDebug
- LogInformation
- LogWarning
- LogError
- LogCritical