Custom Activities - Error Handling / Log Messages

@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

2 Likes