Skip to content
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

Add option to set template as body #104

Merged
merged 4 commits into from
Sep 18, 2023

Conversation

danelson
Copy link
Contributor

Alternative to #101

Copy link
Member

@nblumhardt nblumhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice clean PR! This looks good to me - the comment/example in the IncludedData enum is good 👍

Had a couple of minor comments.

var logRecord = LogRecordBuilder.ToLogRecord(Some.SerilogEvent(), null, IncludedData.TemplateBody, new());
Assert.NotNull(logRecord.Body);
Assert.Equal(Some.TestMessageTemplate, logRecord.Body.StringValue);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps for the test name, TemplateBodyIncludesMessageTemplateInBody()? (A bit painfully verbose but spells out the goal.)

For the Some.SerilogEvent(), the test could end up inconclusive if the sample event has a template with no holes (so the test wouldn't be able to tell the difference between the template and the rendered message). Does the SerilogEvent() method allow specifying the template, e.g.:

Some.SerilogEvent("Hello, {Name}", "world")

or could we find a way to ensure this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to be explicit about the template? Some.SerilogEvent() defines a default template of "Message template {Variable}". If the default was changed to something like Message template then this test would not fail if the underlying implementation "broke". Is that what you are getting at?

In my opinion Some.SerilogEvent() is a bit odd because it lets you override the template but not the properties. I am thinking the best way to handle this might be to refactor the tests a bit. Something like

- internal static LogEvent SerilogEvent(DateTimeOffset? timestamp = null, Exception? ex = null, string messageTemplate = TestMessageTemplate)
+ internal static LogEvent DefaultSerilogEvent()
+ internal static LogEvent SerilogEvent(DateTimeOffset? timestamp = null, Exception? ex = null, string messageTemplate, IEnumerable<LogEventProperty> properties)

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good to me 👍

(Sorry about the slow reply, spent a few nights away over the weekend, just catching up now 😅 )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made some updates, let me know what you think.

{
logRecord.Body = new AnyValue
{
StringValue = renderedMessage
};
}

if (includedFields.HasFlag(IncludedData.TemplateBody) && logEvent.MessageTemplate.Text.Trim() != "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if would correctly reflect the logic here

ProcessLevel(logRecord, logEvent);
ProcessException(logRecord, logEvent);
ProcessIncludedFields(logRecord, logEvent, includedFields, activityContextCollector);

return logRecord;
}

public static void ProcessMessage(LogRecord logRecord, LogEvent logEvent, IFormatProvider? formatProvider)
public static void ProcessMessage(LogRecord logRecord, LogEvent logEvent, IncludedData includedFields, IFormatProvider? formatProvider)
{
var renderedMessage = CleanMessageTemplateFormatter.Format(logEvent.MessageTemplate, logEvent.Properties, formatProvider);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rendering of the message should move inside the first if block, since it allocates/requires cycles that are wasted when the rendered message is not included (the first arm of the if block will need a nested if (renderedMessage.Trim()) inside it - but the noise seems worth it for the improvement in behavior).

@nblumhardt
Copy link
Member

Since this adds a feature, it should probably also bump the VersionPrefix in the CSPROJ file to 1.1.0.

@nblumhardt nblumhardt merged commit 826727e into serilog:dev Sep 18, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants