Logger
By default, the RobotStudio output windows reside at the center bottom part of the view and contain information such as error messages, status updates, and so on.
In this example, we will learn how to:
- create new log message categories
- create log messages under different categories and print them to the output window
- create a log message with hyperlinks and print it to the output window
When you run this example, you can see that "This is just a test message!" is printed twice to the output window, under different categories. You will also see that the last message has three hyperlinks that perform different tasks when they are clicked.
Example
Project.UndoContext.BeginUndoStep("Logger");
try
{
// Add two categories.
if (!Logger.CategoryCaptions.ContainsKey("MyKey"))
{
Logger.CategoryCaptions.Add("MyKey", "My Category");
Logger.CategoryCaptions.Add("AnotherKey", "Another Category");
}
// Add two messages with different category.
Logger.AddMessage(new LogMessage("This is just a test message!", "MyKey"));
Logger.AddMessage(new LogMessage("This is just a test message!", "AnotherKey"));
// How to create log messages with hyperlinks.
string path = @"C:\";
string textString = "Start text {{browse to file here}} more text {{google link here}} more text {{message box here}} end text";
// Add a message with three hyperlinks.
Logger.AddMessage(new LogMessage(textString, (index) =>
{
// If the user clicks a hyperlink this action will be called with the hyperlink index as argument. Index is zero based and is counted left to right.
switch (index)
{
// If the left-most hyperlink is clicked, index will be 0.
case 0:
Process.Start("explorer.exe", $"/select,\"{path}\"");
break;
// If second (from the left) hyperlink is clicked, index will be 1.
case 1:
Process.Start("http://google.com");
break;
// If third hyperlink is clicked, index will be 2.
case 2:
MessageBox.Show("Message box");
break;
default:
break;
}
}));
}
catch
{
Project.UndoContext.CancelUndoStep(CancelUndoStepType.Rollback);
throw;
}
finally
{
Project.UndoContext.EndUndoStep();
}