Skip to content

Commit

Permalink
[ExtensionsMetadataGenerator] improving logging
Browse files Browse the repository at this point in the history
  • Loading branch information
brettsam committed Apr 18, 2019
1 parent 71a6ab3 commit d9f1af2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{A829A3BC-40A2-4415-9494-8126AE4FE561}"
ProjectSection(SolutionItems) = preProject
build\metadatagenerator.props = build\metadatagenerator.props
..\..\build\package.props = ..\..\build\package.props
EndProjectSection
EndProject
Global
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,25 @@ public class Program
{
public static void Main(string[] args)
{
if (args.Length < 2)
try
{
System.Console.WriteLine("Usage: ");
System.Console.WriteLine("metadatagen <sourcepath> <output>");
if (args.Length < 2)
{
System.Console.WriteLine("Usage: ");
System.Console.WriteLine("metadatagen <sourcepath> <output>");

return;
}
return;
}

string sourcePath = args[0];
AssemblyLoader.Initialize(sourcePath, Log);
ExtensionsMetadataGenerator.Generate(sourcePath, args[1], Log);
string sourcePath = args[0];
AssemblyLoader.Initialize(sourcePath, Log);
ExtensionsMetadataGenerator.Generate(sourcePath, args[1], Log);
}
catch (Exception ex)
{
Log("Error generating extension metadata: " + ex.ToString());
throw;
}
}

private static void Log(string message)
Expand All @@ -41,32 +49,40 @@ public static void Initialize(string basePath, Action<string> logger)
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string assemblyName = new AssemblyName(args.Name).Name;
string assemblyPath = Path.Combine(basePath, assemblyName + ".dll");

if (File.Exists(assemblyPath))
{
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
return assembly;
}

// If the assembly file is not found, it may be a runtime assembly for a different
// runtime version (i.e. the Function app assembly targets .NET Core 2.2, yet this
// process is running 2.0). In that case, just try to return the currently-loaded assembly,
// even if it's the wrong version; we won't be running it, just reflecting.
try
{
var assembly = Assembly.Load(assemblyName);
return assembly;
string assemblyName = new AssemblyName(args.Name).Name;
string assemblyPath = Path.Combine(basePath, assemblyName + ".dll");

if (File.Exists(assemblyPath))
{
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
return assembly;
}

// If the assembly file is not found, it may be a runtime assembly for a different
// runtime version (i.e. the Function app assembly targets .NET Core 2.2, yet this
// process is running 2.0). In that case, just try to return the currently-loaded assembly,
// even if it's the wrong version; we won't be running it, just reflecting.
try
{
var assembly = Assembly.Load(assemblyName);
return assembly;
}
catch (Exception exc)
{
// Log and continue. This will likely fail as the assembly won't be found, but we have a clear
// message now that can help us diagnose.
logger($"Unable to find fallback for assmebly `{assemblyName}`. {exc.GetType().Name} {exc.Message}");
}

return null;
}
catch (Exception exc)
catch (Exception ex)
{
// Log and continue. This will likely fail as the assembly won't be found, but we have a clear
// message now that can help us diagnose.
logger($"Unable to find fallback for assmebly `{assemblyName}`. {exc.GetType().Name} {exc.Message}");
logger($"Error resolving assembly '{args.Name}': {ex}");
throw;
}

return null;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
Expand Down Expand Up @@ -44,31 +45,46 @@ public override bool Execute()
Arguments = $"Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator.Console.dll \"{SourcePath}\" \"{outputPath}\""
};

var process = new Process { StartInfo = info };
process.EnableRaisingEvents = true;
process.ErrorDataReceived += (s, e) =>
Log.LogMessage(MessageImportance.Low, $"Extensions generator working directory: '{info.WorkingDirectory}'");
Log.LogMessage(MessageImportance.Low, $"Extensions generator path: '{info.FileName}'");
Log.LogCommandLine(MessageImportance.Low, info.Arguments);

using (var process = new Process { StartInfo = info })
{
if (e.Data != null)
{
Log.LogWarning(e.Data);
}
};
process.EnableRaisingEvents = true;

process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
StringBuilder errorString = new StringBuilder();
process.ErrorDataReceived += (s, e) =>
{
if (e.Data != null)
{
Log.LogWarning(e.Data);
errorString.Append(e.Data);
}
};

if (process.ExitCode != 0)
{
Log.LogError("Metadata generation failed.");
StringBuilder outputString = new StringBuilder();
process.OutputDataReceived += (s, e) =>
{
if (e.Data != null)
{
outputString.Append(e.Data);
}
};

return false;
}
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();

process.Close();
if (process.ExitCode != 0)
{
Log.LogError($"Metadata generation failed. Exit code: '{process.ExitCode}' Output: '{outputString.ToString()}' Error: '{errorString.ToString()}'");
return false;
}

return true;
return true;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\metadatagenerator.props" />
<PropertyGroup>
<Version>1.0.2</Version>
<Version>1.1.0</Version>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
<AssemblyName>Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator</AssemblyName>
Expand Down

0 comments on commit d9f1af2

Please sign in to comment.