Skip to content

Commit a31abb7

Browse files
authored
Merge pull request #256 from ionide/251-binlogs
Ability to get binlogs for projects
2 parents 9271790 + dbccb6c commit a31abb7

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# Icon must end with two \r
1111
Icon
1212

13+
# Temporary folders
14+
[Tt]emp/
1315

1416
# Thumbnails
1517
._*
@@ -554,4 +556,4 @@ FodyWeavers.xsd
554556
# fsdocs
555557
tmp
556558
.fsdocs
557-
output
559+
output

build.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pipeline "Build" {
2929
}
3030
stage "sample" {
3131
run
32-
"dotnet run --project src/FSharp.Analyzers.Cli/FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./artifacts/bin/OptionAnalyzer/release --verbosity d"
32+
"dotnet run --project src/FSharp.Analyzers.Cli/FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./artifacts/bin/OptionAnalyzer/release --verbosity d --binlog-path temp/binlogs"
3333
}
3434
stage "docs" { run "dotnet fsdocs build --properties Configuration=Release --eval --clean --strict" }
3535
runIfOnlySpecified false

src/FSharp.Analyzers.Cli/CustomLogging.fs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ type CustomFormatter(options: IOptionsMonitor<CustomOptions>) as this =
3636
textWriter.WriteLine(message)
3737
this.ResetColor(textWriter)
3838
else
39-
this.WritePrefix(textWriter, logEntry.LogLevel)
39+
this.WritePrefix(textWriter, logEntry.Category, logEntry.LogLevel)
4040
textWriter.WriteLine(message)
4141
// logEntry.Formatter doesn't actually write the exception so we need to do that ourselves
4242
// https://github.com/dotnet/runtime/blob/2bcadad3045934b54672e626bbb6131f7d0a523c/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs#L95-L101
4343
if not (isNull logEntry.Exception) then
4444
textWriter.WriteLine(logEntry.Exception.ToString())
4545

46-
member private x.WritePrefix(textWriter: TextWriter, logLevel: LogLevel) =
46+
member private x.WritePrefix(textWriter: TextWriter, category: string, logLevel: LogLevel) =
4747
if not (isNull formatterOptions.TimestampFormat) then
4848
let dateTime =
4949
if formatterOptions.UseUtcTimestamp then
@@ -55,6 +55,8 @@ type CustomFormatter(options: IOptionsMonitor<CustomOptions>) as this =
5555

5656
textWriter.Write($"{timestamp} ")
5757

58+
textWriter.Write($"[{category}] ")
59+
5860
match logLevel with
5961
| LogLevel.Trace -> textWriter.Write("trace: ")
6062
| LogLevel.Debug -> textWriter.Write("debug: ")

src/FSharp.Analyzers.Cli/Program.fs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Arguments =
5858
| [<Unique>] Code_Root of string
5959
| [<Unique; AltCommandLine("-v")>] Verbosity of string
6060
| [<Unique>] Output_Format of string
61+
| [<Unique>] BinLog_Path of string
6162

6263
interface IArgParserTemplate with
6364
member s.Usage =
@@ -92,7 +93,8 @@ type Arguments =
9293
| Code_Root _ ->
9394
"Root of the current code repository, used in the sarif report to construct the relative file path. The current working directory is used by default."
9495
| Output_Format _ ->
95-
"Format in which to write analyzer results to stdout. The available options are: default, github."
96+
"Format in which to write analyzer results to stdout. The available options are: default, github."
97+
| BinLog_Path(_) -> "Path to a directory where MSBuild binary logs (binlog) will be written. You can use https://msbuildlog.com/ to view them."
9698

9799
type SeverityMappings =
98100
{
@@ -157,7 +159,7 @@ let mutable logger: ILogger = Abstractions.NullLogger.Instance
157159

158160
/// <summary>Runs MSBuild to create FSharpProjectOptions based on the projPaths.</summary>
159161
/// <returns>Returns only the FSharpProjectOptions based on the projPaths and not any referenced projects.</returns>
160-
let loadProjects toolsPath properties (projPaths: string list) =
162+
let loadProjects toolsPath properties (projPaths: string list) (binLogPath : DirectoryInfo option) =
161163
async {
162164
let projPaths =
163165
projPaths
@@ -167,7 +169,16 @@ let loadProjects toolsPath properties (projPaths: string list) =
167169
logger.LogInformation("Loading project {0}", proj)
168170

169171
let loader = WorkspaceLoader.Create(toolsPath, properties)
170-
let projectOptions = loader.LoadProjects projPaths
172+
binLogPath
173+
|> Option.iter (fun path ->
174+
logger.LogInformation("Using binary log path: {0}", path.FullName)
175+
)
176+
let binLogConfig =
177+
binLogPath
178+
|> Option.map (fun path -> BinaryLogGeneration.Within path)
179+
|> Option.defaultValue BinaryLogGeneration.Off
180+
181+
let projectOptions = loader.LoadProjects(projPaths, [], binaryLog = binLogConfig)
171182

172183
let failedLoads =
173184
projPaths
@@ -593,10 +604,18 @@ let main argv =
593604
|> ignore
594605
)
595606

596-
logger <- factory.CreateLogger("")
607+
logger <- factory.CreateLogger("FSharp.Analyzers.Cli")
608+
609+
// Set the Ionide.ProjInfo logger to use the same Microsoft.Extensions.Logging logger
610+
if logLevel <= LogLevel.Information then
611+
Ionide.ProjInfo.Logging.Providers.MicrosoftExtensionsLoggingProvider.setMicrosoftLoggerFactory factory
597612

598613
logger.LogInformation("Running in verbose mode")
599614

615+
let binlogPath =
616+
results.TryGetResult <@ BinLog_Path @>
617+
|> Option.map (Path.GetFullPath >> DirectoryInfo)
618+
600619
AppDomain.CurrentDomain.UnhandledException.Add(fun args ->
601620
let ex = args.ExceptionObject :?> exn
602621

@@ -808,7 +827,7 @@ let main argv =
808827
exit (int ExitErrorCodes.InvalidProjectArguments)
809828
async {
810829
let! scriptOptions = scriptOptions |> Async.StartChild
811-
let! loadedProjects = loadProjects toolsPath properties projects |> Async.StartChild
830+
let! loadedProjects = loadProjects toolsPath properties projects binlogPath |> Async.StartChild
812831
let! loadedProjects = loadedProjects
813832
let! scriptOptions = scriptOptions
814833

0 commit comments

Comments
 (0)