-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using consoleLogger in Telemetry client within MultithreadedAnlyzeCom… #1002
Changes from all commits
3f42e99
d8bc8cd
da3bd09
a8a4d83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
using System.Linq; | ||
using System.Reflection; | ||
|
||
using CommandLine; | ||
|
||
using Microsoft.CodeAnalysis.BinaryParsers; | ||
using Microsoft.CodeAnalysis.IL.Rules; | ||
using Microsoft.CodeAnalysis.IL.Sdk; | ||
|
@@ -48,14 +50,26 @@ | |
|
||
public override BinaryAnalyzerContext InitializeGlobalContextFromOptions(AnalyzeOptions options, ref BinaryAnalyzerContext context) | ||
{ | ||
base.InitializeGlobalContextFromOptions(options, ref context); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw! now seems a good time to talk a bit about testing. one thing I observed in the previous change is that you might have broken support for the I was only peripherally involved with the telemetry client work, unfortunately, so I don't know the state of testing. This method is very testable, however, you can create this object and call this public method on it. So I would suggest creating some tests that hit this code path. Verification looks straightforward, populate the object with a non-null Telemetry object, then call this init method and ensure the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep good point. I tried it with C:\Users\marekaldorf\source\repos\binskim\bld\bin\x64_Debug\netcoreapp3.1\BinSkim.exe (process 40088) exited with code 1.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
I tried to redo it a bit with something like this: if (this.Telemetry?.TelemetryClient != null)
{
// Create an aggregating logger that will combine all loggers into a single logger.
var aggregatingLogger = new AggregatingLogger();
if (context.Logger is AggregatingLogger)
{
aggregatingLogger = context.Logger as AggregatingLogger;
}
else
{
aggregatingLogger.Loggers.Add(context.Logger);
}
var ruleTelemetryLogger = new RuleTelemetryLogger(this.Telemetry.TelemetryClient);
ruleTelemetryLogger.AnalysisStarted();
// Combine rule telemetry with any other loggers that may be present.
aggregatingLogger.Loggers.Add(ruleTelemetryLogger);
context.Logger = aggregatingLogger;
} In this case we'll achieve non empty IAnalyseLogger in the first place but it's a bit hacky I would say with the type checks and such WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just FYI, I'm working now on the test hopefully tomorrow I'll make them working :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michaelcfanning just added the tests |
||
|
||
if (this.Telemetry?.TelemetryClient != null) | ||
{ | ||
// Create an aggregating logger that will combine all loggers into a single logger. | ||
var aggregatingLogger = new AggregatingLogger(); | ||
if (context.Logger is AggregatingLogger) | ||
{ | ||
aggregatingLogger = context.Logger as AggregatingLogger; | ||
} | ||
else | ||
{ | ||
aggregatingLogger.Loggers.Add(context.Logger); | ||
} | ||
|
||
var ruleTelemetryLogger = new RuleTelemetryLogger(this.Telemetry.TelemetryClient); | ||
ruleTelemetryLogger.AnalysisStarted(); | ||
|
||
// Combine rule telemetry with any other loggers that may be present. | ||
aggregatingLogger.Loggers.Add(ruleTelemetryLogger); | ||
Check warning Code scanning / CodeQL Dereferenced variable may be null Warning
Variable
aggregatingLogger Error loading related location Loading this Error loading related location Loading |
||
context.Logger = aggregatingLogger; | ||
} | ||
|
||
|
@@ -65,7 +79,7 @@ | |
? options.MaxFileSizeInKilobytes.Value | ||
: long.MaxValue; | ||
|
||
base.InitializeGlobalContextFromOptions(options, ref context); | ||
|
||
|
||
// Update context object based on command-line parameters. | ||
context.SymbolPath = options.SymbolsPath ?? context.SymbolPath; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,16 @@ | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
using FluentAssertions; | ||
|
||
using Microsoft.ApplicationInsights; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using Microsoft.CodeAnalysis.IL; | ||
using Microsoft.CodeAnalysis.IL.Sdk; | ||
using Microsoft.CodeAnalysis.Sarif.Writers; | ||
|
||
using Xunit; | ||
|
||
|
@@ -150,5 +155,89 @@ private static string[] Shuffle(string[] data) | |
|
||
return data; | ||
} | ||
|
||
[Fact] | ||
public void MultithreadedAnalyzeCommand_InitializeGlobalContextFromOptions_TelemetryNotSpecifiedHasOneConsoleLogger() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice tests! |
||
{ | ||
var options = new AnalyzeOptions(); | ||
options.TargetFileSpecifiers = new List<string> { "test.dll" }; | ||
options.DisableTelemetry = true; | ||
options.OutputFilePath = "test/path/"; | ||
var context = new BinaryAnalyzerContext(); | ||
|
||
var command = new MultithreadedAnalyzeCommand(); | ||
command.InitializeGlobalContextFromOptions(options, ref context); | ||
|
||
Assert.IsType<Sarif.Driver.AggregatingLogger>(context.Logger); | ||
|
||
var aggregatingLogger = (Sarif.Driver.AggregatingLogger)context.Logger; | ||
Assert.Contains(aggregatingLogger.Loggers, l => l is ConsoleLogger); | ||
Assert.Equal(1, aggregatingLogger.Loggers.Count); | ||
} | ||
|
||
[Fact] | ||
public void MultithreadedAnalyzeCommand_InitializeGlobalContextFromOptions_TelemetrySpecifiedHasTwoLoggers() | ||
{ | ||
var telemetryConfiguration = TelemetryConfiguration.CreateDefault(); | ||
var telemetryClient = new TelemetryClient(telemetryConfiguration); | ||
var telemetry = new Telemetry(telemetryConfiguration); | ||
|
||
var options = new AnalyzeOptions(); | ||
options.TargetFileSpecifiers = new List<string> { "test.dll" }; | ||
options.OutputFilePath = "test/path/"; | ||
var context = new BinaryAnalyzerContext(); | ||
|
||
var command = new MultithreadedAnalyzeCommand(telemetry); | ||
command.InitializeGlobalContextFromOptions(options, ref context); | ||
|
||
Assert.IsType<Sarif.Driver.AggregatingLogger>(context.Logger); | ||
|
||
var aggregatingLogger = (Sarif.Driver.AggregatingLogger)context.Logger; | ||
Assert.Contains(aggregatingLogger.Loggers, l => l is ConsoleLogger); | ||
Assert.Contains(aggregatingLogger.Loggers, l => l is RuleTelemetryLogger); | ||
Assert.Equal(2, aggregatingLogger.Loggers.Count); | ||
} | ||
|
||
[Fact] | ||
public void MultithreadedAnalyzeCommand_InitializeGlobalContextFromOptions_QuietOptionSetHasOneRuleTelemetryLogger() | ||
{ | ||
var telemetryConfiguration = TelemetryConfiguration.CreateDefault(); | ||
var telemetryClient = new TelemetryClient(telemetryConfiguration); | ||
var telemetry = new Telemetry(telemetryConfiguration); | ||
|
||
var options = new AnalyzeOptions(); | ||
options.TargetFileSpecifiers = new List<string> { "test.dll" }; | ||
options.OutputFilePath = "test/path/"; | ||
options.Quiet = true; | ||
var context = new BinaryAnalyzerContext(); | ||
|
||
var command = new MultithreadedAnalyzeCommand(telemetry); | ||
command.InitializeGlobalContextFromOptions(options, ref context); | ||
|
||
Assert.IsType<Sarif.Driver.AggregatingLogger>(context.Logger); | ||
|
||
var aggregatingLogger = (Sarif.Driver.AggregatingLogger)context.Logger; | ||
Assert.Contains(aggregatingLogger.Loggers, l => l is RuleTelemetryLogger); | ||
Assert.Equal(1, aggregatingLogger.Loggers.Count); | ||
} | ||
|
||
[Fact] | ||
public void MultithreadedAnalyzeCommand_InitializeGlobalContextFromOptions_QuietOptionSetAndNoTelemetryHasOneConsoleLogger() | ||
{ | ||
var options = new AnalyzeOptions(); | ||
options.TargetFileSpecifiers = new List<string> { "test.dll" }; | ||
options.DisableTelemetry = true; | ||
options.Quiet = true; | ||
options.OutputFilePath = "test/path/"; | ||
var context = new BinaryAnalyzerContext(); | ||
|
||
var command = new MultithreadedAnalyzeCommand(); | ||
command.InitializeGlobalContextFromOptions(options, ref context); | ||
|
||
Assert.IsType<Sarif.Driver.AggregatingLogger>(context.Logger); | ||
|
||
var aggregatingLogger = (Sarif.Driver.AggregatingLogger)context.Logger; | ||
Assert.Equal(0, aggregatingLogger.Loggers.Count); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
necessary? btw - in other of our OSS projects we've figured out how to enforce certain style/other guidelines (like strictly removing unused namespace directives) that we could port here. the relationships between build, dotnet format, and .editorconfig are not obvious.