Logging Exceptions
RobotStudio logs information regarding Exceptions and Errors to facilitate software diagnosis and problem solving. All developers are encouraged to log information about the execution state of a program whenever they find an exception or an error.
The ApplicationLogger class provides an interface for the user to log errors and exceptions into RobotStudio's internal application log. This topic showcases how to use this class and its methods to accomplish this.
Note
You can easily try out this example using the RobotStudio Empty Add-in template in Visual Studio.
Logging Exceptions
Use the LogError(string) method from the ApplicationLogger class to log a generic error into the application log.
try { // Division by 0 to trigger an exception. int z = 0; int i = 1 / z; } catch (Exception) { ApplicationLogger.LogError("Division by zero."); }The LogException(Exception) method from the ApplicationLogger class lets you write an exception message into the log and to RobotStudio's logger.
try { // Division by 0 to trigger an exception. int z = 0; int i = 1 / z; } catch (Exception ex) { // Adds an "Attempted to divide by zero." exception message to the application log AND to RobotStudio's logger. ApplicationLogger.LogException(ex); }When called with an additional boolean value, the LogException(Exception, bool) method writes an exception message to the log, and to RobotStudio's logger depending on the boolean value.
try { // Division by 0 to trigger an exception. int z = 0; int i = 1 / z; } catch (Exception ex) { // Adds an "Attempted to divide by zero." exception message to the application log AND to RobotStudio's logger (depending on the boolean). ApplicationLogger.LogException(ex, toOutput: true); }Specify the message that appears in the logger with a string in the LogException(string, Exception, bool) method.
try { // Division by 0 to trigger an exception. int z = 0; int i = 1 / z; } catch (Exception ex) { // Adds an "Attempted to divide by zero." exception message to the application log AND to RobotStudio's logger (depending on the boolean). string s = "This Add-In raised the divide-by-zero exception!"; ApplicationLogger.LogException(s, ex, toOutput: true); }
Other Methods
To show the location and name of the current file used as the application log by RobotStudio, use the CurrentLogFile property of the ApplicationLogger class.
// Save the current name and directory of the application log: string logFileDir = ApplicationLogger.CurrentLogFile.ToString(); // Display it in RobotStudio's logger: Logger.AddMessage(new LogMessage(logFileDir));The LogInfo(string) method adds a general message to the application log.
ApplicationLogger.LogInfo("This is an example message");Use the LogState(string, string) method to add the current context of the application.
ApplicationLogger.LogState("Initializing", "Problem when starting the program...");To generate a log message that is displayed in RobotStudio's logger, create a new instance of the ExceptionLogMessage class, and display it with the log using the AddMessage(LogMessage) method. Click this log message for more information.
try { // Division by 0 to trigger an exception. int z = 0; int i = 1 / z; } catch (Exception ex) { Logger.AddMessage(new ExceptionLogMessage("Encountered an exception", ex)); }