-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlib-logging.cake
51 lines (45 loc) · 1.44 KB
/
lib-logging.cake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Note: code originally comes from https://stackoverflow.com/questions/50826394/how-to-print-tool-command-line-in-cake
/// <summary>
/// Temporary sets logging verbosity.
/// </summary>
/// <example>
/// <code>
/// // Temporary sets logging verbosity to Diagnostic.
/// using(context.UseVerbosity(Verbosity.Diagnostic))
/// {
/// context.DotNetBuild(project, settings);
/// }
/// </code>
/// </example>
public static VerbosityChanger UseVerbosity(this ICakeContext context, Verbosity newVerbosity) =>
new VerbosityChanger(context.Log, newVerbosity);
/// <summary>
/// Temporary sets logging verbosity to Diagnostic.
/// </summary>
/// <example>
/// <code>
/// // Temporary sets logging verbosity to Diagnostic.
/// using(context.UseDiagnosticVerbosity())
/// {
/// context.DotNetBuild(project, settings);
/// }
/// </code>
/// </example>
public static VerbosityChanger UseDiagnosticVerbosity(this ICakeContext context) =>
context.UseVerbosity(Verbosity.Diagnostic);
/// <summary>
/// Cake log verbosity changer.
/// Restores old verbosity on Dispose.
/// </summary>
public class VerbosityChanger : IDisposable
{
ICakeLog _log;
Verbosity _oldVerbosity;
public VerbosityChanger(ICakeLog log, Verbosity newVerbosity)
{
_log = log;
_oldVerbosity = log.Verbosity;
_log.Verbosity = newVerbosity;
}
public void Dispose() => _log.Verbosity = _oldVerbosity;
}