Skip to content

Commit 61bf1d6

Browse files
authored
Merge pull request #4699 from devlead/feature/gh-4698
GH4698: Add FormattableString support to logging methods
2 parents 10d6d76 + c1c7b80 commit 61bf1d6

File tree

10 files changed

+1766
-929
lines changed

10 files changed

+1766
-929
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Cake.Core;
7+
using Cake.Core.Annotations;
8+
using Cake.Core.Diagnostics;
9+
10+
namespace Cake.Common.Diagnostics;
11+
12+
public static partial class LoggingAliases
13+
{
14+
/// <summary>
15+
/// Sets the log verbosity to quiet and returns a disposable that restores the log verbosity on dispose.
16+
/// </summary>
17+
/// <param name="context">the context.</param>
18+
/// <returns>A disposable that restores the log verbosity.</returns>
19+
/// <example>
20+
/// <code>
21+
/// using (QuietVerbosity())
22+
/// {
23+
/// Error("Show me.");
24+
/// Warning("Hide me.");
25+
/// Information("Hide me.");
26+
/// Verbose("Hide me.");
27+
/// Debug("Hide me.");
28+
/// }
29+
/// </code>
30+
/// </example>
31+
[CakeMethodAlias]
32+
[CakeAliasCategory("Verbosity")]
33+
public static IDisposable QuietVerbosity(this ICakeContext context)
34+
{
35+
ArgumentNullException.ThrowIfNull(context);
36+
return context.Log.QuietVerbosity();
37+
}
38+
39+
/// <summary>
40+
/// Sets the log verbosity to minimal and returns a disposable that restores the log verbosity on dispose.
41+
/// </summary>
42+
/// <param name="context">the context.</param>
43+
/// <returns>A disposable that restores the log verbosity.</returns>
44+
/// <example>
45+
/// <code>
46+
/// using (MinimalVerbosity())
47+
/// {
48+
/// Error("Show me.");
49+
/// Warning("Show me.");
50+
/// Information("Hide me.");
51+
/// Verbose("Hide me.");
52+
/// Debug("Hide me.");
53+
/// }
54+
/// </code>
55+
/// </example>
56+
[CakeMethodAlias]
57+
[CakeAliasCategory("Verbosity")]
58+
public static IDisposable MinimalVerbosity(this ICakeContext context)
59+
{
60+
ArgumentNullException.ThrowIfNull(context);
61+
return context.Log.MinimalVerbosity();
62+
}
63+
64+
/// <summary>
65+
/// Sets the log verbosity to normal and returns a disposable that restores the log verbosity on dispose.
66+
/// </summary>
67+
/// <param name="context">the context.</param>
68+
/// <returns>A disposable that restores the log verbosity.</returns>
69+
/// <example>
70+
/// <code>
71+
/// using (NormalVerbosity())
72+
/// {
73+
/// Error("Show me.");
74+
/// Warning("Show me.");
75+
/// Information("Show me.");
76+
/// Verbose("Hide me.");
77+
/// Debug("Hide me.");
78+
/// }
79+
/// </code>
80+
/// </example>
81+
[CakeMethodAlias]
82+
[CakeAliasCategory("Verbosity")]
83+
public static IDisposable NormalVerbosity(this ICakeContext context)
84+
{
85+
ArgumentNullException.ThrowIfNull(context);
86+
return context.Log.NormalVerbosity();
87+
}
88+
89+
/// <summary>
90+
/// Sets the log verbosity to verbose and returns a disposable that restores the log verbosity on dispose.
91+
/// </summary>
92+
/// <param name="context">the context.</param>
93+
/// <returns>A disposable that restores the log verbosity.</returns>
94+
/// <example>
95+
/// <code>
96+
/// using (VerboseVerbosity())
97+
/// {
98+
/// Error("Show me.");
99+
/// Warning("Show me.");
100+
/// Information("Show me.");
101+
/// Verbose("Show me.");
102+
/// Debug("Hide me.");
103+
/// }
104+
/// </code>
105+
/// </example>
106+
[CakeMethodAlias]
107+
[CakeAliasCategory("Verbosity")]
108+
public static IDisposable VerboseVerbosity(this ICakeContext context)
109+
{
110+
ArgumentNullException.ThrowIfNull(context);
111+
return context.Log.VerboseVerbosity();
112+
}
113+
114+
/// <summary>
115+
/// Sets the log verbosity to diagnostic and returns a disposable that restores the log verbosity on dispose.
116+
/// </summary>
117+
/// <param name="context">the context.</param>
118+
/// <returns>A disposable that restores the log verbosity.</returns>
119+
/// <example>
120+
/// <code>
121+
/// using (DiagnosticVerbosity())
122+
/// {
123+
/// Error("Show me.");
124+
/// Warning("Show me.");
125+
/// Information("Show me.");
126+
/// Verbose("Show me.");
127+
/// Debug("Show me.");
128+
/// }
129+
/// </code>
130+
/// </example>
131+
[CakeMethodAlias]
132+
[CakeAliasCategory("Verbosity")]
133+
public static IDisposable DiagnosticVerbosity(this ICakeContext context)
134+
{
135+
ArgumentNullException.ThrowIfNull(context);
136+
return context.Log.DiagnosticVerbosity();
137+
}
138+
139+
/// <summary>
140+
/// Sets the log verbosity as specified and returns a disposable that restores the log verbosity on dispose.
141+
/// </summary>
142+
/// <param name="context">The context.</param>
143+
/// <param name="verbosity">The verbosity.</param>
144+
/// <returns>A disposable that restores the log verbosity.</returns>
145+
/// <example>
146+
/// <code>
147+
/// using (DiagnosticVerbosity())
148+
/// {
149+
/// Error("Show me.");
150+
/// Warning("Show me.");
151+
/// Information("Show me.");
152+
/// Verbose("Show me.");
153+
/// Debug("Show me.");
154+
/// }
155+
/// </code>
156+
/// </example>
157+
[CakeMethodAlias]
158+
[CakeAliasCategory("Verbosity")]
159+
public static IDisposable WithVerbosity(this ICakeContext context, Verbosity verbosity)
160+
{
161+
ArgumentNullException.ThrowIfNull(context);
162+
return context.Log.WithVerbosity(verbosity);
163+
}
164+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
using System;
2+
using Cake.Core;
3+
using Cake.Core.Annotations;
4+
using Cake.Core.Diagnostics;
5+
6+
namespace Cake.Common.Diagnostics;
7+
8+
public static partial class LoggingAliases
9+
{
10+
/// <summary>
11+
/// Writes an error message to the log using the specified format information.
12+
/// </summary>
13+
/// <param name="context">The context.</param>
14+
/// <param name="formattable">The string to be formatted.</param>
15+
/// <example>
16+
/// <code>
17+
/// Error($"Hello {"World"}! Today is an {DateTime.Now:dddd}");
18+
/// </code>
19+
/// </example>
20+
[CakeMethodAlias]
21+
[CakeAliasCategory("Error")]
22+
public static void Error(this ICakeContext context, FormattableString formattable)
23+
{
24+
ArgumentNullException.ThrowIfNull(context);
25+
context.Log.Error(formattable);
26+
}
27+
28+
/// <summary>
29+
/// Writes an warning message to the log using the specified format information.
30+
/// </summary>
31+
/// <param name="context">The context.</param>
32+
/// <param name="formattable">The string to be formatted.</param>
33+
/// <example>
34+
/// <code>
35+
/// Warning($"Hello {"World"}! Today is an {DateTime.Now:dddd}");
36+
/// </code>
37+
/// </example>
38+
[CakeMethodAlias]
39+
[CakeAliasCategory("Warning")]
40+
public static void Warning(this ICakeContext context, FormattableString formattable)
41+
{
42+
ArgumentNullException.ThrowIfNull(context);
43+
context.Log.Warning(formattable);
44+
}
45+
46+
/// <summary>
47+
/// Writes an informational message to the log using the specified formattable string.
48+
/// </summary>
49+
/// <param name="context">The context.</param>
50+
/// <param name="formattable">The string to be formatted.</param>
51+
/// <example>
52+
/// <code>
53+
/// Information($"Hello {"World"}! Today is an {DateTime.Now:dddd}");
54+
/// </code>
55+
/// </example>
56+
[CakeMethodAlias]
57+
[CakeAliasCategory("Information")]
58+
public static void Information(this ICakeContext context, FormattableString formattable)
59+
{
60+
ArgumentNullException.ThrowIfNull(context);
61+
context.Log.Information(formattable);
62+
}
63+
64+
/// <summary>
65+
/// Writes an verbose message to the log using the specified formattable string.
66+
/// </summary>
67+
/// <param name="context">The context.</param>
68+
/// <param name="formattable">The string to be formatted.</param>
69+
/// <example>
70+
/// <code>
71+
/// Verbose($"Hello {"World"}! Today is an {DateTime.Now:dddd}");
72+
/// </code>
73+
/// </example>
74+
[CakeMethodAlias]
75+
[CakeAliasCategory("Verbose")]
76+
public static void Verbose(this ICakeContext context, FormattableString formattable)
77+
{
78+
ArgumentNullException.ThrowIfNull(context);
79+
context.Log.Verbose(formattable);
80+
}
81+
82+
/// <summary>
83+
/// Writes an debug message to the log using the specified formattable string.
84+
/// </summary>
85+
/// <param name="context">The context.</param>
86+
/// <param name="formattable">The string to be formatted.</param>
87+
/// <example>
88+
/// <code>
89+
/// Debug($"Hello {"World"}! Today is an {DateTime.Now:dddd}");
90+
/// </code>
91+
/// </example>
92+
[CakeMethodAlias]
93+
[CakeAliasCategory("Debug")]
94+
public static void Debug(this ICakeContext context, FormattableString formattable)
95+
{
96+
ArgumentNullException.ThrowIfNull(context);
97+
context.Log.Debug(formattable);
98+
}
99+
100+
/// <summary>
101+
/// Writes an error message to the log using the specified format information.
102+
/// </summary>
103+
/// <param name="context">The context.</param>
104+
/// <param name="formattableLogAction">The log action.</param>
105+
/// <example>
106+
/// <code>
107+
/// Error(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}"));
108+
/// </code>
109+
/// </example>
110+
[CakeMethodAlias]
111+
[CakeAliasCategory("Error")]
112+
public static void Error(this ICakeContext context, FormattableLogAction formattableLogAction)
113+
{
114+
ArgumentNullException.ThrowIfNull(context);
115+
context.Log.Error(formattableLogAction);
116+
}
117+
118+
/// <summary>
119+
/// Writes an warning message to the log using the specified format information.
120+
/// </summary>
121+
/// <param name="context">The context.</param>
122+
/// <param name="formattableLogAction">The log action.</param>
123+
/// <example>
124+
/// <code>
125+
/// Warning(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}"));
126+
/// </code>
127+
/// </example>
128+
[CakeMethodAlias]
129+
[CakeAliasCategory("Warning")]
130+
public static void Warning(this ICakeContext context, FormattableLogAction formattableLogAction)
131+
{
132+
ArgumentNullException.ThrowIfNull(context);
133+
context.Log.Warning(formattableLogAction);
134+
}
135+
136+
/// <summary>
137+
/// Writes an informational message to the log using the specified formattable string.
138+
/// </summary>
139+
/// <param name="context">The context.</param>
140+
/// <param name="formattableLogAction">The log action.</param>
141+
/// <example>
142+
/// <code>
143+
/// Information(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}"));
144+
/// </code>
145+
/// </example>
146+
[CakeMethodAlias]
147+
[CakeAliasCategory("Information")]
148+
public static void Information(this ICakeContext context, FormattableLogAction formattableLogAction)
149+
{
150+
ArgumentNullException.ThrowIfNull(context);
151+
context.Log.Information(formattableLogAction);
152+
}
153+
154+
/// <summary>
155+
/// Writes an verbose message to the log using the specified formattable string.
156+
/// </summary>
157+
/// <param name="context">The context.</param>
158+
/// <param name="formattableLogAction">The log action.</param>
159+
/// <example>
160+
/// <code>
161+
/// Verbose(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}"));
162+
/// </code>
163+
/// </example>
164+
[CakeMethodAlias]
165+
[CakeAliasCategory("Verbose")]
166+
public static void Verbose(this ICakeContext context, FormattableLogAction formattableLogAction)
167+
{
168+
ArgumentNullException.ThrowIfNull(context);
169+
context.Log.Verbose(formattableLogAction);
170+
}
171+
172+
/// <summary>
173+
/// Writes an debug message to the log using the specified formattable string.
174+
/// </summary>
175+
/// <param name="context">The context.</param>
176+
/// <param name="formattableLogAction">The log action.</param>
177+
/// <example>
178+
/// <code>
179+
/// Debug(logAction => logAction($"Hello {"World"}! Today is an {DateTime.Now:dddd}"));
180+
/// </code>
181+
/// </example>
182+
[CakeMethodAlias]
183+
[CakeAliasCategory("Debug")]
184+
public static void Debug(this ICakeContext context, FormattableLogAction formattableLogAction)
185+
{
186+
ArgumentNullException.ThrowIfNull(context);
187+
context.Log.Debug(formattableLogAction);
188+
}
189+
}

0 commit comments

Comments
 (0)