From fcfdad78664fc2444ec53a5e4f1dae35d3fdb92b Mon Sep 17 00:00:00 2001 From: calebtt Date: Mon, 4 Apr 2022 14:21:29 -0500 Subject: [PATCH] Added error logging method, useful for re-routing error logging if desired. Modified LINQ Where clause to use Count instead. --- ZipperLibrary/Zipper.cs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/ZipperLibrary/Zipper.cs b/ZipperLibrary/Zipper.cs index aa74e3d..ea9141c 100644 --- a/ZipperLibrary/Zipper.cs +++ b/ZipperLibrary/Zipper.cs @@ -1,5 +1,5 @@ using System.IO.Compression; - +using System.Reflection; using Microsoft.Extensions.Configuration; namespace ZipperLibrary; @@ -13,7 +13,7 @@ public Zipper(IConfiguration config) IConfigurationSection? exclusionSection = config.GetSection("Exclusions"); string[]? excludedPathsAndExtensions = exclusionSection.Get(); - foreach ( string excludedPathOrExtension in excludedPathsAndExtensions ) + foreach (string excludedPathOrExtension in excludedPathsAndExtensions) { exclusions.Add(excludedPathOrExtension); } @@ -37,17 +37,32 @@ public void Zip(string folder, string zipFile) .GetFiles("*", SearchOption.AllDirectories) .Where(x => (x.Attributes & FileAttributes.Hidden) == 0 && x.FullName != zipFile); - foreach ( var f in files ) + foreach (var f in files) { - if ( exclusions.Where(x => f.FullName.Contains(x)).Count() == 0 ) + if (exclusions.Count(x => f.FullName.Contains(x)) == 0) { archive.CreateEntryFromFile(f.FullName, f.FullName.Substring(folder.Length)); } } } - catch ( Exception ex ) + catch (Exception ex) + { + LogError(ex.Message, this); + } + } + + /// A singular function for error messages, can be re-routed from here, logged to a file, etc. + /// The error message. + /// The handle to the object making the error logging call. + private static void LogError(string message, Object? originator = null) + { + string callerName = ""; + if (originator != null) { - Console.WriteLine(ex.Message); + var t = originator.GetType(); + if (t.FullName != null) + callerName = "Error in: " + t.FullName + Environment.NewLine; } + Console.WriteLine(callerName + message); } }