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

XmlLayout - Render LogEventInfo.Properties as XML #2670

Merged
merged 28 commits into from Nov 2, 2018

Conversation

snakefoot
Copy link
Contributor

@snakefoot snakefoot commented Apr 22, 2018

Ex. SQL XML Column

<properties>
  <property key="Action">GetUsers</property>
  <property key="Controller">UserController</property>
</properties>
SELECT 	[Message]
  , [TimeStamp]
  , [Exception]
  , [Properties].value('(//property[@key="Action"]/node())[1]', 'nvarchar(max)') as Action
FROM [Logs]
WHERE [Properties].value('(//property[@key="Controller"]/node())[1]', 'nvarchar(max)') = 'UserController'

Ex. SMTP mails with advanced XHTML-formatting (colors etc):

msg.BodyFormat = MailFormat.Html;

@codecov
Copy link

codecov bot commented Apr 22, 2018

Codecov Report

Merging #2670 into dev will decrease coverage by <1%.
The diff coverage is 78%.

@@          Coverage Diff           @@
##             dev   #2670    +/-   ##
======================================
- Coverage     80%     80%   -<1%     
======================================
  Files        331     333     +2     
  Lines      25641   26034   +393     
  Branches    3321    3404    +83     
======================================
+ Hits       20560   20844   +284     
- Misses      4145    4227    +82     
- Partials     936     963    +27

@snakefoot
Copy link
Contributor Author

Still work-in-progress

@304NotModified
Copy link
Member

nice!

@snakefoot
Copy link
Contributor Author

Now I think it is ready for review.

@snakefoot snakefoot force-pushed the XmlLayout branch 5 times, most recently from 8dae8cf to d8dfb39 Compare April 24, 2018 19:28
@304NotModified 304NotModified self-requested a review April 24, 2018 19:39
@snakefoot snakefoot force-pushed the XmlLayout branch 2 times, most recently from 33182b8 to 9cdfa86 Compare April 24, 2018 21:43
{
if (i == 0 && (chr == 'x' || chr == 'X') && xmlElementName.Length >= 3)
{
if (char.ToLowerInvariant(xmlElementName[1]) == 'm' && char.ToLowerInvariant(xmlElementName[2]) == 'l')
Copy link
Member

Choose a reason for hiding this comment

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

What are we testing here? The element <xml? And it will be <_xml ?

Please add this to the code comments, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a very nice comment at the top explaining the rules for XML-element-names.

Copy link
Member

Choose a reason for hiding this comment

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

Could we add an unit test for this?

[Layout("XmlLayout")]
[ThreadAgnostic]
[ThreadSafe]
public class XmlLayout : Layout
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't we implement the Interface IIncludeContext ?

Copy link
Contributor Author

@snakefoot snakefoot Apr 29, 2018

Choose a reason for hiding this comment

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

Just like JsonLayout, then XmlLayout doesn't include IncludeNdc and IncludeNdlc as properties.

/// Determines wether or not this attribute will be Json encoded.
/// </summary>
/// <docgen category='XML Attribute Options' order='100' />
public bool Encode
Copy link
Member

Choose a reason for hiding this comment

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

maybe default it to true, as 2 of the 3 ctors set it to true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default is true, as it gets the default value from XmlEncodeLayoutRendererWrapper

src/NLog/Layouts/XmlLayout.cs Outdated Show resolved Hide resolved
src/NLog/Layouts/XmlLayout.cs Outdated Show resolved Hide resolved
src/NLog/Layouts/XmlLayout.cs Outdated Show resolved Hide resolved
src/NLog/Layouts/XmlLayout.cs Outdated Show resolved Hide resolved
src/NLog/Layouts/XmlLayout.cs Outdated Show resolved Hide resolved
Copy link
Member

@304NotModified 304NotModified left a comment

Choose a reason for hiding this comment

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

looks great! Would be nice if we could add some more unit tests for it.

I could help on that, but I'm not sure for every case how to cover it.

PS: im trying create a codecov report for 3d7f3ee

RenderXmlFormattedMessage(logEvent, target);
if (target.Length == orgLength && IncludeEmptyValue && !string.IsNullOrEmpty(ElementName))
{
target.Append('<');
Copy link
Member

Choose a reason for hiding this comment

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

any idea if we could add an unit test for this case?

int beforeAttribLength = sb.Length;
if (!RenderAppendXmlAttributeValue(attrib, logEvent, sb, sb.Length == orgLength))
{
sb.Length = beforeAttribLength;
Copy link
Member

Choose a reason for hiding this comment

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

any idea if we could add an unit test for this case?

string propNameElement = null;
if (_propertiesElementNameHasFormat)
{
propNameElement = XmlHelper.XmlConvertToStringSafe(propName);
Copy link
Member

Choose a reason for hiding this comment

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

any idea if we could add an unit test for this case?

}
else
{
sb.Append('>');
Copy link
Member

Choose a reason for hiding this comment

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

any idea if we could add an unit test for this case?

{
if (i == 0 && (chr == 'x' || chr == 'X') && xmlElementName.Length >= 3)
{
if (char.ToLowerInvariant(xmlElementName[1]) == 'm' && char.ToLowerInvariant(xmlElementName[2]) == 'l')
Copy link
Member

Choose a reason for hiding this comment

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

Could we add an unit test for this?

{
if (chr == '_')
{
sb?.Append(chr);
Copy link
Member

Choose a reason for hiding this comment

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

Could we add an unit test for this?

sb = new StringBuilder(xmlElementName.Length);
if (i > 1)
sb.Append(xmlElementName, 0, i - 1);
if (i == 0 && char.IsWhiteSpace(chr))
Copy link
Member

Choose a reason for hiding this comment

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

Could we add an unit test for this?

@snakefoot
Copy link
Contributor Author

snakefoot commented May 2, 2018

You can have as many unit-test that you like, just a matter of work-hours.

Prefer to have agreement on API before starting to lock things down with unit-tests. But happy you like it.

Copy link
Member

@304NotModified 304NotModified left a comment

Choose a reason for hiding this comment

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

You can have as many unit-test that you like, just a matter of work-hours.

yes, but then no force-push please.

Prefer to have agreement on API before starting to lock things down with unit-test

I'm happy with the design 👍 but there are some small naming things i'm doubting about. Added them as comments

Maybe we should move to a separate XML serializer class in the future, although 3rd party XML serializes aren't common

src/NLog/Layouts/XmlAttribute.cs Show resolved Hide resolved
src/NLog/Layouts/XmlLayout.cs Outdated Show resolved Hide resolved
src/NLog/Layouts/XmlLayout.cs Show resolved Hide resolved
src/NLog/Layouts/XmlLayout.cs Show resolved Hide resolved
src/NLog/Layouts/XmlLayout.cs Outdated Show resolved Hide resolved
src/NLog/Layouts/XmlAttribute.cs Outdated Show resolved Hide resolved
src/NLog/Layouts/XmlAttribute.cs Outdated Show resolved Hide resolved
src/NLog/Layouts/XmlAttribute.cs Outdated Show resolved Hide resolved
/// Auto indent and create new lines
/// </summary>
/// <docgen category='XML Options' order='10' />
public bool IndentXml { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

maybe just "Indent"

src/NLog/Layouts/XmlLayout.cs Show resolved Hide resolved
@304NotModified 304NotModified added this to the 4.6 milestone May 2, 2018
@304NotModified 304NotModified changed the base branch from master to dev August 14, 2018 20:26
@304NotModified
Copy link
Member

@snakefoot could you please resolve the merge conflict? Thanks!

@snakefoot snakefoot force-pushed the XmlLayout branch 4 times, most recently from d95987c to d174e55 Compare September 30, 2018 22:03
@304NotModified 304NotModified merged commit 9a05ee9 into NLog:dev Nov 2, 2018
snakefoot added a commit to snakefoot/NLog that referenced this pull request Nov 4, 2018
* XmlLayout - Render LogEventInfo.Properties as XML (Ex. SQL XML Column)

* Handle self-referencing types

* XmlLayout - Render LogEventInfo.Properties as XML (More test coverage)

* XmlLayout - Render LogEventInfo.Properties as XML (Support for singleton tag)

* XmlLayout - Render LogEventInfo.Properties as XML (Xml config in unit test)

* XmlLayout - Render LogEventInfo.Properties as XML (Less flexible, easier to use)

* XmlLayout - Renamed NodeName to ElementName

* XmlLayout - Fixed XML comments

* XmlLayout - Added support for MutableUnsafe

* XmlLayout - Added support for MutableUnsafe (Refactor)

* XmlLayout - Render LogEventInfo.Properties as XML (Optimize for XmlEncode = false)

* XmlLayout - Render LogEventInfo.Properties as XML (No need for message template, yet)

* XmlLayout - Render LogEventInfo.Properties as XML (Optimize for XmlEncode = false)

* consts, c# 7

* deduplicate

* deduplicate properties rendering

* attrib -> attributes

* small improvements

* XmlLayout - Removed unnecessary allocation of yield state-machine

* XmlLayout - Test proposal

* Added test (fails)

* Added test (succeeds)

* Added test (succeeds)

* Added test (fails)

* Added test (fails)

* Added test (succeeds)

* XmlLayout - Fixed test of IncludeEmptyValue, and removed newline issues

* XmlLayout - Added unit test with encoding of element-names
@snakefoot snakefoot mentioned this pull request Jan 14, 2019
@snakefoot
Copy link
Contributor Author

Started working on https://github.com/NLog/NLog/wiki/XmlLayout

@304NotModified
Copy link
Member

@snakefoot this could be helpful:

XmlLayout.md.txt

@304NotModified
Copy link
Member

304NotModified commented Feb 22, 2019

updated the xml layout. Added some more sections, not sure if it's better. Feel free to restructure

@304NotModified
Copy link
Member

bit stange that attribute has "name" and element has "ElementName " 👼

@snakefoot
Copy link
Contributor Author

bit stange that attribute has "name" and element has "ElementName "

Think it was your decision. Don't mind changing it.

@304NotModified
Copy link
Member

304NotModified commented Feb 22, 2019

Then maybe its my mistake 😂

@snakefoot
Copy link
Contributor Author

snakefoot commented Feb 23, 2019

Created #3137 as example of possible rename.

@304NotModified
Copy link
Member

304NotModified commented Mar 4, 2019

@snakefoot I'm happy with the docs now, are you also? (https://github.com/NLog/NLog/wiki/XmlLayout)

One thing I'm doubting of,

the first "definition" is a bit strange:

image

It's now a combo of a definition (includeAllProperties="Boolean`" ) and an example. Not that I am a fan of the layout of the definitions of other targets...

Should we convert it to an example or a definition?

@304NotModified 304NotModified added the documentation done all docs done (wiki, api docs, lists on nlog-project.org, xmldocs) label Mar 5, 2019
@snakefoot
Copy link
Contributor Author

snakefoot commented Mar 8, 2019

@304NotModified Made some updates to the Wiki-page for XmlLayout: https://github.com/NLog/NLog/wiki/XmlLayout (Like that it is an example)

@snakefoot snakefoot deleted the XmlLayout branch April 4, 2020 13:09
This was referenced Jan 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants