Custom Activities - Error Handling / Log Messages

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!

1 Like

Hey @Jacob-Roboyo

First I would like to understand like how are you currently handling error please ?

Anything you follow…

Thanks
#nK

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.

1 Like

Paul,

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 am able to post screenshots now.

In my visual studio, I have attached what I have, for example, if a file does not exist.

In my UiPath Studio, you can see that I run the code, get a trace log, but still see “one or more errors occurred”.


image

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 :slight_smile:

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 :slight_smile:

2 Likes

@loginerror

Thank you Maciej for bringing that post into my attention.

@Jacob-Roboyo

Hello Jacob,

to the first of your questions:

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.

Here my test workflow:

image

image

image

Best regards
Stefan

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.

image

1 Like

Apologies as I just posted a different reply, I will take a look at trying this example too.

1 Like

@Jacob-Roboyo

Thanks Jacob for this interesting example. I will try it.

@Jacob-Roboyo

Okay, so the second question is answered from the UiPath partner team. :+1:

1 Like

@StefanSchnell Where would you find either of those DLLs?

@Jacob-Roboyo

Hello Jacob,
I tried this interesting approach in Windows Legacy mode and it works :grinning:

image

Another way.
Thanks for letting us know.
Best regards
Stefan

@Jacob-Roboyo

Hello Jacob,
you can find the assemblies in the path

%USERPROFILE%\AppData\Local\Programs\UiPath\Studio\

if you use the Community Edition.
Best regards
Stefan

@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.

After importing Contracts.dll, I still could not get that example I posted to work. Would you be able to show your working version?

@Jacob-Roboyo

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------------------------------------------------------------------