-
Notifications
You must be signed in to change notification settings - Fork 786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Demonstrate compile-time logging source gen without message template #5553
Demonstrate compile-time logging source gen without message template #5553
Conversation
These are the warning messages I got:
This is probably something that I personally would suggest that we remove the warning message entirely, because this is better aligned with opentelemetry-dotnet/docs/logs/complex-objects/Program.cs Lines 34 to 37 in 84bdeb3
|
@@ -32,7 +32,7 @@ internal static partial class LoggerExtensions | |||
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] | |||
public static partial void FoodPriceChanged(this ILogger logger, string name, double price); | |||
|
|||
[LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] | |||
[LoggerMessage(LogLevel.Critical)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not intended to be merged, but to open discussion only right?
Using internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")]
public static partial void FoodRecallNotice(
this ILogger logger,
string brandName,
string productDescription,
string productType,
string recallReasonDescription,
string companyName);
}
logger.FoodRecallNotice(
brandName: "Contoso",
productDescription: "Salads",
productType: "Food & Beverages",
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
companyName: "Contoso Fresh Vegetables, Inc.");
JSON output: {
"brandName": "Contoso",
"productDescription": "Salads",
"productType": "Food & Beverages",
"recallReasonDescription": "due to a possible health risk from Listeria monocytogenes",
"companyName": "Contoso Fresh Vegetables, Inc.",
"{OriginalFormat}": "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription})."
} |
Using internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Critical)]
public static partial void FoodRecallNotice(
this ILogger logger,
string brandName,
string productDescription,
string productType,
string recallReasonDescription,
string companyName);
}
logger.FoodRecallNotice(
brandName: "Contoso",
productDescription: "Salads",
productType: "Food & Beverages",
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
companyName: "Contoso Fresh Vegetables, Inc.");
JSON output: {
"brandName": "Contoso",
"productDescription": "Salads",
"productType": "Food & Beverages",
"recallReasonDescription": "due to a possible health risk from Listeria monocytogenes",
"companyName": "Contoso Fresh Vegetables, Inc.",
"{OriginalFormat}": ""
} Build warnings:
Summary:
|
Using var foodRecallNotice = new FoodRecallNotice
{
BrandName = "Contoso",
ProductDescription = "Salads",
ProductType = "Food & Beverages",
RecallReasonDescription = "due to a possible health risk from Listeria monocytogenes",
CompanyName = "Contoso Fresh Vegetables, Inc.",
};
logger.FoodRecallNotice(foodRecallNotice);
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Critical)]
public static partial void FoodRecallNotice(
this ILogger logger,
[LogProperties(OmitReferenceName = true)] FoodRecallNotice foodRecallNotice);
}
JSON output: {
"CompanyName": "Contoso Fresh Vegetables, Inc.",
"RecallReasonDescription": "due to a possible health risk from Listeria monocytogenes",
"ProductType": "Food & Beverages",
"ProductDescription": "Salads",
"BrandName": "Contoso"
} Summary:
|
This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or Pushing will instruct the bot to automatically remove the label. This bot runs once per day. |
Updating the doc/example to show that it is possible to have structured logging without requiring a message template string.
For users who don't need the format string, this could save computation and networking cost.