Skip to content

Commit 1efd3ab

Browse files
authored
Switched logging to serilog. Now actually works. (#63)
1 parent 8071099 commit 1efd3ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+243
-227
lines changed

AttackSurfaceAnalyzer.sln

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
ProjectSection(SolutionItems) = preProject
1313
BUILD.md = BUILD.md
1414
CONTRIBUTING.md = CONTRIBUTING.md
15+
filters.json = filters.json
1516
global.json = global.json
17+
LICENSE = LICENSE
1618
PRIVACY.md = PRIVACY.md
1719
README.md = README.md
1820
version.json = version.json
19-
filters.json = filters.json
20-
LICENSE.txt = LICENSE.txt
21-
nlog.config = nlog.config
22-
NOTICE.txt = NOTICE.txt
2321
EndProjectSection
2422
EndProject
2523
Global

Cli/AttackSurfaceAnalyzerCli.csproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717

1818
<ItemGroup>
1919
<PackageReference Include="CommandLineParser" Version="2.4.3" />
20+
<PackageReference Include="Microsoft.ApplicationInsights.NLogTarget" Version="2.9.1" />
2021
<PackageReference Include="RazorLight" Version="2.0.0-beta1" />
2122
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.3" />
23+
<PackageReference Include="Serilog" Version="2.8.0" />
2224
<PackageReference Include="System.Management" Version="4.6.0-preview.19073.11" />
2325
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.0" />
2426
<PackageReference Include="Nerdbank.GitVersioning" Version="2.3.138" />
25-
<PackageReference Include="NLog" Version="4.6.1" />
27+
<PackageReference Include="NLog" Version="4.6.2" />
2628
</ItemGroup>
2729
<ItemGroup>
2830
<ProjectReference Include="..\Lib\AttackSurfaceAnalyzerLib.csproj" />
29-
<Content Include="Output\Output.cshtml" CopyToPublishDirectory="PreserveNewest" />
31+
<Content Include="Output\Output.cshtml" CopyToPublishDirectory="PreserveNewest">
32+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
33+
</Content>
3034
<Content Include="..\LICENSE.txt">
3135
<Link>LICENSE.txt</Link>
3236
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

Cli/Program.cs

+56-58
Large diffs are not rendered by default.

Gui/AttackSurfaceAnalyzerGui.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<PackageReference Include="Diff.Match.Patch" Version="2.1.0" />
2323
<PackageReference Include="BundlerMinifier.Core" Version="2.9.406" />
2424
<PackageReference Include="Microsoft.AspNetCore.App" />
25+
<PackageReference Include="Serilog" Version="2.8.0" />
2526
</ItemGroup>
2627
<ItemGroup>
2728
<ProjectReference Include="..\Lib\AttackSurfaceAnalyzerLib.csproj" />

Gui/Controllers/HomeController.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.ApplicationInsights.Extensibility;
1818
using System.Runtime.InteropServices;
1919
using Microsoft.ApplicationInsights;
20+
using Serilog;
2021

2122
namespace AttackSurfaceAnalyzer.Gui.Controllers
2223
{
@@ -421,8 +422,8 @@ public ActionResult StartMonitoring(string RunId, string Directory, string Exten
421422
}
422423
catch (Exception e)
423424
{
424-
Logger.Instance.Warn(e.StackTrace);
425-
Logger.Instance.Warn(e.Message);
425+
Log.Warning(e.StackTrace);
426+
Log.Warning(e.Message);
426427
return Json((int)ERRORS.UNIQUE_ID);
427428
}
428429
}

Lib/AttackSurfaceAnalyzerLib.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ This NuGet contains the AttackSurfaceAnalyzer Library, which is used by the CLI
4141
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.2.0" />
4242
<PackageReference Include="Microsoft.Win32.Registry" Version="4.6.0-preview.19073.11" />
4343
<PackageReference Include="murmurhash" Version="1.0.3" />
44-
<PackageReference Include="NLog.Extensions.Logging" Version="1.4.0" />
44+
<PackageReference Include="Serilog" Version="2.8.0" />
45+
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
46+
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
4547
<PackageReference Include="System.Data.HashFunction.xxHash" Version="2.0.0" />
4648
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
4749
<PackageReference Include="System.Management" Version="4.6.0-preview.19073.11" />
@@ -52,7 +54,6 @@ This NuGet contains the AttackSurfaceAnalyzer Library, which is used by the CLI
5254
<PackageReference Include="System.Security.AccessControl" Version="4.6.0-preview.19073.11" />
5355
<PackageReference Include="System.Threading.AccessControl" Version="4.6.0-preview.19073.11" />
5456
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
55-
<PackageReference Include="NLog" Version="4.6.1" />
5657
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.3" />
5758
<PackageReference Include="NuGet.Build.Packaging" Version="0.2.2">
5859
<PrivateAssets>all</PrivateAssets>

Lib/Collectors/BaseCollector.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33
using AttackSurfaceAnalyzer.ObjectTypes;
4+
using Serilog;
45

56
namespace AttackSurfaceAnalyzer.Collectors
67
{

Lib/Collectors/BaseCompare.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using AttackSurfaceAnalyzer.ObjectTypes;
66
using AttackSurfaceAnalyzer.Utils;
77
using Microsoft.Data.Sqlite;
8+
using Serilog;
89

910
namespace AttackSurfaceAnalyzer.Collectors
1011
{
@@ -37,8 +38,8 @@ public bool TryCompare(string firstRunId, string secondRunId)
3738
}
3839
catch(Exception ex)
3940
{
40-
Logger.Instance.Warn(ex, "Exception from Compare(): {0}", ex.StackTrace);
41-
Logger.Instance.Warn(ex.Message);
41+
Log.Warning(ex, "Exception from Compare(): {0}", ex.StackTrace);
42+
Log.Warning(ex.Message);
4243
Stop();
4344
return false;
4445
}

Lib/Collectors/BaseMonitor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33
using AttackSurfaceAnalyzer.ObjectTypes;
4+
using Serilog;
45

56
namespace AttackSurfaceAnalyzer.Collectors
67
{

Lib/Collectors/Certificates/CertificateCollector.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Security.Cryptography.X509Certificates;
1212
using Newtonsoft.Json;
1313
using Newtonsoft.Json.Serialization;
14+
using Serilog;
1415

1516
namespace AttackSurfaceAnalyzer.Collectors.Certificates
1617
{
@@ -70,11 +71,11 @@ public void Write(StoreLocation storeLocation, StoreName storeName, X509Certific
7071
}
7172
catch (NullReferenceException e)
7273
{
73-
Logger.Instance.Warn(e.StackTrace);
74+
Log.Warning(e.StackTrace);
7475
}
7576
catch (Microsoft.Data.Sqlite.SqliteException e)
7677
{
77-
Logger.Instance.Warn(e.Message);
78+
Log.Warning(e.Message);
7879
//This catches duplicate certificates
7980
}
8081
}
@@ -106,9 +107,9 @@ public override void Execute()
106107
}
107108
catch (Exception e)
108109
{
109-
Logger.Instance.Debug(e.StackTrace);
110-
Logger.Instance.Debug(e.GetType());
111-
Logger.Instance.Debug(e.Message);
110+
Log.Debug(e.StackTrace);
111+
Log.Debug(e.GetType().ToString());
112+
Log.Debug(e.Message);
112113
}
113114
}
114115
}

Lib/Collectors/Certificates/CertificateCompare.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using AttackSurfaceAnalyzer.Utils;
66
using Microsoft.Data.Sqlite;
77
using AttackSurfaceAnalyzer.ObjectTypes;
8+
using Serilog;
89

910
namespace AttackSurfaceAnalyzer.Collectors.Certificates
1011
{
@@ -98,8 +99,8 @@ public override void Compare(string firstRunId, string secondRunId)
9899
}
99100
catch (Exception e)
100101
{
101-
Logger.Instance.Debug(e.StackTrace);
102-
Logger.Instance.Debug(e.Message);
102+
Log.Debug(e.StackTrace);
103+
Log.Debug(e.Message);
103104
}
104105
}
105106
}

Lib/Collectors/FileSystem/FileSystemCollector.cs

+17-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using AttackSurfaceAnalyzer.Utils;
1313
using Microsoft.Data.Sqlite;
1414
using Newtonsoft.Json;
15+
using Serilog;
1516

1617
namespace AttackSurfaceAnalyzer.Collectors.FileSystem
1718
{
@@ -51,7 +52,7 @@ public void WriteUntilEmpty()
5152
CommitTimer.Enabled = false;
5253
while (_queue.Count > 0)
5354
{
54-
Logger.Instance.Warn(_queue.Count);
55+
Log.Warning(_queue.Count.ToString());
5556
FileSystemObject fso = _queue.Dequeue();
5657
Write(cmd, fso);
5758
}
@@ -75,9 +76,9 @@ public void Write(SqliteCommand cmd, FileSystemObject obj)
7576
}
7677
catch (Exception e)
7778
{
78-
Logger.Instance.Info(e.StackTrace);
79-
Logger.Instance.Info(e.Message);
80-
Logger.Instance.Info(e.GetType());
79+
Log.Information(e.StackTrace);
80+
Log.Information(e.Message);
81+
Log.Information(e.GetType().ToString());
8182
}
8283
}
8384

@@ -120,9 +121,9 @@ public void Write(FileSystemObject obj)
120121
}
121122
catch (Exception e)
122123
{
123-
Logger.Instance.Info(e.StackTrace);
124-
Logger.Instance.Info(e.Message);
125-
Logger.Instance.Info(e.GetType());
124+
Log.Information(e.StackTrace);
125+
Log.Information(e.Message);
126+
Log.Information(e.GetType().ToString());
126127
}
127128
}
128129

@@ -172,9 +173,9 @@ public override bool CanRunOnPlatform()
172173
// }
173174
// catch (Exception e)
174175
// {
175-
// Logger.Instance.Info(e.StackTrace);
176-
// Logger.Instance.Info(e.Message);
177-
// Logger.Instance.Info(e.GetType());
176+
// Log.Information(e.StackTrace);
177+
// Log.Information(e.Message);
178+
// Log.Information(e.GetType());
178179
// }
179180
//}
180181

@@ -216,7 +217,7 @@ public override void Execute()
216217

217218
foreach (var root in this.roots)
218219
{
219-
Logger.Instance.Warn("Scanning root " + root.ToString());
220+
Log.Warning("Scanning root " + root.ToString());
220221
try
221222
{
222223
var fileInfoEnumerable = DirectoryWalker.WalkDirectory(root);
@@ -251,13 +252,13 @@ public override void Execute()
251252
}
252253
catch (Exception ex)
253254
{
254-
Logger.Instance.Debug(ex, "Error processing {0}", fileInfo?.FullName);
255+
Log.Debug(ex, "Error processing {0}", fileInfo?.FullName);
255256
}
256257
}));
257258
}
258259
catch (Exception ex)
259260
{
260-
Logger.Instance.Debug(ex, "Error collecting file system information: {0}", ex.Message);
261+
Log.Debug(ex, "Error collecting file system information: {0}", ex.Message);
261262
}
262263
}
263264

@@ -270,8 +271,8 @@ public override void Execute()
270271
t.Minutes,
271272
t.Seconds,
272273
t.Milliseconds);
273-
Logger.Instance.Info("Completed FileSystemCollector in " + answer);
274-
Logger.Instance.Info("Flushing data");
274+
Log.Information("Completed FileSystemCollector in " + answer);
275+
Log.Information("Flushing data");
275276
watch = System.Diagnostics.Stopwatch.StartNew();
276277

277278
DatabaseManager.Commit();
@@ -283,7 +284,7 @@ public override void Execute()
283284
t.Minutes,
284285
t.Seconds,
285286
t.Milliseconds);
286-
Logger.Instance.Info("Flush completed in " + answer);
287+
Log.Information("Flush completed in " + answer);
287288
}
288289
}
289290
}

Lib/Collectors/FileSystem/FileSystemCompare.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AttackSurfaceAnalyzer.Utils;
77
using Microsoft.Data.Sqlite;
88
using Newtonsoft.Json;
9+
using Serilog;
910

1011
namespace AttackSurfaceAnalyzer.Collectors.FileSystem
1112
{
@@ -123,7 +124,7 @@ public override void Compare(string firstRunId, string secondRunId)
123124
catch (Exception e)
124125
{
125126
// Debugging
126-
Logger.Instance.Info(e.Message);
127+
Log.Information(e.Message);
127128
}
128129
}
129130
}

Lib/Collectors/FileSystem/FileSystemMonitor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using AttackSurfaceAnalyzer.Utils;
1010
using Microsoft.Data.Sqlite;
1111
using Newtonsoft.Json;
12+
using Serilog;
1213

1314
namespace AttackSurfaceAnalyzer.Collectors.FileSystem
1415
{

Lib/Collectors/FileSystem/FileSystemUtils.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Security.Cryptography.X509Certificates;
99
using AttackSurfaceAnalyzer.Utils;
1010
using Murmur;
11+
using Serilog;
1112

1213
namespace AttackSurfaceAnalyzer.Collectors.FileSystem
1314
{
@@ -31,7 +32,7 @@ protected internal static string GetFilePermissions(FileSystemInfo fileInfo)
3132

3233
protected internal static string GetFileHash(FileSystemInfo fileInfo)
3334
{
34-
Logger.Instance.Debug("Generating file hash for {0}", fileInfo.FullName);
35+
Log.Debug("Generating file hash for {0}", fileInfo.FullName);
3536

3637
string hashValue = null;
3738
try
@@ -43,7 +44,7 @@ protected internal static string GetFileHash(FileSystemInfo fileInfo)
4344
}
4445
catch (Exception ex)
4546
{
46-
Logger.Instance.Warn("Unable to take hash of file: {0}: {1}", fileInfo.FullName, ex.Message);
47+
Log.Warning("Unable to take hash of file: {0}: {1}", fileInfo.FullName, ex.Message);
4748
}
4849
return hashValue;
4950
}
@@ -75,7 +76,7 @@ public static KeyValuePair<bool, X509Certificate2> GetSignatureDetails(string pa
7576
}
7677
catch(Exception ex)
7778
{
78-
Logger.Instance.Debug(ex, "Exception checking for file signature for {0}: {1}", path, ex.Message);
79+
Log.Debug(ex, "Exception checking for file signature for {0}: {1}", path, ex.Message);
7980
return new KeyValuePair<bool, X509Certificate2>(false, certificate);
8081
}
8182

Lib/Collectors/FileSystem/LinuxFileSystemUtils.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using AttackSurfaceAnalyzer.Utils;
66
using Mono.Unix;
7+
using Serilog;
78

89
namespace AttackSurfaceAnalyzer.Collectors.FileSystem
910
{
@@ -24,7 +25,7 @@ protected internal static string GetFilePermissions(FileSystemInfo fileInfo)
2425
}
2526
catch (Exception ex)
2627
{
27-
Logger.Instance.Warn("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
28+
Log.Warning("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
2829
}
2930
}
3031
else if (fileInfo is DirectoryInfo)
@@ -35,7 +36,7 @@ protected internal static string GetFilePermissions(FileSystemInfo fileInfo)
3536
}
3637
catch (Exception ex)
3738
{
38-
Logger.Instance.Warn("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
39+
Log.Warning("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
3940
}
4041
}
4142
else

Lib/Collectors/FileSystem/WindowsFileSystemUtils.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Security.AccessControl;
66
using AttackSurfaceAnalyzer.Utils;
7+
using Serilog;
78

89
namespace AttackSurfaceAnalyzer.Collectors.FileSystem
910
{
@@ -26,8 +27,8 @@ protected internal static string GetFilePermissions(FileSystemInfo fileInfo)
2627
}
2728
catch (Exception ex)
2829
{
29-
Logger.Instance.Debug("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
30-
//Logger.Instance.Debug(ex.StackTrace);
30+
Log.Debug("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
31+
//Log.Debug(ex.StackTrace);
3132
}
3233
}
3334
else if (fileInfo is DirectoryInfo)
@@ -38,8 +39,8 @@ protected internal static string GetFilePermissions(FileSystemInfo fileInfo)
3839
}
3940
catch (Exception ex)
4041
{
41-
Logger.Instance.Debug("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
42-
//Logger.Instance.Debug(ex.StackTrace);
42+
Log.Debug("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
43+
//Log.Debug(ex.StackTrace);
4344

4445
}
4546
}

0 commit comments

Comments
 (0)