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