Make the Logger class non static and use it with dependency injection

This commit is contained in:
Cédric Luthi 2017-11-04 13:55:58 +01:00 коммит произвёл Ahmed ElSayed
Родитель d7ce83257e
Коммит 9fd3bdc1d8
5 изменённых файлов: 51 добавлений и 29 удалений

Просмотреть файл

@ -8,11 +8,13 @@ namespace MakeFunctionJson
{
private const string buildArtifactsLogName = "functionsSdk.out";
private readonly HashSet<string> _artifacts;
private readonly ILogger _logger;
private readonly string _logPath;
public BuildArtifactsLog(string outputPath)
public BuildArtifactsLog(ILogger logger, string outputPath)
{
_artifacts = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_logPath = Path.Combine(outputPath, buildArtifactsLogName);
try
{
@ -26,8 +28,8 @@ namespace MakeFunctionJson
}
catch (Exception e)
{
Logger.LogWarning($"Unable to read file {_logPath}");
Logger.LogWarningFromException(e);
_logger.LogWarning($"Unable to read file {_logPath}");
_logger.LogWarningFromException(e);
}
}
@ -57,13 +59,13 @@ namespace MakeFunctionJson
{
if (isError)
{
Logger.LogError(message);
Logger.LogErrorFromException(e);
_logger.LogError(message);
_logger.LogErrorFromException(e);
}
else
{
Logger.LogWarning(message);
Logger.LogWarningFromException(e);
_logger.LogWarning(message);
_logger.LogWarningFromException(e);
}
return !isError;
}

Просмотреть файл

@ -2,24 +2,24 @@
namespace MakeFunctionJson
{
internal static class Logger
internal class ConsoleLogger : ILogger
{
public static void LogError(string message)
public void LogError(string message)
{
Console.Error.WriteLine(message);
}
public static void LogErrorFromException(Exception e)
public void LogErrorFromException(Exception e)
{
Console.Error.WriteLine(e);
}
public static void LogWarningFromException(Exception e)
public void LogWarningFromException(Exception e)
{
Console.WriteLine(e);
}
public static void LogWarning(string message)
public void LogWarning(string message)
{
Console.WriteLine(message);
}

Просмотреть файл

@ -11,6 +11,7 @@ namespace MakeFunctionJson
{
private string _assemblyPath;
private string _outputPath;
private readonly ILogger _logger;
private readonly IDictionary<string, MethodInfo> _functionNamesSet;
private readonly BuildArtifactsLog _buildArtifactsLog;
@ -20,8 +21,13 @@ namespace MakeFunctionJson
"host.json"
};
internal FunctionJsonConverter(string assemblyPath, string outputPath)
internal FunctionJsonConverter(ILogger logger, string assemblyPath, string outputPath)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
if (string.IsNullOrEmpty(assemblyPath))
{
throw new ArgumentNullException(nameof(assemblyPath));
@ -32,6 +38,7 @@ namespace MakeFunctionJson
throw new ArgumentNullException(nameof(outputPath));
}
_logger = logger;
_assemblyPath = assemblyPath;
_outputPath = outputPath.Trim('"');
if (!Path.IsPathRooted(_outputPath))
@ -39,7 +46,7 @@ namespace MakeFunctionJson
_outputPath = Path.Combine(Directory.GetCurrentDirectory(), _outputPath);
}
_functionNamesSet = new Dictionary<string, MethodInfo>(StringComparer.OrdinalIgnoreCase);
_buildArtifactsLog = new BuildArtifactsLog(_outputPath);
_buildArtifactsLog = new BuildArtifactsLog(logger, _outputPath);
}
/// <summary>
@ -61,7 +68,7 @@ namespace MakeFunctionJson
this._functionNamesSet.Clear();
if (!_buildArtifactsLog.TryClearBuildArtifactsLog())
{
Logger.LogError("Unable to clean build artifacts file.");
_logger.LogError("Unable to clean build artifacts file.");
return false;
}
@ -72,7 +79,7 @@ namespace MakeFunctionJson
}
catch (Exception e)
{
Logger.LogErrorFromException(e);
_logger.LogErrorFromException(e);
return false;
}
}
@ -95,8 +102,8 @@ namespace MakeFunctionJson
}
catch (Exception e)
{
Logger.LogWarning($"Unable to copy '{sourceFile}' to '{targetFile}'");
Logger.LogWarningFromException(e);
_logger.LogWarning($"Unable to copy '{sourceFile}' to '{targetFile}'");
_logger.LogWarningFromException(e);
}
}
}
@ -113,7 +120,7 @@ namespace MakeFunctionJson
{
if (method.HasUnsuportedAttributes(out string error))
{
Logger.LogError(error);
_logger.LogError(error);
return false;
}
else if (method.IsWebJobsSdkMethod())
@ -139,11 +146,11 @@ namespace MakeFunctionJson
}
else if (method.HasFunctionNameAttribute())
{
Logger.LogWarning($"Method {method.Name} is missing a trigger attribute. Both a trigger attribute and FunctionName attribute are required for an Azure function definition.");
_logger.LogWarning($"Method {method.Name} is missing a trigger attribute. Both a trigger attribute and FunctionName attribute are required for an Azure function definition.");
}
else if (method.HasWebJobSdkAttribute())
{
Logger.LogWarning($"Method {method.Name} is missing the 'FunctionName' attribute. Both a trigger attribute and 'FunctionName' are required for an Azure function definition.");
_logger.LogWarning($"Method {method.Name} is missing the 'FunctionName' attribute. Both a trigger attribute and 'FunctionName' are required for an Azure function definition.");
}
}
}
@ -158,7 +165,7 @@ namespace MakeFunctionJson
if (this._functionNamesSet.ContainsKey(functionName))
{
var dupMethod = this._functionNamesSet[functionName];
Logger.LogError($"Function {method.DeclaringType.FullName}.{method.Name} and {dupMethod.DeclaringType.FullName}.{dupMethod.Name} have the same value for FunctionNameAttribute. Each function must have a unique name.");
_logger.LogError($"Function {method.DeclaringType.FullName}.{method.Name} and {dupMethod.DeclaringType.FullName}.{dupMethod.Name} have the same value for FunctionNameAttribute. Each function must have a unique name.");
return false;
}
else
@ -190,7 +197,7 @@ namespace MakeFunctionJson
if (string.IsNullOrWhiteSpace(azureWebJobsStorage) && !isHttpTrigger)
{
Logger.LogWarning($"Function [{functionName}]: Missing value for AzureWebJobsStorage in {settingsFileName}. This is required for all triggers other than HTTP.");
_logger.LogWarning($"Function [{functionName}]: Missing value for AzureWebJobsStorage in {settingsFileName}. This is required for all triggers other than HTTP.");
}
foreach (var binding in functionJson.Bindings)
@ -202,7 +209,7 @@ namespace MakeFunctionJson
var appSettingName = token.Value.ToString();
if (!values.Any(v => v.Key.Equals(appSettingName, StringComparison.OrdinalIgnoreCase)))
{
Logger.LogWarning($"Function [{functionName}]: cannot find value named '{appSettingName}' in {settingsFileName} that matches '{token.Key}' property set on '{binding["type"]?.ToString()}'");
_logger.LogWarning($"Function [{functionName}]: cannot find value named '{appSettingName}' in {settingsFileName} that matches '{token.Key}' property set on '{binding["type"]?.ToString()}'");
}
}
}
@ -211,7 +218,7 @@ namespace MakeFunctionJson
}
catch (Exception e)
{
Logger.LogWarningFromException(e);
_logger.LogWarningFromException(e);
}
// We only return false on an error, not a warning.
return true;
@ -233,7 +240,7 @@ namespace MakeFunctionJson
}
catch
{
Logger.LogWarning($"Unable to clean directory {directory}.");
_logger.LogWarning($"Unable to clean directory {directory}.");
}
});
}

Просмотреть файл

@ -0,0 +1,12 @@
using System;
namespace MakeFunctionJson
{
internal interface ILogger
{
void LogError(string message);
void LogErrorFromException(Exception e);
void LogWarning(string message);
void LogWarningFromException(Exception e);
}
}

Просмотреть файл

@ -6,18 +6,19 @@ namespace Microsoft.NET.Sdk.Functions.Console
{
private static void Main(string[] args)
{
var logger = new ConsoleLogger();
if (args.Length != 2)
{
Logger.LogError("USAGE: <assemblyPath> <outputPath>");
logger.LogError("USAGE: <assemblyPath> <outputPath>");
}
else
{
var assemblyPath = args[0];
var outputPath = args[1];
var converter = new FunctionJsonConverter(assemblyPath, outputPath);
var converter = new FunctionJsonConverter(logger, assemblyPath, outputPath);
if (!converter.TryRun())
{
Logger.LogError("Error generating functions metadata");
logger.LogError("Error generating functions metadata");
}
}
}