Skip to content

Commit 022b812

Browse files
committed
Feat: Logger AsTrue & AsFalse
1 parent 2e42263 commit 022b812

File tree

1 file changed

+163
-3
lines changed

1 file changed

+163
-3
lines changed

src/CodeOfChaos.Extensions.Serilog/LoggerExtensions.cs

Lines changed: 163 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,167 @@ namespace Serilog;
1515
/// Provides extension methods for the <see cref="ILogger" /> interface.
1616
/// </summary>
1717
public static class LoggerExtensions {
18+
#region AsFalse
19+
/// <summary>
20+
/// Logs a verbose message using the specified message template and property values, and returns false.
21+
/// </summary>
22+
/// <param name="logger">The logger instance used for logging.</param>
23+
/// <param name="messageTemplate">The message template to format and log.</param>
24+
/// <param name="propertyValues">An array of property values to include in the log message.</param>
25+
/// <returns>Always returns false.</returns>
26+
[MessageTemplateFormatMethod("messageTemplate")]
27+
public static bool VerboseAsFalse(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
28+
logger.Verbose(messageTemplate, propertyValues);
29+
return false;
30+
}
31+
32+
/// <summary>
33+
/// Logs an informational message and returns false.
34+
/// </summary>
35+
/// <param name="logger">The logger instance.</param>
36+
/// <param name="messageTemplate">The message template to log.</param>
37+
/// <param name="propertyValues">The property values for the message template.</param>
38+
/// <returns>Always returns false.</returns>
39+
[MessageTemplateFormatMethod("messageTemplate")]
40+
public static bool InformationAsFalse(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
41+
logger.Information(messageTemplate, propertyValues);
42+
return false;
43+
}
44+
45+
/// <summary>
46+
/// Logs a debug-level message and always returns false.
47+
/// </summary>
48+
/// <param name="logger">The logger instance used to log the message.</param>
49+
/// <param name="messageTemplate">The message template that describes the log message.</param>
50+
/// <param name="propertyValues">Optional property values for formatting the message template.</param>
51+
/// <returns>Always returns false.</returns>
52+
[MessageTemplateFormatMethod("messageTemplate")]
53+
public static bool DebugAsFalse(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
54+
logger.Debug(messageTemplate, propertyValues);
55+
return false;
56+
}
57+
58+
/// <summary>
59+
/// Logs a warning message and always returns false.
60+
/// </summary>
61+
/// <param name="logger">The logger instance.</param>
62+
/// <param name="messageTemplate">The message template to log.</param>
63+
/// <param name="propertyValues">Values to format into the template.</param>
64+
/// <returns>Always returns false.</returns>
65+
[MessageTemplateFormatMethod("messageTemplate")]
66+
public static bool WarningAsFalse(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
67+
logger.Warning(messageTemplate, propertyValues);
68+
return false;
69+
}
70+
71+
/// <summary>
72+
/// Logs an error message with the specified message template and property values,
73+
/// then always returns false.
74+
/// </summary>
75+
/// <param name="logger">The logger instance.</param>
76+
/// <param name="messageTemplate">The message template describing the log message format.</param>
77+
/// <param name="propertyValues">An array of objects to format into the message template.</param>
78+
/// <returns>Always returns false.</returns>
79+
[MessageTemplateFormatMethod("messageTemplate")]
80+
public static bool ErrorAsFalse(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
81+
logger.Error(messageTemplate, propertyValues);
82+
return false;
83+
}
84+
85+
/// <summary>
86+
/// Logs a fatal-level message and returns false.
87+
/// </summary>
88+
/// <param name="logger">The logger instance used to write the message.</param>
89+
/// <param name="messageTemplate">The message template that describes the log message.</param>
90+
/// <param name="propertyValues">The values to populate the message template.</param>
91+
/// <returns>Always returns false.</returns>
92+
[MessageTemplateFormatMethod("messageTemplate")]
93+
public static bool FatalAsFalse(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
94+
logger.Fatal(messageTemplate, propertyValues);
95+
return false;
96+
}
97+
#endregion
98+
#region AsTrue
99+
/// <summary>
100+
/// Logs a verbose message and always returns true.
101+
/// </summary>
102+
/// <param name="logger">The logger instance.</param>
103+
/// <param name="messageTemplate">The message template to be logged.</param>
104+
/// <param name="propertyValues">The property values to format the message template.</param>
105+
/// <returns>Always returns true.</returns>
106+
[MessageTemplateFormatMethod("messageTemplate")]
107+
public static bool VerboseAsTrue(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
108+
logger.Verbose(messageTemplate, propertyValues);
109+
return true;
110+
}
111+
112+
/// <summary>
113+
/// Logs a debug-level message and returns true.
114+
/// </summary>
115+
/// <param name="logger">The logger instance.</param>
116+
/// <param name="messageTemplate">The message template for the log entry.</param>
117+
/// <param name="propertyValues">The property values corresponding to the message template.</param>
118+
/// <returns>Always returns true.</returns>
119+
[MessageTemplateFormatMethod("messageTemplate")]
120+
public static bool DebugAsTrue(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
121+
logger.Debug(messageTemplate, propertyValues);
122+
return true;
123+
}
124+
125+
/// <summary>
126+
/// Logs an information-level message and returns true.
127+
/// </summary>
128+
/// <param name="logger">The logger instance.</param>
129+
/// <param name="messageTemplate">The message template to log.</param>
130+
/// <param name="propertyValues">An array of property values to format and include in the message.</param>
131+
/// <returns>Returns true after logging the message.</returns>
132+
[MessageTemplateFormatMethod("messageTemplate")]
133+
public static bool InformationAsTrue(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
134+
logger.Information(messageTemplate, propertyValues);
135+
return true;
136+
}
137+
138+
/// <summary>
139+
/// Logs a warning level message and returns true.
140+
/// </summary>
141+
/// <param name="logger">The logger instance used for logging.</param>
142+
/// <param name="messageTemplate">The message template describing the log event.</param>
143+
/// <param name="propertyValues">The values to format the message template.</param>
144+
/// <returns>Always returns true.</returns>
145+
[MessageTemplateFormatMethod("messageTemplate")]
146+
public static bool WarningAsTrue(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
147+
logger.Warning(messageTemplate, propertyValues);
148+
return true;
149+
}
150+
151+
/// <summary>
152+
/// Logs an error message at the Error level and returns a boolean value of true.
153+
/// </summary>
154+
/// <param name="logger">The logger instance.</param>
155+
/// <param name="messageTemplate">The message template to be logged.</param>
156+
/// <param name="propertyValues">The property values for the message template.</param>
157+
/// <returns>True, indicating that the operation was successful.</returns>
158+
[MessageTemplateFormatMethod("messageTemplate")]
159+
public static bool ErrorAsTrue(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
160+
logger.Error(messageTemplate, propertyValues);
161+
return true;
162+
}
163+
164+
/// <summary>
165+
/// Logs a fatal message and always returns true.
166+
/// </summary>
167+
/// <param name="logger">The logger instance.</param>
168+
/// <param name="messageTemplate">The message template to log.</param>
169+
/// <param name="propertyValues">The values to include in the message template.</param>
170+
/// <returns>Always returns true.</returns>
171+
[MessageTemplateFormatMethod("messageTemplate")]
172+
public static bool FatalAsTrue(this ILogger logger, string messageTemplate, params object?[]? propertyValues) {
173+
logger.Fatal(messageTemplate, propertyValues);
174+
return true;
175+
}
176+
#endregion
177+
178+
#region Throwable
18179
/// <summary>
19180
/// Throws a Error log message, logs the exception, and throws it.
20181
/// </summary>
@@ -83,7 +244,7 @@ public static TException ThrowableFatal<TException>(this ILogger logger, TExcept
83244
logger.Fatal(exception, messageTemplate, propertyValues);
84245
return exception;
85246
}
86-
247+
#endregion
87248
/// <summary>
88249
/// Writes a fatal log message and exits the application with the specified exit code.
89250
/// </summary>
@@ -102,7 +263,6 @@ public static void ExitFatal(this ILogger logger, int exitCode, string messageTe
102263
logger.Fatal(messageTemplate, propertyValues);
103264
throw new ExitApplicationException(exitCode, messageTemplate);
104265
}
105-
106-
266+
107267
public static ILogger ForSectionProperty(this ILogger logger, string sectionName) => logger.ForContext("Section", sectionName);
108268
}

0 commit comments

Comments
 (0)