Custom Activities - Error Handling / Log Messages

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

I cannot see 22.7 under stable, only 22.4. I wonder if its a difference of packages you use in UiPath. The following are my packages and versions:

"UiPath.Excel.Activities": "2.11.4",
"UiPath.System.Activities": "21.10.4",
"UiPath.UIAutomation.Activities": "21.10.5"

For the UiPath.Robot.Activities.Api packages, I have tried to use: 22.4.1, 21.10.3, and 21.4.3, none of which work in UiPath Studio when I go to run my code, exactly as you have it. All of them result in an Object Null Reference error. Could you provide your package versions that worked and mirroring my versions as well?

@Jacob-Roboyo

Hello Jacob,
yes, release 22.7 is a preview. The package versions you reference in your workflow are without significance. It is necessary to reference it in your Visual Studio.
Best regards
Stefan

I would agree, however the packages seem to compile and I do not have any errors in Visual Studio, its only when I go to run it in UiPath Studio. That being said, I tried to uninstall UiPath Studio community, use both community and enterprise licenses/installations, but nothing seems to fix this issue.

I have an issue with the following two lines:

var jobInfo = executorRuntime.RunningJobInformation;
executorRuntime.LogMessage(new LogMessage…);

I cannot figure out what is causing the Null object reference to both of these lines. If I separate the new LogMessage into a different variable, I can confirm the variable is created without issues. However, something about that .LogMessage command is giving me issues. Any ideas what could be causing the issues?

@Jacob-Roboyo

Hello Jacob,
I assume it is an error in line

var executorRuntime = context.GetExtension<IExecutorRuntime>();

because it delivers null in executorRuntime from GetExtension method and that could be the reason for the null object reference error in the following line. The method returns an extension of the specified type IExecutorRuntime. This should be available, no idea why not.
Best regards
Stefan