Skip to content
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

Get more logs out of msbuild #189

Merged
merged 1 commit into from
Apr 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 66 additions & 18 deletions src/Ionide.ProjInfo/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,44 @@ module ProjectLoader =
| Success of LoadedProject
| Error of string

let internal logger (writer: StringWriter) =
let internal msBuildLogger = lazy (LogProvider.getLoggerByName "MsBuild") //lazy because dotnet test wont pickup our logger otherwise

let msBuildToLogProvider () =
let msBuildLogger = msBuildLogger.Value

{ new ILogger with
member this.Initialize(eventSource: IEventSource) : unit =
eventSource.ErrorRaised.Add(fun t -> msBuildLogger.error (Log.setMessage t.Message))
eventSource.WarningRaised.Add(fun t -> msBuildLogger.warn (Log.setMessage t.Message))
eventSource.BuildStarted.Add(fun t -> msBuildLogger.info (Log.setMessage t.Message))
eventSource.BuildFinished.Add(fun t -> msBuildLogger.info (Log.setMessage t.Message))

eventSource.MessageRaised.Add(fun t ->
match t.Importance with
| MessageImportance.High -> msBuildLogger.debug (Log.setMessage t.Message)
| MessageImportance.Normal
| MessageImportance.Low
| _ -> msBuildLogger.trace (Log.setMessage t.Message)
)

eventSource.TargetStarted.Add(fun t -> msBuildLogger.trace (Log.setMessage t.Message))
eventSource.TargetFinished.Add(fun t -> msBuildLogger.trace (Log.setMessage t.Message))
eventSource.TaskStarted.Add(fun t -> msBuildLogger.trace (Log.setMessage t.Message))
eventSource.TaskFinished.Add(fun t -> msBuildLogger.trace (Log.setMessage t.Message))

member this.Parameters
with get (): string = ""
and set (v: string): unit = ()

member this.Shutdown() : unit = ()

member this.Verbosity
with get (): LoggerVerbosity = LoggerVerbosity.Detailed
and set (v: LoggerVerbosity): unit = ()
}


let internal stringWriterLogger (writer: StringWriter) =
{ new ILogger with
member this.Initialize(eventSource: IEventSource) : unit =
// eventSource.ErrorRaised.Add(fun t -> writer.WriteLine t.Message) //Only log errors
Expand Down Expand Up @@ -379,15 +416,19 @@ module ProjectLoader =
Some tfm

let createLoggers (paths: string seq) (binaryLogs: BinaryLogGeneration) (sw: StringWriter) =
let logger = logger (sw)
let swLogger = stringWriterLogger (sw)
let msBuildLogger = msBuildToLogProvider ()

let logFilePath (dir: DirectoryInfo, projectPath: string) =
let projectFileName = Path.GetFileName projectPath
let logFileName = Path.ChangeExtension(projectFileName, ".binlog")
Path.Combine(dir.FullName, logFileName)

match binaryLogs with
| BinaryLogGeneration.Off -> [ logger ]
| BinaryLogGeneration.Off -> [
swLogger
msBuildLogger
]
| BinaryLogGeneration.Within dir ->
let loggers =
paths
Expand All @@ -397,7 +438,8 @@ module ProjectLoader =
)

[
logger
swLogger
msBuildLogger
yield! loggers
]

Expand Down Expand Up @@ -923,12 +965,11 @@ type WorkspaceLoaderViaProjectGraph private (toolsPath, ?globalProperties: (stri
pg

let loadProjects (projects: ProjectGraph, customProperties: string list, binaryLogs: BinaryLogGeneration) =
let handleError (e: exn) =
let handleError (msbuildErrors: string) (e: exn) =
let msg = e.Message
printfn "error -> %A" e

logger.error (
Log.setMessage "Failed loading"
Log.setMessageI $"Failed loading {msbuildErrors:msbuildErrors}"
>> Log.addExn e
)

Expand Down Expand Up @@ -994,27 +1035,34 @@ type WorkspaceLoaderViaProjectGraph private (toolsPath, ?globalProperties: (stri

bm.EndBuild()

let msbuildMessage = sw.ToString()

if
result.Exception
|> isNull
|> not
then
handleError result.Exception
handleError msbuildMessage result.Exception
else
let buildProjs =
result.ResultsByNode.Keys
|> Seq.collect (fun (pgn: ProjectGraphNode) -> seq { yield pgn.ProjectInstance })
|> Seq.toList

logger.info (
Log.setMessage "{overallCode}, projects built {count} {projects} "
>> Log.addContextDestructured
"count"
(buildProjs
|> Seq.length)
>> Log.addContextDestructured "overallCode" result.OverallResult
>> Log.addExn result.Exception
)
let projectsBuilt = Seq.length buildProjs

match result.OverallResult with
| BuildResultCode.Success ->
logger.info (
Log.setMessageI $"Overall Build: {result.OverallResult:overallCode}, projects built {projectsBuilt:count}"
>> Log.addExn result.Exception
)
| BuildResultCode.Failure
| _ ->
logger.error (
Log.setMessageI $"Overall Build: {result.OverallResult:overallCode}, projects built {projectsBuilt:count} : {msbuildMessage:msbuildMessage} "
>> Log.addExn result.Exception
)

let projects =
buildProjs
Expand Down Expand Up @@ -1058,7 +1106,7 @@ type WorkspaceLoaderViaProjectGraph private (toolsPath, ?globalProperties: (stri

allProjectOptions :> seq<_>
with e ->
handleError e
handleError "" e


interface IWorkspaceLoader with
Expand Down