Skip to content

Commit 271b1dd

Browse files
jaybarden1jbarden
andauthored
Add a simple debug, info, warning and error template (#25)
Co-authored-by: Jason Barden <[email protected]>
1 parent 081b66f commit 271b1dd

File tree

6 files changed

+145
-35
lines changed

6 files changed

+145
-35
lines changed

blog-draft.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This guide is for beginner or junior developers (and anyone who enjoys a touch o
88
- **AStarLogger**: An all-in-one logger and telemetry wonder.
99
- **LoggingExtensions**: Easy-to-use methods for wiring up Serilog and Application Insights.
1010
- **SerilogConfigure**: The configuration behind AStar logging magic.
11-
- **LogMessageTemplate**: Strongly-typed, reusable logging templates—because why type the same message twice?
11+
- **LogMessage**: Strongly-typed, reusable logging templates—because why type the same message twice?
1212

1313
Ready? Grab your logging toolbox and let’s get started! 🚀
1414

@@ -105,7 +105,7 @@ What’s happening here? It’s setting up these critical parts:
105105
2. Console output gets formatted with timestamps, log levels, and exceptions.
106106
3. External configuration (`serilogsettings.json`) is loaded.
107107

108-
### 5. **🛠️ LogMessageTemplate: Predefined Messages for Convenience**
108+
### 5. **🛠️ LogMessage: Predefined Messages for Convenience**
109109

110110
Why write the same logging message repeatedly when you can have reusable, strongly-typed templates with `[LoggerMessage]`?
111111
**A Simple Example:**
@@ -114,10 +114,10 @@ Why write the same logging message repeatedly when you can have reusable, strong
114114
var logger = Substitute.For<ILogger>();
115115

116116
// Log a 400 (Bad Request)
117-
LogMessageTemplate.BadRequest(logger, "/example-path");
117+
LogMessage.BadRequest(logger, "/example-path");
118118

119119
// Log a 404 (Not Found)
120-
LogMessageTemplate.NotFound(logger, "/missing-resource");
120+
LogMessage.NotFound(logger, "/missing-resource");
121121
```
122122

123123
These helper methods will output descriptive log entries like:
@@ -149,7 +149,7 @@ app.MapGet("/", (ILogger<AStarLogger<string>> logger, ITelemetryClient telemetry
149149

150150
astarLogger.LogPageView("HomePage"); // Logs and tracks telemetry
151151
logger.LogWarning("This is a warning log.");
152-
LogMessageTemplate.NotFound(logger, "/missing-resource");
152+
LogMessage.NotFound(logger, "/missing-resource");
153153

154154
return Results.Ok("Check your logs for structured awesomeness.");
155155
});
@@ -171,7 +171,7 @@ Searching through unstructured logs is like trying to find Waldo in a snowstorm.
171171
- **👨‍🚀 AStarLogger** combines structured logs with telemetry tracking.
172172
- **🔌 LoggingExtensions** simplifies Serilog and Application Insights configuration.
173173
- **📋 SerilogConfigure** ensures your logs are sent to reliable sinks.
174-
- **🛠️ LogMessageTemplate** makes strongly-typed reusable logs.
174+
- **🛠️ LogMessage** makes strongly-typed reusable logs.
175175

176176
### 🚀 Closing Logs
177177

src/AStar.Dev.Logging.Extensions/AStar.Dev.Logging.Extensions.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<PackageId>AStar.Dev.Logging.Extensions</PackageId>
2323
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2424
<PackageReadmeFile>Readme.md</PackageReadmeFile>
25-
<PackageReleaseNotes>Add some LoggerMessage templates to cover the core use-cases</PackageReleaseNotes>
25+
<PackageReleaseNotes>Add additional LoggerMessage templates to cover the basic use-cases missed previously</PackageReleaseNotes>
2626
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2727
<PackageTags>Logging Application Insights Serilog LoggerMessage Templates</PackageTags>
2828
<PublishRepositoryUrl>true</PublishRepositoryUrl>
@@ -31,7 +31,7 @@
3131
<RepositoryUrl>https://github.com/astar-development/astar-dev-logging-extensions.git</RepositoryUrl>
3232
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
3333
<Title>AStar.Dev.Logging.Extensions</Title>
34-
<Version>0.6.1</Version>
34+
<Version>0.6.2</Version>
3535
</PropertyGroup>
3636

3737
<ItemGroup>

src/AStar.Dev.Logging.Extensions/AStar.Dev.Logging.Extensions.xml

Lines changed: 65 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AStar.Dev.Logging.Extensions/AStarLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public sealed class AStarLogger<TCategoryName>(ILogger<TCategoryName> logger, IT
1313
public void LogPageView(string pageName)
1414
{
1515
ArgumentNullException.ThrowIfNull(pageName);
16-
LogMessageTemplate.NotFound(logger, "/missing-resource");
16+
LogMessage.NotFound(logger, "/missing-resource");
1717

1818
telemetryClient.TrackPageView(pageName);
1919
}

src/AStar.Dev.Logging.Extensions/LogMessageTemplate.cs renamed to src/AStar.Dev.Logging.Extensions/LogMessage.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace AStar.Dev.Logging.Extensions;
55
/// <summary>
66
/// Provides static methods for logging specific HTTP-related events using strongly-typed logging templates.
77
/// </summary>
8-
public static partial class LogMessageTemplate
8+
public static partial class LogMessage
99
{
1010
/// <summary>
1111
/// Logs an informational message indicating that a specific page has been viewed.
@@ -15,6 +15,64 @@ public static partial class LogMessageTemplate
1515
[LoggerMessage(EventId = 200, Level = LogLevel.Information, Message = "Page `{PageName}` viewed.")]
1616
public static partial void PageView(ILogger logger, string pageName);
1717

18+
/// <summary>
19+
/// Logs an informational message indicating that a specific page has been viewed.
20+
/// </summary>
21+
/// <param name="logger">The logger to be used for logging the event.</param>
22+
/// <param name="pageName">The name of the page that was viewed.</param>
23+
[LoggerMessage(EventId = 200, Level = LogLevel.Information, Message = "Page `{PageName}` viewed.")]
24+
public static partial void Trace(ILogger logger, string pageName);
25+
26+
/// <summary>
27+
/// Logs a debug message for the specified location.
28+
/// </summary>
29+
/// <param name="logger">The logger to be used for logging the event.</param>
30+
/// <param name="location">The location of the event.</param>
31+
/// <param name="debugMessage">The debug message to log.</param>
32+
[LoggerMessage(EventId = 200, Level = LogLevel.Debug, Message = "{Location} has raised: `{DebugMessage}`.")]
33+
public static partial void Debug(ILogger logger, string location, string debugMessage);
34+
35+
/// <summary>
36+
/// Logs an informational message for the specified location.
37+
/// </summary>
38+
/// <param name="logger">The logger to be used for logging the event.</param>
39+
/// <param name="location">The location of the event.</param>
40+
/// <param name="informationMessage">The information message to log.</param>
41+
[LoggerMessage(EventId = 200, Level = LogLevel.Information, Message = "{Location} has raised: `{InformationMessage}`.")]
42+
public static partial void Information(ILogger logger, string location, string informationMessage);
43+
44+
/// <summary>
45+
/// Logs an informational message with details about a specific API call.
46+
/// </summary>
47+
/// <param name="logger">The logger to be used for logging the event.</param>
48+
/// <param name="location">The location where the event occurred.</param>
49+
/// <param name="httpRequest">A summary of the request.</param>
50+
/// <param name="httpMethod">The HTTP Method (GET / POST etc.)</param>
51+
/// <param name="apiEndpoint">The API Endpoint called.</param>
52+
/// <param name="apiName">The name of the API.</param>
53+
[LoggerMessage(EventId = 200, Level = LogLevel.Information, Message = "{Location} - request: {HttpRequest}, Method: {HttpMethod}, apiEndpoint: {ApiEndpoint}, apiName: {ApiName}.")]
54+
public static partial void Information(ILogger logger, string location, string httpRequest, string httpMethod, string apiEndpoint, string apiName);
55+
56+
/// <summary>
57+
/// Logs a warning message for the specified location.
58+
/// </summary>
59+
/// <param name="logger">The logger to be used for logging the event.</param>
60+
/// <param name="location">The location of the event.</param>
61+
/// <param name="warningMessage">The warning message to log.</param>
62+
[LoggerMessage(EventId = 400, Level = LogLevel.Warning, Message = "{Location} has raised: `{WarningMessage}`.")]
63+
public static partial void Warning(ILogger logger, string location, string warningMessage);
64+
65+
/// <summary>
66+
/// Logs a critical exception event, providing detailed information about the exception type, message, and stack trace.
67+
/// </summary>
68+
/// <param name="logger">The logger used to log the event.</param>
69+
/// <param name="location">The location of the event.</param>
70+
/// <param name="exceptionType">The type of the exception being logged.</param>
71+
/// <param name="exceptionMessage">The message associated with the exception.</param>
72+
/// <param name="exceptionStack">The stack trace of the exception.</param>
73+
[LoggerMessage(EventId = 500, Level = LogLevel.Error, Message = "{Location} encountered {exceptionType} with `{exceptionMessage}`\nExceptionStack: {exceptionStack}")]
74+
public static partial void LogException(ILogger logger, string location, string exceptionType, string exceptionMessage, string exceptionStack);
75+
1876
/// <summary>
1977
/// Logs a warning message for a Bad Request (400) event, including the requested path.
2078
/// </summary>

0 commit comments

Comments
 (0)