Skip to content

Commit

Permalink
Gfs/improve logging (#37)
Browse files Browse the repository at this point in the history
* Switching to programatically defined logger.

* Improve BUILD.md to reflect move to .NET Core 2.2
  • Loading branch information
gfs authored Apr 1, 2019
1 parent e396445 commit 8a7c0f7
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 118 deletions.
20 changes: 11 additions & 9 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Pre-requisites

### CLI + GUI:
- .NET Core 2.1.9 SDK
- .NET Core 2.2.3 SDK
- Latest Visual Studio 2017
- GitVersioning

Expand All @@ -25,25 +25,27 @@ Run these commands in the CLI directory.

#### Building a Debug version

Windows
```
dotnet clean
dotnet build
```

#### Building a Release version

Windows
Linux/Mac
```
.\build.ps1 -release Release
make
```

Linux
#### Building a Release version

Windows
```
sh build-linux.sh -r Release
dotnet publish -c Release -r win10-x64
```

Mac
Linux/Mac
```
sh build-mac.sh -r Release
make release
```

### GUI
Expand Down
64 changes: 37 additions & 27 deletions Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using AttackSurfaceAnalyzer.ObjectTypes;
using Newtonsoft.Json;
using System.Reflection;
using System.Diagnostics;

namespace AttackSurfaceAnalyzer.Cli
{
Expand Down Expand Up @@ -173,7 +174,7 @@ public static class AttackSurfaceAnalyzerCLI

static void Main(string[] args)
{

Logger.Setup();
string version = (Assembly
.GetEntryAssembly()
.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)
Expand Down Expand Up @@ -230,6 +231,12 @@ private static int ListRuns(ListCommandOptions opts)

private static int RunExportCollectCommand(ExportCollectCommandOptions opts)
{
#if DEBUG
Logger.Setup(true, opts.Verbose);
#else
Logger.Setup(false, opts.Verbose);
#endif

DatabaseManager.SqliteFilename = opts.DatabaseFilename;

bool RunComparisons = true;
Expand Down Expand Up @@ -416,6 +423,11 @@ public static void WriteScanJson(int ResultType, string BaseId, string CompareId

private static int RunExportMonitorCommand(ExportMonitorCommandOptions opts)
{
#if DEBUG
Logger.Setup(true, opts.Verbose);
#else
Logger.Setup(false, opts.Verbose);
#endif
DatabaseManager.SqliteFilename = opts.DatabaseFilename;

WriteMonitorJson(opts.RunId, (int)RESULT_TYPE.FILE, opts.OutputPath);
Expand Down Expand Up @@ -459,30 +471,13 @@ public static void WriteMonitorJson(string RunId, int ResultType, string OutputP

private static int RunMonitorCommand(MonitorCommandOptions opts)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (!Elevation.IsAdministrator())
{
Logger.Instance.Warn("Attack Surface Enumerator must be run as Administrator.");
Environment.Exit(1);
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
if (!Elevation.IsRunningAsRoot())
{
Logger.Instance.Fatal("Attack Surface Enumerator must be run as root.");
Environment.Exit(1);
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
if (!Elevation.IsRunningAsRoot())
{
Logger.Instance.Fatal("Attack Surface Enumerator must be run as root.");
Environment.Exit(1);
}
}
#if DEBUG
Logger.Setup(true, opts.Verbose);
#else
Logger.Setup(false, opts.Verbose);
#endif
AdminOrQuit();

DatabaseManager.SqliteFilename = opts.DatabaseFilename;

var cmd = new SqliteCommand(SQL_GET_RUN, DatabaseManager.Connection);
Expand Down Expand Up @@ -864,8 +859,7 @@ public static int StopMonitors()

return 0;
}

public static int RunCollectCommand(CollectCommandOptions opts)
public static void AdminOrQuit()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand All @@ -891,6 +885,17 @@ public static int RunCollectCommand(CollectCommandOptions opts)
Environment.Exit(1);
}
}
}

public static int RunCollectCommand(CollectCommandOptions opts)
{
#if DEBUG
Logger.Setup(true, opts.Verbose);
#else
Logger.Setup(false, opts.Verbose);
#endif
AdminOrQuit();

DatabaseManager.SqliteFilename = opts.DatabaseFilename;

int returnValue = (int)ERRORS.NONE;
Expand Down Expand Up @@ -1037,6 +1042,11 @@ public static void ClearMonitors()

private static int RunCompareCommand(CompareCommandOptions opts)
{
#if DEBUG
Logger.Setup(true, opts.Verbose);
#else
Logger.Setup(false, opts.Verbose);
#endif
DatabaseManager.SqliteFilename = opts.DatabaseFilename;

var results = CompareRuns(opts);
Expand Down
14 changes: 0 additions & 14 deletions Cli/build.ps1

This file was deleted.

31 changes: 0 additions & 31 deletions Cli/nlog.config

This file was deleted.

1 change: 0 additions & 1 deletion Gui/AttackSurfaceAnalyzerGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<AssemblyName>AttackSurfaceAnalyzer-GUI</AssemblyName>
<ReleaseVersion>2.0.0.0-pre-alpha</ReleaseVersion>
<ApplicationInsightsAnnotationResourceId>/subscriptions/743195b0-d049-4532-bb00-4abf3bc37af9/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/AttackSurfaceAnalyzerGui</ApplicationInsightsAnnotationResourceId>
<ApplicationInsightsResourceId>/subscriptions/743195b0-d049-4532-bb00-4abf3bc37af9/resourcegroups/Default-ApplicationInsights-EastUS/providers/microsoft.insights/components/AttackSurfaceAnalyzerGui</ApplicationInsightsResourceId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ElectronNet.API" Version="0.0.11" />
Expand Down
29 changes: 0 additions & 29 deletions Gui/nlog.config

This file was deleted.

1 change: 0 additions & 1 deletion Lib/Utils/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public static void Setup()
cmd = new SqliteCommand(SQL_CREATE_RESULT_COMPARE_RUN_ID_INDEX, DatabaseManager.Connection, DatabaseManager.Transaction);
cmd.ExecuteNonQuery();
Commit();
Logger.Instance.Warn("Done setting up.");
}
}

Expand Down
3 changes: 0 additions & 3 deletions Lib/Utils/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,5 @@ public static string GetVersionString()
return fileVersionInfo.ProductVersion;

}

}


}
52 changes: 49 additions & 3 deletions Lib/Utils/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,68 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using NLog;
using NLog.Config;
using NLog.Targets;

namespace AttackSurfaceAnalyzer.Utils
{
public class Logger
{

public static ILogger Instance { get; private set; }

static Logger()
{
Instance = LogManager.GetCurrentClassLogger();
}

public static void Output(string message, params object[] args)
public static void Setup()
{
Logger.Instance.Info(message, args);
Setup(false, false);
}

public static void Setup(bool debug, bool verbose)
{
// Step 1. Create configuration object
var config = new LoggingConfiguration();

// Step 2. Create targets
var consoleTarget = new ColoredConsoleTarget("console")
{
Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
};
config.AddTarget(consoleTarget);

var fileTarget = new FileTarget("debug")
{
FileName = "asa.debug.log",
Layout = "${longdate} ${level} ${message} ${exception}"
};
config.AddTarget(fileTarget);

if (debug)
{
config.AddRuleForOneLevel(LogLevel.Debug, fileTarget);
config.AddRuleForOneLevel(LogLevel.Warn, consoleTarget);
config.AddRuleForOneLevel(LogLevel.Error, consoleTarget);
config.AddRuleForOneLevel(LogLevel.Fatal, consoleTarget);
}

if (verbose)
{
config.AddRuleForAllLevels(consoleTarget);
}
else
{
config.AddRuleForOneLevel(LogLevel.Info, consoleTarget);
config.AddRuleForOneLevel(LogLevel.Warn, consoleTarget);
config.AddRuleForOneLevel(LogLevel.Error, consoleTarget);
config.AddRuleForOneLevel(LogLevel.Fatal, consoleTarget);
}

// Step 4. Activate the configuration
LogManager.Configuration = config;
}

}
}

0 comments on commit 8a7c0f7

Please sign in to comment.