对于UiPath
版本18.2.3
,我成功创建了一个新的日志记录活动。这个想法是,在将所有内容记录在Orchestrator
中时,新活动还将另外记录一个文件。即将加入我们的组织将升级到UiPath 2019.4.3
,但是我的活动无法正常进行,因此我需要将UiPath引用升级到当前的dlls
。这样做为我提供了可编译的代码,但是活动导致null-pointer exception
。为什么?
这是我的代码:
using System;
using System.Activities;
using System.Activities.Presentation.Metadata;
using System.ComponentModel;
using System.IO;
using System.Text;
using UiPath.Core.Activities;
namespace CustomCode.Log
{
/// <summary>
/// Designer attribute gives a look & feel like UiPaths own LogMessage activity
/// </summary>
[Designer(typeof(UiPath.Core.Activities.Design.LogDesigner))]
public class LogMessage : CodeActivity, IRegisterMetadata
{
[Category("Input")]
public CurentLogLevel Level { get; set; }
[Category("Input")]
public InArgument<System.String> Message { get; set; }
[Category("Input")]
public InArgument<System.String> LogFilePath { get; set; }
/// <summary>
/// UiPath Log Message object reference
/// </summary>
private UiPath.Core.Activities.LogMessage _LogMessage { get; set; }
/// <summary>
/// Constructor
/// </summary>
public LogMessage()
{
DisplayName = "Extended Log Message";
Level = CurentLogLevel.Trace; // default setting
}
/// <summary>
/// declare dummy register metadata class to force UiPath to load this assembly
/// </summary>
public void Register()
{
// intentionally empty
}
/// <summary>
/// First log as if done by native UiPath then log to the file specified
/// </summary>
/// <param name="context"></param>
protected override void Execute(CodeActivityContext context)
{
// first do the usual UiPath logging...
string message = Message.Get(context);
//bool isNew = false;
#region log as UiPath standard
// it is necessary to create new objects every time.
// reassigning property values causes errors when logging is frequent...
_LogMessage = new UiPath.Core.Activities.LogMessage
{
Level = this.Level,
Message = new InArgument<string>(message),
};
// do UiPath log
try
{
WorkflowInvoker.Invoke(_LogMessage); // NULL POINTER EXCEPTION HERE
}
catch (Exception ex)
{
throw new Exception("Internal UiPath Invoke Exception ", ex);
}
#endregion
string logFilePath = LogFilePath.Get(context);
// log to file if path is specified...
if (logFilePath != null)
{
LogToFile(logFilePath, message, Level.ToString());
}
}
/// <summary>
/// Method to write a line in the log file
/// </summary>
/// <param name="filename">the file to which to log</param>
/// <param name="message">the log message</param>
/// <param name="level">the log level</param>
private void LogToFile(string filename, string message, string level)
{
StringBuilder LogLine = new StringBuilder(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
LogLine.Append("\t");
LogLine.Append(level);
LogLine.Append("\t");
LogLine.Append("\t - ");
LogLine.Append(message);
LogLine.Append(Environment.NewLine);
try
{
// append the log line
File.AppendAllText(filename, LogLine.ToString());
}
catch (Exception ex)
{
// try to log the exception in UiPath
_LogMessage.Message = new InArgument<string>(ex.Message + Environment.NewLine + ex.StackTrace);
throw new Exception("Internal append to log file exception", ex);
}
}
}
}
答案 0 :(得分:0)
您的错误表明 _LogMessage 为空,因此为空指针错误。
您可以通过简单地检查部分代码来验证这一点
_LogMessage = new UiPath.Core.Activities.LogMessage
{
Level = this.Level,
Message = new InArgument<string>(message),
};
如果您的变量为空:
if (_LogMessage.Any())
希望这会有所帮助。