Skip to content

Commit c70c8d9

Browse files
authored
Feature: HostsFileEditor Debug Log + Create Entry (#3103)
* Feature: Debug Log + Create Entry * Docs: #3103
1 parent 1483168 commit c70c8d9

File tree

5 files changed

+206
-183
lines changed

5 files changed

+206
-183
lines changed

Source/NETworkManager.Models/HostsFileEditor/HostsFileEditor.cs

Lines changed: 69 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,16 @@ private static IEnumerable<HostsFileEntry> GetHostsFileEntries()
132132

133133
if (result.Success)
134134
{
135-
Log.Debug("GetHostsFileEntries - Line matched: " + line);
135+
Log.Debug($"GetHostsFileEntries - Line matched: {line}");
136136

137137
var entry = new HostsFileEntry
138-
{
139-
IsEnabled = !result.Groups[1].Value.Equals("#"),
140-
IPAddress = result.Groups[2].Value,
141-
Hostname = result.Groups[3].Value.Replace(@"\s", "").Trim(),
142-
Comment = result.Groups[4].Value.TrimStart('#', ' '),
143-
Line = line
144-
};
138+
(
139+
!result.Groups[1].Value.Equals("#"),
140+
result.Groups[2].Value,
141+
result.Groups[3].Value.Replace(@"\s", "").Trim(),
142+
result.Groups[4].Value.TrimStart('#', ' '),
143+
line
144+
);
145145

146146
// Skip example entries
147147
if (!entry.IsEnabled)
@@ -157,7 +157,7 @@ private static IEnumerable<HostsFileEntry> GetHostsFileEntries()
157157
}
158158
else
159159
{
160-
Log.Debug("GetHostsFileEntries - Line not matched: " + line);
160+
Log.Debug($"GetHostsFileEntries - Line not matched: {line}");
161161
}
162162
}
163163

@@ -201,31 +201,34 @@ private static HostsFileEntryModifyResult EnableEntry(HostsFileEntry entry)
201201
return HostsFileEntryModifyResult.ReadError;
202202
}
203203

204-
bool entryFound = false;
204+
var entryFound = false;
205205

206206
for (var i = 0; i < hostsFileLines.Count; i++)
207207
{
208-
if (hostsFileLines[i] == entry.Line)
209-
{
210-
entryFound = true;
208+
if (hostsFileLines[i] != entry.Line)
209+
continue;
211210

212-
hostsFileLines.RemoveAt(i);
213-
hostsFileLines.Insert(i, CreateEntryLine(new HostsFileEntry
214-
{
215-
IsEnabled = true,
216-
IPAddress = entry.IPAddress,
217-
Hostname = entry.Hostname,
218-
Comment = entry.Comment,
219-
Line = entry.Line
220-
}));
221-
222-
break;
223-
}
211+
entryFound = true;
212+
213+
Log.Debug($"EnableEntry - Found entry: {hostsFileLines[i]}");
214+
hostsFileLines.RemoveAt(i);
215+
216+
var newEntry = new HostsFileEntry(
217+
true,
218+
entry.IPAddress,
219+
entry.Hostname,
220+
entry.Comment
221+
);
222+
223+
Log.Debug($"EnableEntry - Enabling entry: {newEntry.Line}");
224+
hostsFileLines.Insert(i, newEntry.Line);
225+
226+
break;
224227
}
225228

226229
if (!entryFound)
227230
{
228-
Log.Warn($"EnableEntry - Entry not found in hosts file: {entry.Line}");
231+
Log.Warn($"EnableEntry - Entry not found in hosts file: {entry}");
229232

230233
return HostsFileEntryModifyResult.NotFound;
231234
}
@@ -281,26 +284,29 @@ private static HostsFileEntryModifyResult DisableEntry(HostsFileEntry entry)
281284
return HostsFileEntryModifyResult.ReadError;
282285
}
283286

284-
bool entryFound = false;
287+
var entryFound = false;
285288

286289
for (var i = 0; i < hostsFileLines.Count; i++)
287290
{
288-
if (hostsFileLines[i] == entry.Line)
289-
{
290-
entryFound = true;
291+
if (hostsFileLines[i] != entry.Line)
292+
continue;
291293

292-
hostsFileLines.RemoveAt(i);
293-
hostsFileLines.Insert(i, CreateEntryLine(new HostsFileEntry
294-
{
295-
IsEnabled = false,
296-
IPAddress = entry.IPAddress,
297-
Hostname = entry.Hostname,
298-
Comment = entry.Comment,
299-
Line = entry.Line
300-
}));
301-
302-
break;
303-
}
294+
entryFound = true;
295+
296+
Log.Debug($"DisableEntry - Found entry: {hostsFileLines[i]}");
297+
hostsFileLines.RemoveAt(i);
298+
299+
var newEntry = new HostsFileEntry(
300+
false,
301+
entry.IPAddress,
302+
entry.Hostname,
303+
entry.Comment
304+
);
305+
306+
Log.Debug($"DisableEntry - Disabling entry: {newEntry.Line}");
307+
hostsFileLines.Insert(i, newEntry.Line);
308+
309+
break;
304310
}
305311

306312
if (!entryFound)
@@ -360,7 +366,8 @@ private static HostsFileEntryModifyResult AddEntry(HostsFileEntry entry)
360366
return HostsFileEntryModifyResult.ReadError;
361367
}
362368

363-
hostsFileLines.Add(CreateEntryLine(entry));
369+
Log.Debug($"AddEntry - Adding entry: {entry.Line}");
370+
hostsFileLines.Add(entry.Line);
364371

365372
try
366373
{
@@ -415,19 +422,22 @@ private static HostsFileEntryModifyResult EditEntry(HostsFileEntry entry, HostsF
415422
return HostsFileEntryModifyResult.ReadError;
416423
}
417424

418-
bool entryFound = false;
425+
var entryFound = false;
419426

420427
for (var i = 0; i < hostsFileLines.Count; i++)
421428
{
422-
if (hostsFileLines[i] == entry.Line)
423-
{
424-
entryFound = true;
429+
if (hostsFileLines[i] != entry.Line)
430+
continue;
425431

426-
hostsFileLines.RemoveAt(i);
427-
hostsFileLines.Insert(i, CreateEntryLine(newEntry));
432+
entryFound = true;
428433

429-
break;
430-
}
434+
Log.Debug($"EditEntry - Found entry: {hostsFileLines[i]}");
435+
hostsFileLines.RemoveAt(i);
436+
437+
Log.Debug($"EditEntry - Editing entry: {newEntry.Line}");
438+
hostsFileLines.Insert(i, newEntry.Line);
439+
440+
break;
431441
}
432442

433443
if (!entryFound)
@@ -487,18 +497,19 @@ private static HostsFileEntryModifyResult DeleteEntry(HostsFileEntry entry)
487497
return HostsFileEntryModifyResult.ReadError;
488498
}
489499

490-
bool entryFound = false;
500+
var entryFound = false;
491501

492502
for (var i = 0; i < hostsFileLines.Count; i++)
493503
{
494-
if (hostsFileLines[i] == entry.Line)
495-
{
496-
entryFound = true;
504+
if (hostsFileLines[i] != entry.Line)
505+
continue;
497506

498-
hostsFileLines.RemoveAt(i);
507+
entryFound = true;
499508

500-
break;
501-
}
509+
Log.Debug($"DeleteEntry - Found entry: {hostsFileLines[i]}");
510+
hostsFileLines.RemoveAt(i);
511+
512+
break;
502513
}
503514

504515
if (!entryFound)
@@ -521,25 +532,6 @@ private static HostsFileEntryModifyResult DeleteEntry(HostsFileEntry entry)
521532
return HostsFileEntryModifyResult.Success;
522533
}
523534

524-
/// <summary>
525-
/// Create a line for the hosts file entry.
526-
/// </summary>
527-
/// <param name="entry">Entry to create the line for.</param>
528-
/// <returns>Line for the hosts file entry.</returns>
529-
private static string CreateEntryLine(HostsFileEntry entry)
530-
{
531-
var line = entry.IsEnabled ? "" : "# ";
532-
533-
line += $"{entry.IPAddress} {entry.Hostname}";
534-
535-
if (!string.IsNullOrWhiteSpace(entry.Comment))
536-
{
537-
line += $" # {entry.Comment}";
538-
}
539-
540-
return line.Trim();
541-
}
542-
543535
/// <summary>
544536
/// Create a "daily" backup of the hosts file (before making a change).
545537
/// </summary>

Source/NETworkManager.Models/HostsFileEditor/HostsFileEntry.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public HostsFileEntry(bool isEnabled, string ipAddress, string hostname, string
5252
IPAddress = ipAddress;
5353
Hostname = hostname;
5454
Comment = comment;
55+
56+
var line = isEnabled ? "" : "# ";
57+
58+
line += $"{ipAddress} {hostname}";
59+
60+
if (!string.IsNullOrWhiteSpace(comment))
61+
line += $" # {comment}";
62+
63+
Line = line;
5564
}
5665

5766
/// <summary>
@@ -62,8 +71,21 @@ public HostsFileEntry(bool isEnabled, string ipAddress, string hostname, string
6271
/// <param name="hostname">Host name(s) of the host.</param>
6372
/// <param name="comment">Comment of the host.</param>
6473
/// <param name="line">Line of the entry in the hosts file.</param>
65-
public HostsFileEntry(bool isEnabled, string ipAddress, string hostname, string comment, string line) : this(isEnabled, ipAddress, hostname, comment)
74+
public HostsFileEntry(bool isEnabled, string ipAddress, string hostname, string comment, string line)
6675
{
76+
IsEnabled = isEnabled;
77+
IPAddress = ipAddress;
78+
Hostname = hostname;
79+
Comment = comment;
6780
Line = line;
6881
}
82+
83+
/// <summary>
84+
/// Overrides the ToString method to return the line of the entry in the hosts file.
85+
/// </summary>
86+
/// <returns>Line of the entry in the hosts file.</returns>
87+
public override string ToString()
88+
{
89+
return Line;
90+
}
6991
}

0 commit comments

Comments
 (0)