I am creating some custom activities using UiPath and I had a couple of questions I could use some help with. Is there a way to use the UiPath log messages in a custom activity created in visual studio? Currently I have tried Console.WriteLine and the NLog package, but both seem to write logs at a “trace” level. I am looking to write logs at an info or error level as well within my custom code.
Secondly, is there a way to do custom error messages? Right now, if my custom component fails, I get an error that says “One or more errors have occurred”. How do I prevent that and created my own error messages? I am trying to make this as readable as possible to my end users. Thank you!
Errors bubble up to the parent automation the same as any other activity. The issue here isn’t that it’s a custom activity, and there’s nothing that needs to be fixed. This is happening because the error message from the code/activity inside your custom activity isn’t giving a helpful error message. You need to do error handling within your custom activity and then throw useful error messages.
Currently I am throwing my own custom errors, like this one below. Instead of the expected error message displayed, I am getting “One or More errors occurred” in studio:
if (!File.Exists(excelFilePath)) throw new FileNotFoundException("The following Workbook could not be found at the given path: " + excelFilePath);
I’m sorry, I didn’t realize you’re doing this via VB rather than just a XAML library. Custom libraries created in UiPath Studio are very simple to do. If you could achieve your goal that way I recommend it over VB. Unfortunately, I don’t know much about creating them in VB and won’t be able to help you with this error situation.
@loginerror Hey friend, would you happen to have anyone from your team that may be able to help with custom components created in Visual Studio? Sorry to tag you, but you seem to have a lot of knowledge around the forums
Although I don’t know if I can help here myself, I’m sure great Forum minds such as @StefanSchnell, @ppr and @balupad14 will chime in with more information
To use the log messages in the context of an UiPath workflow it is necessary to reference also to the assemblies UiPath.System.Activities.dll and UiPath.Activities.Contracts.dll. Here an example how to use UiPath log messages with different log levels in your custom activity:
//-Begin----------------------------------------------------------------
using System;
using System.Activities;
using System.ComponentModel;
using System.Diagnostics;
using UiPath.Activities.Contracts;
using UiPath.Shared.Contracts;
namespace LogCustomActivity {
public class LogTest : CodeActivity {
public enum LogLevel {
Fatal = 1,
Error = 2,
Warn = 4,
Info = 8,
Trace = 16
}
[Category("Input")]
[RequiredArgument]
public InArgument<string> Message { get; set; }
[Category("Input")]
[DefaultValue(null)]
public InArgument<LogLevel> Level { get; set; }
protected override void Execute(CodeActivityContext context) {
if((InArgument<LogLevel>)Level.Get(context) == null) {
Level.Set(context, LogLevel.Info);
}
IWorkflowRuntime workflowRuntime = context.GetExtension<IWorkflowRuntime>();
LogMessage logMessage = new LogMessage();
logMessage.EventType = (TraceEventType)Level.Get(context);
logMessage.Message = Message.Get(context);
workflowRuntime.Log(logMessage, context.WorkflowInstanceId);
}
}
}
//-End------------------------------------------------------------------
The input is in this example the message text and the logging level. As you can see you need only five lines of code. I tried this example in Windows compatibility mode and as PlatformTarget attribute I set x64.
Hey @StefanSchnell, I reached out to UiPath partner team directly and they were able to help me out a bit with my error handling problem, however I still have the issue of log messages. They reached out to me requesting that I look at this link (https://docs.uipath.com/studio/docs/building-activities-project-settings#get-process-id-and-logs), however when I implement the example code like below, I am getting a null object error “Object reference not set to an instance of an object”. Any ideas or help would be appreciated.
@StefanSchnell I was unable to get that example to work, do you have the Contracts dll imported in your project? I will try that on my end to see if that makes a difference.
Hello Jacob,
sure. As you can see I have changed nothing. All I add are the System.Diagnostics namespace for the TraceEventType enumeration and UiPath.Robot.Activities.Api namespace for the UiPath specific characteristics. The documentation you give above refers to release 22.4, I use 22.7, maybe that is still a difference.
Best regards
Stefan
//-Begin----------------------------------------------------------------
using System;
using System.Activities;
using System.ComponentModel;
using System.Diagnostics;
using UiPath.Robot.Activities.Api;
namespace LogMessageActivity {
public class LogTest : CodeActivity {
protected override void Execute(CodeActivityContext context) {
var executorRuntime = context.GetExtension<IExecutorRuntime>();
var jobInfo = executorRuntime.RunningJobInformation;
executorRuntime.LogMessage(new LogMessage {
EventType = TraceEventType.Warning,
Message = $"Job {jobInfo.JobId}: My log message from workflow {jobInfo.WorkflowFilePath}"
});
}
}
}
//-End------------------------------------------------------------------