Skip to content

Commit 90c0cfe

Browse files
authored
Merge pull request #14 from TechieGuy12/cleanup
Cleanup
2 parents 42ef558 + fdc398e commit 90c0cfe

File tree

14 files changed

+155
-77
lines changed

14 files changed

+155
-77
lines changed

FileWatcher/Configuration/Actions/Action.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ namespace TE.FileWatcher.Configuration.Actions
1414
/// </summary>
1515
public class Action : RunnableBase
1616
{
17-
// The placeholders for the destination path
18-
//private Dictionary<string, string> _destinationPlaceholders
19-
// = new Dictionary<string, string>();
20-
21-
// The placeholders for the source path
22-
//private Dictionary<string, string> _sourcePlaceholders
23-
// = new Dictionary<string, string>();
24-
2517
/// <summary>
2618
/// The type of action to perform.
2719
/// </summary>
@@ -118,7 +110,9 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
118110
{
119111
if (string.IsNullOrWhiteSpace(destination))
120112
{
121-
Logger.WriteLine($"The file '{source}' could not be copied because the destination file was not specified.");
113+
Logger.WriteLine(
114+
$"The file '{source}' could not be copied because the destination file was not specified.",
115+
LogLevel.ERROR);
122116
return;
123117
}
124118

@@ -127,7 +121,9 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
127121
}
128122
else
129123
{
130-
Logger.WriteLine($"The file '{source}' could not be copied because the path was not valid, the file doesn't exists, or it was in use.");
124+
Logger.WriteLine(
125+
$"The file '{source}' could not be copied because the path was not valid, the file doesn't exists, or it was in use.",
126+
LogLevel.ERROR);
131127
}
132128
break;
133129
case ActionType.Move:
@@ -144,7 +140,9 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
144140
}
145141
else
146142
{
147-
Logger.WriteLine($"The file '{source}' could not be moved because the path was not valid, the file doesn't exists, or it was in use.");
143+
Logger.WriteLine(
144+
$"The file '{source}' could not be moved because the path was not valid, the file doesn't exists, or it was in use.",
145+
LogLevel.ERROR);
148146
}
149147
break;
150148
case ActionType.Delete:
@@ -155,15 +153,17 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
155153
}
156154
else
157155
{
158-
Logger.WriteLine($"The file '{source}' could not be deleted because the path was not valid, the file doesn't exists, or it was in use.");
156+
Logger.WriteLine(
157+
$"The file '{source}' could not be deleted because the path was not valid, the file doesn't exists, or it was in use.",
158+
LogLevel.ERROR);
159159
}
160160
break;
161161
}
162162
}
163163
catch (Exception ex)
164164
{
165165
string message = (ex.InnerException == null) ? ex.Message : ex.InnerException.Message;
166-
Logger.WriteLine($"Could not {Type.ToString().ToLower()} file {source}. Reason: {message}");
166+
Logger.WriteLine($"Could not {Type.ToString().ToLower()} file {source}. Reason: {message}", LogLevel.ERROR);
167167
return;
168168
}
169169
}

FileWatcher/Configuration/Commands/Command.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,16 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
7777

7878
if (string.IsNullOrWhiteSpace(commandPath))
7979
{
80-
Logger.WriteLine($"The command was not provided. Command was not run.");
80+
Logger.WriteLine($"The command was not provided. Command was not run.",
81+
LogLevel.ERROR);
8182
return;
8283
}
8384

8485
if (!File.Exists(commandPath))
8586
{
86-
Logger.WriteLine($"The command '{commandPath}' was not found. Command was not run.");
87+
Logger.WriteLine(
88+
$"The command '{commandPath}' was not found. Command was not run.",
89+
LogLevel.ERROR);
8790
return;
8891
}
8992

@@ -111,7 +114,9 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
111114
}
112115
catch (Exception ex)
113116
{
114-
Logger.WriteLine($"Could not run the command '{commandPath} {arguments}'. Reason: {ex.Message}");
117+
Logger.WriteLine(
118+
$"Could not run the command '{commandPath} {arguments}'. Reason: {ex.Message}",
119+
LogLevel.ERROR);
115120
}
116121
}
117122

@@ -148,7 +153,9 @@ private void Execute()
148153
}
149154
else
150155
{
151-
Logger.WriteLine($"The command '{startInfo.FileName}' was not found. Command was not run.");
156+
Logger.WriteLine(
157+
$"The command '{startInfo.FileName}' was not found. Command was not run.",
158+
LogLevel.ERROR);
152159

153160
// Execute the next process in the queue
154161
Execute();
@@ -159,11 +166,15 @@ private void Execute()
159166
{
160167
if (_process != null)
161168
{
162-
Logger.WriteLine($"Could not run the command '{_process.StartInfo.FileName} {_process.StartInfo.Arguments}'. Reason: {ex.Message}");
169+
Logger.WriteLine(
170+
$"Could not run the command '{_process.StartInfo.FileName} {_process.StartInfo.Arguments}'. Reason: {ex.Message}",
171+
LogLevel.ERROR);
163172
}
164173
else
165174
{
166-
Logger.WriteLine($"Could not run the command. Reason: {ex.Message}");
175+
Logger.WriteLine(
176+
$"Could not run the command. Reason: {ex.Message}",
177+
LogLevel.ERROR);
167178
}
168179
}
169180
}

FileWatcher/Configuration/Logging.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,25 @@ namespace TE.FileWatcher.Configuration
1515
/// </summary>
1616
public class Logging
1717
{
18+
/// <summary>
19+
/// The default log size.
20+
/// </summary>
21+
public const int DEFAULT_LOG_SIZE = 5;
22+
23+
/// <summary>
24+
/// The default log number.
25+
/// </summary>
26+
public const int DEFAULT_LOG_NUMBER = 10;
27+
1828
// The log path
1929
private string? _logPath;
2030

31+
// The size of the log file
32+
private int _logSize = DEFAULT_LOG_SIZE;
33+
34+
// The number of log files to retain
35+
private int _logNumber = DEFAULT_LOG_NUMBER;
36+
2137
/// <summary>
2238
/// Gets or sets the path of the log file.
2339
/// </summary>
@@ -39,13 +55,33 @@ public string? LogPath
3955
/// backed up and a new log file is created.
4056
/// </summary>
4157
[XmlElement("size")]
42-
public int Size { get; set; }
58+
public int Size
59+
{
60+
get
61+
{
62+
return _logSize;
63+
}
64+
set
65+
{
66+
_logSize = value > 0 ? value : DEFAULT_LOG_SIZE;
67+
}
68+
}
4369

4470
/// <summary>
4571
/// Gets or sets the number of log file to retain.
4672
/// </summary>
4773
[XmlElement("number")]
48-
public int Number { get; set; }
74+
public int Number
75+
{
76+
get
77+
{
78+
return _logNumber;
79+
}
80+
set
81+
{
82+
_logNumber = value > 0 ? value : DEFAULT_LOG_NUMBER;
83+
}
84+
}
4985

5086
/// <summary>
5187
/// Initializes an instance of the <see cref="Logging"/> class.
@@ -62,8 +98,8 @@ public string? LogPath
6298
public Logging()
6399
{
64100
LogPath = Path.Combine(Path.GetTempPath(), Logger.DEFAULT_LOG_NAME);
65-
Size = 5;
66-
Number = 10;
101+
Size = DEFAULT_LOG_SIZE;
102+
Number = DEFAULT_LOG_NUMBER;
67103
}
68104
}
69105
}

FileWatcher/Configuration/Notifications/Notifications.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,17 @@ private async void OnElapsed(object? source, ElapsedEventArgs e)
9999
foreach (Exception ex in aex.Flatten().InnerExceptions)
100100
{
101101
Logger.WriteLine(ex.Message, LogLevel.ERROR);
102-
Logger.WriteLine($"StackTrace:{Environment.NewLine}{ex.StackTrace}");
102+
Logger.WriteLine(
103+
$"StackTrace:{Environment.NewLine}{ex.StackTrace}",
104+
LogLevel.ERROR);
103105
}
104106
}
105107
catch (NullReferenceException ex)
106108
{
107109
Logger.WriteLine(ex.Message, LogLevel.ERROR);
108-
Logger.WriteLine($"StackTrace:{Environment.NewLine}{ex.StackTrace}");
110+
Logger.WriteLine(
111+
$"StackTrace:{Environment.NewLine}{ex.StackTrace}",
112+
LogLevel.ERROR);
109113
}
110114
}
111115
}

FileWatcher/Configuration/RunnableBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private static string GetRelativeFullPath(string watchPath, string fullPath)
130130
/// </returns>
131131
private static string? GetRelativePath(string watchPath, string fullPath)
132132
{
133-
string? relativeFullPath = IO.Path.GetDirectoryName(fullPath);
133+
string? relativeFullPath = Path.GetDirectoryName(fullPath);
134134
if (relativeFullPath == null)
135135
{
136136
return null;
@@ -150,12 +150,12 @@ private static string GetRelativeFullPath(string watchPath, string fullPath)
150150
/// </returns>
151151
private static string? GetFilename(string fullPath, bool includeExtension)
152152
{
153-
if (string.IsNullOrEmpty(fullPath) || !IO.File.Exists(fullPath))
153+
if (string.IsNullOrEmpty(fullPath) || !File.Exists(fullPath))
154154
{
155155
return null;
156156
}
157157

158-
return includeExtension ? IO.Path.GetFileNameWithoutExtension(fullPath) : IO.Path.GetFileName(fullPath);
158+
return includeExtension ? Path.GetFileNameWithoutExtension(fullPath) : Path.GetFileName(fullPath);
159159
}
160160

161161
/// <summary>
@@ -169,12 +169,12 @@ private static string GetRelativeFullPath(string watchPath, string fullPath)
169169
/// </returns>
170170
private static string? GetFileExtension(string fullPath)
171171
{
172-
if (string.IsNullOrEmpty(fullPath) || !IO.File.Exists(fullPath))
172+
if (string.IsNullOrEmpty(fullPath) || !File.Exists(fullPath))
173173
{
174174
return null;
175175
}
176176

177-
return IO.Path.GetExtension(fullPath);
177+
return Path.GetExtension(fullPath);
178178
}
179179
}
180180
}

FileWatcher/Configuration/Watch.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ private void CreateFileSystemWatcher()
173173
{
174174
if (string.IsNullOrWhiteSpace(Path))
175175
{
176-
Logger.WriteLine("The path to watch was not specified.");
176+
Logger.WriteLine(
177+
"The path to watch was not specified.",
178+
LogLevel.ERROR);
177179
return;
178180
}
179181

@@ -461,7 +463,7 @@ private static void NotAccessibleError(FileSystemWatcher source, ErrorEventArgs
461463
catch
462464
{
463465
source.EnableRaisingEvents = false;
464-
System.Threading.Thread.Sleep(iTimeOut);
466+
Thread.Sleep(iTimeOut);
465467
}
466468
}
467469

FileWatcher/Configuration/Watches.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public class Watches
3131
/// </summary>
3232
public void Start()
3333
{
34-
if (WatchList == null)
34+
if (WatchList == null || WatchList.Count <= 0)
3535
{
36+
Logger.WriteLine("No watches were specified.", LogLevel.ERROR);
3637
return;
3738
}
3839

FileWatcher/Configuration/XmlFile.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace TE.FileWatcher.Configuration
1313
{
14+
/// <summary>
15+
/// The XML configuration file.
16+
/// </summary>
1417
public class XmlFile : IConfigurationFile
1518
{
1619
// The default configuration file name
@@ -47,7 +50,7 @@ public XmlFile(string path, string name)
4750
/// The folder path.
4851
/// </param>
4952
/// <returns>
50-
/// The folder path of the files, otherwise null.
53+
/// The folder path of the files, otherwise <c>null</c>.
5154
/// </returns>
5255
private static string? GetFolderPath(string? path)
5356
{
@@ -85,7 +88,7 @@ public XmlFile(string path, string name)
8588
/// The name of the configuration file.
8689
/// </param>
8790
/// <returns>
88-
/// The full path to the configuration file, otherwise null.
91+
/// The full path to the configuration file, otherwise <c>null</c>.
8992
/// </returns>
9093
private static string? GetFullPath(string path, string name)
9194
{
@@ -126,7 +129,7 @@ public XmlFile(string path, string name)
126129
/// </summary>
127130
/// <returns>
128131
/// A <see cref="Watches"/> object if the file was read successfully,
129-
/// otherwise null.
132+
/// otherwise <c>null</c>.
130133
/// </returns>
131134
[RequiresUnreferencedCode("Calls XmlSerializer")]
132135
public Watches? Read()

FileWatcher/FileSystem/Directory.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ public static class Directory
1818
/// <param name="path">
1919
/// The path that includes the folder structure to create.
2020
/// </param>
21+
/// <exception cref="ArgumentNullException">
22+
/// Thrown when an argument is null or empty.
23+
/// </exception>
24+
/// <exception cref="FileWatcherException">
25+
/// Thrown when the directory could not be created.
26+
/// </exception>
27+
/// <exception cref="NullReferenceException">
28+
/// Thrown when the directory from the path is null.
29+
/// </exception>
2130
public static void Create(string path)
2231
{
2332
if (string.IsNullOrWhiteSpace(path))
2433
{
25-
return;
34+
throw new ArgumentNullException(nameof(path));
2635
}
2736

2837
string? folders = Path.GetDirectoryName(path);
@@ -34,8 +43,16 @@ public static void Create(string path)
3443
if (!IO.Directory.Exists(folders))
3544
{
3645
IO.Directory.CreateDirectory(folders);
46+
if (!IO.Directory.Exists(folders))
47+
{
48+
throw new FileWatcherException($"The directory {folders} could not be created.");
49+
}
3750
}
3851
}
52+
else
53+
{
54+
throw new NullReferenceException("The directory path could not be determined.");
55+
}
3956
}
4057

4158
/// <summary>

FileWatcher/FileSystem/File.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static void Copy(string source, string destination, bool verify)
182182
/// <returns>
183183
/// <c>true</c> if the file is valid, otherwise <c>false</c>.
184184
/// </returns>
185-
public static bool IsValid(string? path)
185+
public static bool IsValid(string path)
186186
{
187187
if (string.IsNullOrWhiteSpace(path))
188188
{

0 commit comments

Comments
 (0)