Skip to content

Commit ca3d183

Browse files
asbjornuJakeGinnivan
authored andcommitted
Avoid NullReferenceException in Logger by null-guarding the loggers in SetLoggers().
1 parent be4dee3 commit ca3d183

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/GitVersionCore/Logger.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ public static class Logger
99
static readonly Regex ObscurePasswordRegex = new Regex("(https?://)(.+)(:.+@)", RegexOptions.Compiled);
1010
static string indent = string.Empty;
1111

12-
public static Action<string> WriteInfo { get; private set; }
13-
public static Action<string> WriteWarning { get; private set; }
14-
public static Action<string> WriteError { get; private set; }
15-
1612

1713
static Logger()
1814
{
1915
Reset();
2016
}
2117

18+
19+
public static Action<string> WriteInfo { get; private set; }
20+
public static Action<string> WriteWarning { get; private set; }
21+
public static Action<string> WriteError { get; private set; }
22+
23+
2224
public static IDisposable IndentLog(string operationDescription)
2325
{
2426
var start = DateTime.Now;
@@ -31,6 +33,7 @@ public static IDisposable IndentLog(string operationDescription)
3133
});
3234
}
3335

36+
3437
static Action<string> ObscurePassword(Action<string> info)
3538
{
3639
Action<string> logAction = s =>
@@ -41,34 +44,55 @@ static Action<string> ObscurePassword(Action<string> info)
4144
return logAction;
4245
}
4346

47+
4448
public static void SetLoggers(Action<string> info, Action<string> warn, Action<string> error)
4549
{
50+
if (info == null)
51+
{
52+
throw new ArgumentNullException("info");
53+
}
54+
55+
if (warn == null)
56+
{
57+
throw new ArgumentNullException("warn");
58+
}
59+
60+
if (error == null)
61+
{
62+
throw new ArgumentNullException("error");
63+
}
64+
4665
WriteInfo = LogMessage(ObscurePassword(info), "INFO");
4766
WriteWarning = LogMessage(ObscurePassword(warn), "WARN");
4867
WriteError = LogMessage(ObscurePassword(error), "ERROR");
4968
}
5069

70+
5171
static Action<string> LogMessage(Action<string> logAction, string level)
5272
{
5373
return s => logAction(string.Format(CultureInfo.InvariantCulture, "{0}{1} [{2:MM/dd/yy H:mm:ss:ff}] {3}", indent, level, DateTime.Now, s));
5474
}
5575

76+
5677
public static void Reset()
5778
{
5879
WriteInfo = s => { throw new Exception("Logger not defined."); };
5980
WriteWarning = s => { throw new Exception("Logger not defined."); };
6081
WriteError = s => { throw new Exception("Logger not defined."); };
6182
}
6283

84+
6385
class ActionDisposable : IDisposable
6486
{
6587
readonly Action action;
6688

89+
6790
public ActionDisposable(Action action)
6891
{
6992
this.action = action;
7093
}
7194

95+
7296
public void Dispose()
7397
{
7498
action();

0 commit comments

Comments
 (0)