@@ -9,16 +9,18 @@ public static class Logger
9
9
static readonly Regex ObscurePasswordRegex = new Regex ( "(https?://)(.+)(:.+@)" , RegexOptions . Compiled ) ;
10
10
static string indent = string . Empty ;
11
11
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
-
16
12
17
13
static Logger ( )
18
14
{
19
15
Reset ( ) ;
20
16
}
21
17
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
+
22
24
public static IDisposable IndentLog ( string operationDescription )
23
25
{
24
26
var start = DateTime . Now ;
@@ -31,6 +33,7 @@ public static IDisposable IndentLog(string operationDescription)
31
33
} ) ;
32
34
}
33
35
36
+
34
37
static Action < string > ObscurePassword ( Action < string > info )
35
38
{
36
39
Action < string > logAction = s =>
@@ -41,34 +44,55 @@ static Action<string> ObscurePassword(Action<string> info)
41
44
return logAction ;
42
45
}
43
46
47
+
44
48
public static void SetLoggers ( Action < string > info , Action < string > warn , Action < string > error )
45
49
{
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
+
46
65
WriteInfo = LogMessage ( ObscurePassword ( info ) , "INFO" ) ;
47
66
WriteWarning = LogMessage ( ObscurePassword ( warn ) , "WARN" ) ;
48
67
WriteError = LogMessage ( ObscurePassword ( error ) , "ERROR" ) ;
49
68
}
50
69
70
+
51
71
static Action < string > LogMessage ( Action < string > logAction , string level )
52
72
{
53
73
return s => logAction ( string . Format ( CultureInfo . InvariantCulture , "{0}{1} [{2:MM/dd/yy H:mm:ss:ff}] {3}" , indent , level , DateTime . Now , s ) ) ;
54
74
}
55
75
76
+
56
77
public static void Reset ( )
57
78
{
58
79
WriteInfo = s => { throw new Exception ( "Logger not defined." ) ; } ;
59
80
WriteWarning = s => { throw new Exception ( "Logger not defined." ) ; } ;
60
81
WriteError = s => { throw new Exception ( "Logger not defined." ) ; } ;
61
82
}
62
83
84
+
63
85
class ActionDisposable : IDisposable
64
86
{
65
87
readonly Action action ;
66
88
89
+
67
90
public ActionDisposable ( Action action )
68
91
{
69
92
this . action = action ;
70
93
}
71
94
95
+
72
96
public void Dispose ( )
73
97
{
74
98
action ( ) ;
0 commit comments