Skip to content

Commit 664c7a4

Browse files
author
Claus
authored
Merge pull request #45 from umbraco/v3/feature/nestedcontent-improved-logging
Improved logging for Nested Content connector
2 parents 5331e5f + c24904b commit 664c7a4

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

src/Umbraco.Deploy.Contrib.Connectors/ValueConnectors/NestedContentValueConnector.cs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class NestedContentValueConnector : IValueConnector
2525
// Umbraco.NestedContent is Core NestedContent (introduced in v7.7)
2626
public virtual IEnumerable<string> PropertyEditorAliases => new[] { "Our.Umbraco.NestedContent", "Umbraco.NestedContent" };
2727

28-
// cannot inject ValueConnectorCollection else of course it creates a circular (recursive) dependency,
28+
// cannot inject ValueConnectorCollection as it creates a circular (recursive) dependency,
2929
// so we have to inject it lazily and use the lazy value when actually needing it
3030
private ValueConnectorCollection ValueConnectors => _valueConnectorsLazy.Value;
3131

@@ -41,12 +41,20 @@ public NestedContentValueConnector(IContentTypeService contentTypeService, Lazy<
4141

4242
public string ToArtifact(object value, PropertyType propertyType, ICollection<ArtifactDependency> dependencies)
4343
{
44+
_logger.Info<NestedContentValueConnector>("Converting {PropertyType} to artifact.", propertyType.Alias);
4445
var svalue = value as string;
46+
4547
if (string.IsNullOrWhiteSpace(svalue))
48+
{
49+
_logger.Warn<NestedContentValueConnector>($"Value is null or whitespace. Skipping conversion to artifact.");
4650
return null;
51+
}
4752

4853
if (svalue.DetectIsJson() == false)
54+
{
55+
_logger.Warn<NestedContentValueConnector>("Value '{Value}' is not a json string. Skipping conversion to artifact.", svalue);
4956
return null;
57+
}
5058

5159
var nestedContent = new List<NestedContentValue>();
5260
if (svalue.Trim().StartsWith("{"))
@@ -59,7 +67,10 @@ public string ToArtifact(object value, PropertyType propertyType, ICollection<Ar
5967
}
6068

6169
if (nestedContent.All(x => x == null))
70+
{
71+
_logger.Warn<NestedContentValueConnector>("Value contained no elements. Skipping conversion to artifact.");
6272
return null;
73+
}
6374

6475
var allContentTypes = nestedContent.Select(x => x.ContentTypeAlias)
6576
.Distinct()
@@ -68,12 +79,13 @@ public string ToArtifact(object value, PropertyType propertyType, ICollection<Ar
6879
//Ensure all of these content types are found
6980
if (allContentTypes.Values.Any(contentType => contentType == null))
7081
{
71-
throw new InvalidOperationException($"Could not resolve these content types for the Nested Content property: {string.Join(",", allContentTypes.Where(x => x.Value == null).Select(x => x.Key))}");
82+
throw new InvalidOperationException($"Could not resolve these content types for the Nested Content property: {string.Join(",", allContentTypes.Where(x => x.Value == null).Select(x => x.Key))}.");
7283
}
7384

7485
//Ensure that these content types have dependencies added
7586
foreach (var contentType in allContentTypes.Values)
7687
{
88+
_logger.Debug<NestedContentValueConnector>("Adding dependency for content type {ContentType}.", contentType.Alias);
7789
dependencies.Add(new ArtifactDependency(contentType.GetUdi(), false, ArtifactDependencyMode.Match));
7890
}
7991

@@ -88,25 +100,24 @@ public string ToArtifact(object value, PropertyType propertyType, ICollection<Ar
88100
if (key == "key")
89101
continue;
90102

91-
var propType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == key);
103+
var innerPropertyType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == key);
92104

93-
if (propType == null)
105+
if (innerPropertyType == null)
94106
{
95-
_logger.Debug<NestedContentValueConnector>($"No property type found with alias {key} on content type {contentType.Alias}");
107+
_logger.Warn<NestedContentValueConnector>("No property type found with alias {PropertyType} on content type {ContentType}.", key, propertyType.Alias);
96108
continue;
97109
}
98110

99111
// fetch the right value connector from the collection of connectors, intended for use with this property type.
100112
// throws if not found - no need for a null check
101-
var propValueConnector = ValueConnectors.Get(propType);
113+
var propertyValueConnector = ValueConnectors.Get(innerPropertyType);
102114

103115
// pass the value, property type and the dependencies collection to the connector to get a "artifact" value
104-
var val = row.PropertyValues[key];
105-
object parsedValue = propValueConnector.ToArtifact(val, propType, dependencies);
116+
var innerValue = row.PropertyValues[key];
117+
object parsedValue = propertyValueConnector.ToArtifact(innerValue, innerPropertyType, dependencies);
106118

107119
// getting Map image value umb://media/43e7401fb3cd48ceaa421df511ec703c to (nothing) - why?!
108-
_logger.Debug<NestedContentValueConnector>("Map " + key + " value '" + row.PropertyValues[key] + "' to '" + parsedValue
109-
+ "' using " + propValueConnector.GetType() + " for " + propType);
120+
_logger.Debug<NestedContentValueConnector>("Mapped {Key} value '{PropertyValue}' to '{ParsedValue}' using {PropertyValueConnectorType} for {PropertyType}.", key, row.PropertyValues[key], parsedValue, propertyValueConnector.GetType(), innerPropertyType.Alias);
110121

111122
parsedValue = parsedValue?.ToString();
112123

@@ -115,23 +126,32 @@ public string ToArtifact(object value, PropertyType propertyType, ICollection<Ar
115126
}
116127

117128
value = JsonConvert.SerializeObject(nestedContent);
129+
_logger.Info<BlockEditorValueConnector>("Finished converting {PropertyType} to artifact.", propertyType.Alias);
118130
return (string)value;
119131
}
120132

121133
public object FromArtifact(string value, PropertyType propertyType, object currentValue)
122134
{
135+
_logger.Info<NestedContentValueConnector>("Converting {PropertyType} from artifact.", propertyType.Alias);
123136
if (string.IsNullOrWhiteSpace(value))
124137
{
138+
_logger.Warn<NestedContentValueConnector>($"Value is null or whitespace. Skipping conversion from artifact.");
125139
return value;
126140
}
127141

128142
if (value.DetectIsJson() == false)
143+
{
144+
_logger.Warn<NestedContentValueConnector>("Value '{Value}' is not a json string. Skipping conversion from artifact.", value);
129145
return value;
146+
}
130147

131148
var nestedContent = JsonConvert.DeserializeObject<NestedContentValue[]>(value);
132149

133-
if (nestedContent == null)
150+
if (nestedContent == null || nestedContent.All(x => x == null))
151+
{
152+
_logger.Warn<NestedContentValueConnector>("Value contained no elements. Skipping conversion from artifact.");
134153
return value;
154+
}
135155

136156
var allContentTypes = nestedContent.Select(x => x.ContentTypeAlias)
137157
.Distinct()
@@ -140,7 +160,7 @@ public object FromArtifact(string value, PropertyType propertyType, object curre
140160
//Ensure all of these content types are found
141161
if (allContentTypes.Values.Any(contentType => contentType == null))
142162
{
143-
throw new InvalidOperationException($"Could not resolve these content types for the Nested Content property: {string.Join(",", allContentTypes.Where(x => x.Value == null).Select(x => x.Key))}");
163+
throw new InvalidOperationException($"Could not resolve these content types for the Nested Content property: {string.Join(",", allContentTypes.Where(x => x.Value == null).Select(x => x.Key))}.");
144164
}
145165

146166
foreach (var row in nestedContent)
@@ -153,25 +173,26 @@ public object FromArtifact(string value, PropertyType propertyType, object curre
153173
// see note in NestedContentValue - leave it unchanged
154174
if (key == "key")
155175
continue;
156-
176+
157177
var innerPropertyType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == key);
158178

159179
if (innerPropertyType == null)
160180
{
161-
_logger.Debug<NestedContentValueConnector>($"No property type found with alias {key} on content type {contentType.Alias}");
181+
_logger.Warn<NestedContentValueConnector>("No property type found with alias {PropertyType} on content type {ContentType}.", key, contentType.Alias);
162182
continue;
163183
}
164184

165185
// fetch the right value connector from the collection of connectors, intended for use with this property type.
166186
// throws if not found - no need for a null check
167-
var propValueConnector = ValueConnectors.Get(innerPropertyType);
187+
var propertyValueConnector = ValueConnectors.Get(innerPropertyType);
168188

169-
var rowValue = row.PropertyValues[key];
189+
var innerValue = row.PropertyValues[key];
170190

171-
if (rowValue != null)
191+
if (innerValue != null)
172192
{
173193
// pass the artifact value and property type to the connector to get a real value from the artifact
174-
var convertedValue = propValueConnector.FromArtifact(rowValue.ToString(), innerPropertyType, null);
194+
var convertedValue = propertyValueConnector.FromArtifact(innerValue.ToString(), innerPropertyType, null);
195+
175196
if (convertedValue == null)
176197
{
177198
row.PropertyValues[key] = null;
@@ -185,16 +206,21 @@ public object FromArtifact(string value, PropertyType propertyType, object curre
185206
{
186207
row.PropertyValues[key] = convertedValue;
187208
}
209+
_logger.Debug<NestedContentValueConnector>("Mapped {Key} value '{PropertyValue}' to '{ConvertedValue}' using {PropertyValueConnectorType} for {PropertyType}.", key, innerValue, convertedValue, propertyValueConnector.GetType(), innerPropertyType.Alias);
188210
}
189211
else
190212
{
191-
row.PropertyValues[key] = rowValue;
213+
row.PropertyValues[key] = innerValue;
214+
_logger.Debug<NestedContentValueConnector>("{Key} value was null. Setting value as null without conversion.", key);
192215
}
193216
}
194217
}
195218

196219
// Note: NestedContent does not use formatting when serializing JSON values.
197220
value = JArray.FromObject(nestedContent).ToString(Formatting.None);
221+
222+
_logger.Info<NestedContentValueConnector>("Finished converting {PropertyType} from artifact.", propertyType.Alias);
223+
198224
return value;
199225
}
200226

0 commit comments

Comments
 (0)