Saturday, November 23, 2019

Logging Exceptions to Fileshare C# .NET

Logging exceptions to Fileshare using C#.NET. Sample code.

try
{
 string filePath = @"\\sharepointFix\Error.txt";
 .............
.............
}
catch
{
                using (StreamWriter writer = new StreamWriter(filePath, true))
              {
                    writer.WriteLine("Message: " + exp.Message + Environment.NewLine + "Stack Trace : " + exp.StackTrace
                        + Environment.NewLine + "Inner Exception: " + exp.InnerException
                        + Environment.NewLine + "Date :" + DateTime.Now.ToString());
                }
}

How to load an Assembly (DLL) programatically

Sample code to load Assembly. (DLL) programatically

Assembly assembly = Assembly.LoadFile(Properties.Settings.Default.AssemblyPath);

            Type myClassType =
            (from type in assembly.GetExportedTypes()
             where typeof(InterfaceBulkLoad).IsAssignableFrom(type)
             select type).Single();

            var instance = (InterfaceBulkLoad)Activator.CreateInstance(myClassType);
            if (args != null)
            {
                var isLoaded = instance.LoadFiles(new LoadFileRequest
                {
                    Environment = Properties.Settings.Default.Environment,
                    ProcessName = args[0],
                    ConnectionString = Properties.Settings.Default.ConnectionString
                });

                Console.WriteLine(isLoaded);
            }
            else
            {
                Console.WriteLine("Arguments were not supplied");
            }