**Questions:-**1)I am trying to handle all exception at once in a single text file and i made a custom activity but error occurs that expression does not produce value but the same code is working perfectly in visual studio.
2) i have also tried using streamwriter classes to make a single exception and the code i used is below:
In the assign activity- streamwriter sw=new streamwriter(“path”,true)
then in message box- sw.writeline(ex.tostring) and here it says expression does not produce value
and i have tried multiple ways of using streamwriter but it seems doesn’t working here i don’t know why but the same thing working perfectly in visual studio.
**3)**File.AppendAllText also not working.
So i think uipath system.IO library is not working properly so please update it or if i made any mistake then please give me the solution.
ErrorHandling.nupkg (5.4 KB)
Hi Andrzej, here i attached the nuget package i made can you pls elaborate your solution how to use it using invoke method…
StreamWriter.WriteLine(value) is a void method. Meaning it doesn’t return a value, hence the error is correct. Same for File.AppendAllText.
MessageBox and WriteLine activity require a string InArgument. Since expressions you’re putting in them do not produce it, it won’t work.
There are 2 solutions:
Create an actual activity for those (it looks like you made just classes, not activities) and use those (preferred).
Reason that it’s working in VS is that you’re (most probably) not running this as a workflow, but normal code. Workflows work on contexts, which requires a specific way of handling (hence activities with [In|Out|InOut]Arguments etc.).
Thanks for ur solution andrzej…but i still stucked on my problem,So could you please help me to make a custom activity for my code i attached below.This is the code for which i made the nuget package and trying to use to handle all error at once in a text file.
could You please help me for the accurate solution?If possible please review and update the code.
Ok, a couple of remarks - don’t take it personally:
Don’t post code as html. Use code tags for it (simplest way - put three ` at the beginning and at end:
Like this
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Activities.Presentation.Metadata;
namespace errorcapturing
{
public class ErrorHandle
{
private static String ErrorlineNo, Errormsg, extype, ErrorLocation;
public static void SendErrorToText(Exception ex)
{
var line = Environment.NewLine + Environment.NewLine;
ErrorlineNo = ex.StackTrace.Substring(ex.StackTrace.Length - 7, 7);
Errormsg = ex.GetType().Name.ToString();
extype = ex.GetType().ToString();
ErrorLocation = ex.Message.ToString();
try
{
string filepath = "c:\\LogError\\";
//Text File Path
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
filepath = filepath + "ProgramLog" + " " + DateTime.Today.ToString("yyyy-MM-dd") + "." + "txt";
//Text File Name
if (!File.Exists(filepath))
{
File.Create(filepath).Dispose();
}
using (StreamWriter sw = File.AppendText(filepath))
{
string error = "Log Written Date:" + " " + DateTime.Now.ToString() + line + "Error Line No :" + " " + ErrorlineNo + line + "Error Message:" + " " + Errormsg + line + "Exception Type:" + " " + extype + line + "Error Location :" + " " + ErrorLocation + line;
sw.WriteLine("-----------Exception Details on " + " " + DateTime.Now.ToString() + "-----------------");
sw.WriteLine("-------------------------------------------------------------------------------------");
sw.WriteLine(line);
sw.WriteLine(error);
sw.WriteLine("--------------------------------*End*------------------------------------------");
sw.WriteLine(line);
sw.Flush();
sw.Close();
}
}
catch (Exception e) { e.ToString(); }
}
}
// Declare dummy register metadata class to force UiPath to load this assembly
public class RegisterMetadata : IRegisterMetadata { public void Register() { } }
}
Sidenote: that catch body does nothing useful
You might be better off just reconfiguring NLog - see here for example. That way you can have errors in separate file.
An activity for this kind of custom logger might look similar to this:
CodeActivity
public sealed class LogExceptionToFile : CodeActivity
{
[RequiredArgument]
public InArgument<Exception> ExceptionToLog { get; set; }
[DefaultValue(@"c:\LogError")]
public InArgument<String> LogFolderPath { get; set; }
protected override void Execute(CodeActivityContext context)
{
var ex = ExceptionToLog.Get(context);
var sb = new StringBuilder();
sb.AppendFormat("Log Written Date: {0}", DateTime.Now.ToString());
sb.AppendLine();
sb.AppendFormat("Error Line No: {0}", ex.StackTrace.Substring(ex.StackTrace.Length - 7, 7));
sb.AppendLine();
// add more stuff here
string filepath = Path.Combine(LogFolderPath.Get(context), "ProgramLog_" + DateTime.Today.ToString("yyyy-MM-dd") + "." + "txt");
File.AppendAllText(filepath, sb.ToString());
}
}
Admittedly it’s not complete and you’ll still need to add what you really want to do there (I just did a barebones convertion from what you had in your code), but it should get you on the track.
Good luck and let us know if you’ll get stuck on something