예제 #1
0
        private static void LogToolNotification(
            SarifLogger logger,
            string message,
            FailureLevel level = FailureLevel.Note,
            Exception ex       = null)
        {
            ExceptionData exceptionData = null;

            if (ex != null)
            {
                exceptionData = new ExceptionData
                {
                    Kind    = ex.GetType().FullName,
                    Message = ex.Message,
                    Stack   = Stack.CreateStacks(ex).FirstOrDefault()
                };
            }

            TextWriter writer = level == FailureLevel.Error ? Console.Error : Console.Out;

            writer.WriteLine(message);
            logger.LogToolNotification(new Notification
            {
                Level   = level,
                Message = new Message
                {
                    Text = message
                },
                Exception = exceptionData
            });
        }
예제 #2
0
        public void SarifLogger_DoNotScrapeFilesFromNotifications()
        {
            var sb = new StringBuilder();

            using (var textWriter = new StringWriter(sb))
            {
                using (var sarifLogger = new SarifLogger(
                           textWriter,
                           analysisTargets: null,
                           dataToInsert: OptionallyEmittedData.Hashes,
                           prereleaseInfo: null,
                           invocationTokensToRedact: null,
                           invocationPropertiesToLog: null))
                {
                    var toolNotification = new Notification
                    {
                        PhysicalLocation = new PhysicalLocation {
                            FileLocation = new FileLocation {
                                Uri = new Uri(@"file:///file0.cpp")
                            }
                        }
                    };
                    sarifLogger.LogToolNotification(toolNotification);

                    var configurationNotification = new Notification
                    {
                        PhysicalLocation = new PhysicalLocation {
                            FileLocation = new FileLocation {
                                Uri = new Uri(@"file:///file0.cpp")
                            }
                        }
                    };
                    sarifLogger.LogConfigurationNotification(configurationNotification);

                    string ruleId = "RuleId";
                    var    rule   = new Rule()
                    {
                        Id = ruleId
                    };

                    var result = new Result()
                    {
                        RuleId = ruleId
                    };

                    sarifLogger.Log(rule, result);
                }
            }

            string logText  = sb.ToString();
            var    sarifLog = JsonConvert.DeserializeObject <SarifLog>(logText);

            sarifLog.Runs[0].Files.Should().BeNull();
        }
예제 #3
0
        public void SarifLogger_DoNotScrapeFilesFromNotifications()
        {
            var sb = new StringBuilder();

            using (var textWriter = new StringWriter(sb))
            {
                using (var sarifLogger = new SarifLogger(
                           textWriter,
                           analysisTargets: null,
                           loggingOptions: LoggingOptions.ComputeFileHashes,
                           prereleaseInfo: null,
                           invocationTokensToRedact: null,
                           invocationPropertiesToLog: null))
                {
                    var toolNotification = new Notification
                    {
                        PhysicalLocation = new PhysicalLocation {
                            Uri = new Uri(@"file:///file0.cpp")
                        }
                    };
                    sarifLogger.LogToolNotification(toolNotification);

                    var configurationNotification = new Notification
                    {
                        PhysicalLocation = new PhysicalLocation {
                            Uri = new Uri(@"file:///file0.cpp")
                        }
                    };
                    sarifLogger.LogConfigurationNotification(configurationNotification);
                }
            }

            string logText  = sb.ToString();
            var    sarifLog = JsonConvert.DeserializeObject <SarifLog>(logText);

            sarifLog.Runs[0].Files.Should().BeNull();
        }
예제 #4
0
        public void SarifLogger_DoNotScrapeFilesFromNotifications()
        {
            var sb = new StringBuilder();

            using (var textWriter = new StringWriter(sb))
            {
                using (var sarifLogger = new SarifLogger(
                           textWriter,
                           analysisTargets: null,
                           dataToInsert: OptionallyEmittedData.Hashes,
                           invocationTokensToRedact: null,
                           invocationPropertiesToLog: null))
                {
                    var toolNotification = new Notification
                    {
                        Locations = new List <Location>
                        {
                            new Location
                            {
                                PhysicalLocation = new PhysicalLocation {
                                    ArtifactLocation = new ArtifactLocation {
                                        Uri = new Uri(@"file:///file.cpp")
                                    }
                                }
                            }
                        },
                        Message = new Message {
                            Text = "A notification was raised."
                        }
                    };
                    sarifLogger.LogToolNotification(toolNotification);

                    var configurationNotification = new Notification
                    {
                        Locations = new List <Location>
                        {
                            new Location
                            {
                                PhysicalLocation = new PhysicalLocation {
                                    ArtifactLocation = new ArtifactLocation {
                                        Uri = new Uri(@"file:///file.cpp")
                                    }
                                }
                            }
                        },
                        Message = new Message {
                            Text = "A notification was raised."
                        }
                    };
                    sarifLogger.LogConfigurationNotification(configurationNotification);

                    string ruleId = "RuleId";
                    var    rule   = new ReportingDescriptor {
                        Id = ruleId
                    };

                    var result = new Result
                    {
                        RuleId  = ruleId,
                        Message = new Message {
                            Text = "Some testing occurred."
                        }
                    };

                    sarifLogger.Log(rule, result);
                }
            }

            string logText  = sb.ToString();
            var    sarifLog = JsonConvert.DeserializeObject <SarifLog>(logText);

            sarifLog.Runs[0].Artifacts.Should().BeNull();
        }