Elsa workflow write to a log file

509 views Asked by At

I want to write to a log file in an Elsa workflow. I was wondering if someone has an example of how they were able to write to a log file in an Elsa workflow. I was trying to use the OutFile activity, but I am wondering if someone has an example of writing to a file using this activity or if there is another option to write to a log file in an Elsa workflow

1

There are 1 answers

0
user1408767 On

I found the best way to handle this was to create a custom activity in code. Here is the class that was created:

[Action(
Category = "File",
DisplayName = "Write to File",
Description = "Writes text to a file")]
public class WriteToFile : Activity
{
    [Required]
    [ActivityInput(Hint = "Text to write to file.", UIHint = ActivityInputUIHints.SingleLine, SupportedSyntaxes = new[] { SyntaxNames.JavaScript, SyntaxNames.Liquid })]
    public string Text { get; set; } = default!;

    [Required]
    [ActivityInput(Hint = "Path of the file to write to.", UIHint = ActivityInputUIHints.SingleLine, SupportedSyntaxes = new[] { SyntaxNames.JavaScript, SyntaxNames.Liquid })]
    public string FilePath { get; set; } = default!;

    [ActivityInput(Hint = "How to handle an existing file.", UIHint = ActivityInputUIHints.Dropdown, DefaultValue = CopyMode.Append)]
    public CopyMode Mode { get; set; }

    public override async ValueTask<IActivityExecutionResult> ExecuteAsync(ActivityExecutionContext context)
    {
        using (StreamWriter file = new(FilePath, append: Mode == CopyMode.Append))
        {
            await file.WriteLineAsync(Text);
        };

        return Done();
    }

}