├── global.json
├── .editorconfig
├── test
├── MessageTemplates.Net35Tests
│ ├── NuGet.config
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── packages.config
│ └── MessageTemplates.Net35Tests.csproj
├── MessageTemplates.Net40Tests
│ ├── NuGet.config
│ ├── packages.config
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── MessageTemplates.Net40Tests.csproj
└── MessageTemplates.Tests
│ ├── MessageTemplates.Tests.csproj
│ ├── Formatting
│ └── JsonValueFormatterTests.cs
│ ├── ParserTests.cs
│ └── FormatTests.cs
├── appveyor.yml
├── src
└── MessageTemplates
│ ├── Core
│ ├── IMessageTemplateParser.cs
│ ├── IScalarConversionPolicy.cs
│ ├── ILogEventPropertyValueFactory.cs
│ ├── Padding.cs
│ ├── TemplatePropertyValueDictionary.cs
│ ├── ScalarTemplatePropertyValueDictionary.cs
│ ├── IDestructuringPolicy.cs
│ ├── ILogEventPropertyFactory.cs
│ ├── TemplatePropertyList.cs
│ └── TemplatePropertyValueList.cs
│ ├── Parsing
│ ├── AlignmentDirection.cs
│ ├── Destructuring.cs
│ ├── Alignment.cs
│ ├── MessageTemplateToken.cs
│ ├── TextToken.cs
│ ├── PropertyToken.cs
│ └── MessageTemplateParser.cs
│ ├── Policies
│ ├── DelegateDestructuringPolicy.cs
│ ├── ReflectionTypesScalarDestructuringPolicy.cs
│ ├── EnumScalarConversionPolicy.cs
│ ├── SimpleScalarConversionPolicy.cs
│ ├── ProjectedDestructuringPolicy.cs
│ ├── ByteArrayScalarConversionPolicy.cs
│ └── NullableScalarConversionPolicy.cs
│ ├── Debugging
│ ├── LoggingFailedException.cs
│ └── SelfLog.cs
│ ├── Parameters
│ ├── GetablePropertyFinder.cs
│ ├── DepthLimiter.cs
│ ├── PropertyBinder.cs
│ └── PropertyValueConverter.cs
│ ├── Structure
│ ├── TemplateProperty.cs
│ ├── SequenceValue.cs
│ ├── DictionaryValue.cs
│ ├── TemplatePropertyValue.cs
│ ├── StructureValue.cs
│ └── ScalarValue.cs
│ ├── MessageTemplates.csproj
│ ├── Data
│ └── TemplatePropertyValueVisitor.cs
│ ├── MessageTemplate.cs
│ └── Formatting
│ └── JsonValueFormatter.cs
├── Build.ps1
├── MessageTemplates.sln
├── .gitattributes
├── README.md
├── .gitignore
└── LICENSE
/global.json:
--------------------------------------------------------------------------------
1 | {
2 | "sdk": {
3 | "version": "2.1.104"
4 | }
5 | }
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | root=true
2 |
3 | [*]
4 | indent_style = space
5 | indent_size = 4
6 |
--------------------------------------------------------------------------------
/test/MessageTemplates.Net35Tests/NuGet.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/test/MessageTemplates.Net40Tests/NuGet.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/test/MessageTemplates.Net35Tests/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("MessageTemplates.Net35Tests")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("MessageTemplates.Net35Tests")]
13 | [assembly: AssemblyCopyright("Copyright © 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
--------------------------------------------------------------------------------
/test/MessageTemplates.Tests/MessageTemplates.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net452;netcoreapp1.0;netcoreapp2.0
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/appveyor.yml:
--------------------------------------------------------------------------------
1 | version: '{build}'
2 | image: Visual Studio 2017
3 | configuration: Release
4 | install:
5 | - ps: mkdir -Force ".\build\" | Out-Null
6 | - ps: Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile ".\build\installcli.ps1"
7 | - ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli"
8 | - ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 2.1.104'
9 | - ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
10 | build_script:
11 | - ps: ./Build.ps1
12 | artifacts:
13 | - path: artifacts/MessageTemplates.*.nupkg
14 | deploy:
15 | - provider: NuGet
16 | api_key:
17 | secure: ZdlULX6DI2b/fKecXcFFuKZmMP+q2lz+us4pAeGqFl7mK0Be2/hG7LDSfZgIgAKI
18 | skip_symbols: true
19 | on:
20 | branch: /^(dev|master)$/
21 |
22 |
--------------------------------------------------------------------------------
/test/MessageTemplates.Net35Tests/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/test/MessageTemplates.Net40Tests/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Core/IMessageTemplateParser.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MessageTemplates.Core
16 | {
17 | interface IMessageTemplateParser
18 | {
19 | MessageTemplate Parse(string messageTemplate);
20 | }
21 | }
--------------------------------------------------------------------------------
/src/MessageTemplates/Core/IScalarConversionPolicy.cs:
--------------------------------------------------------------------------------
1 |
2 | using MessageTemplates.Structure;
3 |
4 | namespace MessageTemplates.Core
5 | {
6 | ///
7 | /// Determine how a simple value is carried through the logging
8 | /// pipeline as an immutable .
9 | ///
10 | interface IScalarConversionPolicy
11 | {
12 | ///
13 | /// If supported, convert the provided value into an immutable scalar.
14 | ///
15 | /// The value to convert.
16 | /// Recursively apply policies to convert additional values.
17 | /// The converted value, or null.
18 | /// True if the value could be converted under this policy.
19 | bool TryConvertToScalar(object value, IMessageTemplatePropertyValueFactory propertyValueFactory, out ScalarValue result);
20 | }
21 | }
--------------------------------------------------------------------------------
/src/MessageTemplates/Core/ILogEventPropertyValueFactory.cs:
--------------------------------------------------------------------------------
1 | using MessageTemplates.Structure;
2 |
3 | namespace MessageTemplates.Core
4 | {
5 | ///
6 | /// Supports the policy-driven construction of s given
7 | /// regular .NET objects.
8 | ///
9 | public interface IMessageTemplatePropertyValueFactory
10 | {
11 | ///
12 | /// Create a given a .NET object and destructuring
13 | /// strategy.
14 | ///
15 | /// The value of the property.
16 | /// If true, and the value is a non-primitive, non-array type,
17 | /// then the value will be converted to a structure; otherwise, unknown types will
18 | /// be converted to scalars, which are generally stored as strings.
19 | /// The value.
20 | TemplatePropertyValue CreatePropertyValue(object value, bool destructureObjects = false);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Core/Padding.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using MessageTemplates.Parsing;
3 |
4 | namespace MessageTemplates.Formatting.Display
5 | {
6 | static class Padding
7 | {
8 | ///
9 | /// Writes the provided value to the output, applying direction-based padding when is provided.
10 | ///
11 | public static void Apply(TextWriter output, string value, Alignment? alignment)
12 | {
13 | if (!alignment.HasValue)
14 | {
15 | output.Write(value);
16 | return;
17 | }
18 |
19 | var pad = alignment.Value.Width - value.Length;
20 |
21 | if (alignment.Value.Direction == AlignmentDirection.Right)
22 | output.Write(new string(' ', pad));
23 |
24 | output.Write(value);
25 |
26 | if (alignment.Value.Direction == AlignmentDirection.Left)
27 | output.Write(new string(' ', pad));
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/src/MessageTemplates/Core/TemplatePropertyValueDictionary.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using MessageTemplates.Structure;
4 |
5 | namespace MessageTemplates.Core
6 | {
7 | ///
8 | ///
9 | ///
10 | public class TemplatePropertyValueDictionary
11 | {
12 | private Dictionary props;
13 |
14 | ///
15 | ///
16 | ///
17 | ///
18 | public TemplatePropertyValueDictionary(TemplatePropertyList props)
19 | {
20 | this.props = props.ToDictionary(p => p.Name, p=> p.Value);
21 | }
22 |
23 | ///
24 | ///
25 | ///
26 | ///
27 | ///
28 | ///
29 | public bool TryGetValue(string propertyName, out TemplatePropertyValue propertyValue)
30 | {
31 | return props.TryGetValue(propertyName, out propertyValue);
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Parsing/AlignmentDirection.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MessageTemplates.Parsing
16 | {
17 | ///
18 | /// Defines the direction of the alignment.
19 | ///
20 | public enum AlignmentDirection
21 | {
22 | ///
23 | /// Text will be left-aligned.
24 | ///
25 | Left,
26 | ///
27 | /// Text will be right-aligned.
28 | ///
29 | Right
30 | }
31 | }
--------------------------------------------------------------------------------
/src/MessageTemplates/Policies/DelegateDestructuringPolicy.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System;
16 | using MessageTemplates.Core;
17 | using MessageTemplates.Structure;
18 |
19 | namespace MessageTemplates.Policies
20 | {
21 | class DelegateDestructuringPolicy : IDestructuringPolicy
22 | {
23 | public bool TryDestructure(object value, IMessageTemplatePropertyValueFactory propertyValueFactory, out TemplatePropertyValue result)
24 | {
25 | if (value is Delegate del)
26 | {
27 | result = new ScalarValue(del.ToString());
28 | return true;
29 | }
30 |
31 | result = null;
32 | return false;
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Debugging/LoggingFailedException.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System;
16 |
17 | namespace MessageTemplates.Debugging
18 | {
19 | ///
20 | /// May be thrown by log event sinks when a failure occurs. Should not be used in cases
21 | /// where the exception would propagate out to callers.
22 | ///
23 | public class LoggingFailedException : Exception
24 | {
25 | ///
26 | /// Construct a to communicate a logging failure.
27 | ///
28 | /// A message describing the logging failure.
29 | public LoggingFailedException(string message)
30 | : base(message)
31 | {
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Core/ScalarTemplatePropertyValueDictionary.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 | using MessageTemplates.Structure;
4 | using System.Linq;
5 |
6 | namespace MessageTemplates.Core
7 | {
8 | ///
9 | ///
10 | ///
11 | public class ScalarTemplatePropertyValueDictionary
12 | : IEnumerable>
13 | {
14 | private IDictionary elements;
15 |
16 | ///
17 | ///
18 | ///
19 | ///
20 | public ScalarTemplatePropertyValueDictionary(
21 | IEnumerable> elements)
22 | {
23 | this.elements = elements.ToDictionary(e => e.Key, e => e.Value);
24 | }
25 |
26 | ///
27 | ///
28 | ///
29 | ///
30 | public IEnumerator> GetEnumerator()
31 | {
32 | return elements.GetEnumerator();
33 | }
34 |
35 | ///
36 | ///
37 | ///
38 | ///
39 | IEnumerator IEnumerable.GetEnumerator()
40 | {
41 | return GetEnumerator();
42 | }
43 | }
44 | }
--------------------------------------------------------------------------------
/src/MessageTemplates/Parsing/Destructuring.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MessageTemplates.Parsing
16 | {
17 | ///
18 | /// Instructs the logger on how to store information about provided
19 | /// parameters.
20 | ///
21 | public enum Destructuring
22 | {
23 | ///
24 | /// Convert known types and objects to scalars, arrays to sequences.
25 | ///
26 | Default,
27 |
28 | ///
29 | /// Convert all types to scalar strings. Prefix name with '$'.
30 | ///
31 | Stringify,
32 |
33 | ///
34 | /// Convert known types to scalars, destructure objects and collections
35 | /// into sequences and structures. Prefix name with '@'.
36 | ///
37 | Destructure
38 | }
39 | }
--------------------------------------------------------------------------------
/src/MessageTemplates/Policies/ReflectionTypesScalarDestructuringPolicy.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2016 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System;
16 | using System.Reflection;
17 | using MessageTemplates.Core;
18 | using MessageTemplates.Structure;
19 |
20 | namespace MessageTemplates.Policies
21 | {
22 | class ReflectionTypesScalarDestructuringPolicy : IDestructuringPolicy
23 | {
24 | public bool TryDestructure(object value, IMessageTemplatePropertyValueFactory propertyValueFactory, out TemplatePropertyValue result)
25 | {
26 | // These types and their subclasses are property-laden and deep;
27 | // most sinks will convert them to strings.
28 | if (value is Type || value is MemberInfo)
29 | {
30 | result = new ScalarValue(value);
31 | return true;
32 | }
33 |
34 | result = null;
35 | return false;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/test/MessageTemplates.Net40Tests/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("MessageTemplates.Net40Tests")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("MessageTemplates.Net40Tests")]
13 | [assembly: AssemblyCopyright("Copyright © 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("481d8962-9efd-4470-bf83-87a761581367")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Policies/EnumScalarConversionPolicy.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Reflection;
16 | using MessageTemplates.Core;
17 | using MessageTemplates.Structure;
18 |
19 | namespace MessageTemplates.Policies
20 | {
21 | class EnumScalarConversionPolicy : IScalarConversionPolicy
22 | {
23 | public bool TryConvertToScalar(object value, IMessageTemplatePropertyValueFactory propertyValueFactory, out ScalarValue result)
24 | {
25 | bool isEnum = false;
26 | #if !REFLECTION_API_EVOLVED // https://blogs.msdn.microsoft.com/dotnet/2012/08/28/evolving-the-reflection-api/
27 | isEnum = value.GetType().IsEnum;
28 | #else
29 | isEnum = value.GetType().GetTypeInfo().IsEnum;
30 | #endif
31 |
32 | if (isEnum)
33 | {
34 | result = new ScalarValue(value);
35 | return true;
36 | }
37 |
38 | result = null;
39 | return false;
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Policies/SimpleScalarConversionPolicy.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System;
16 | using System.Collections.Generic;
17 | using MessageTemplates.Core;
18 | using MessageTemplates.Structure;
19 |
20 | namespace MessageTemplates.Policies
21 | {
22 | class SimpleScalarConversionPolicy : IScalarConversionPolicy
23 | {
24 | readonly HashSet _scalarTypes;
25 |
26 | public SimpleScalarConversionPolicy(IEnumerable scalarTypes)
27 | {
28 | _scalarTypes = new HashSet(scalarTypes);
29 | }
30 |
31 | public bool TryConvertToScalar(object value, IMessageTemplatePropertyValueFactory propertyValueFactory, out ScalarValue result)
32 | {
33 | if (_scalarTypes.Contains(value.GetType()))
34 | {
35 | result = new ScalarValue(value);
36 | return true;
37 | }
38 |
39 | result = null;
40 | return false;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Core/IDestructuringPolicy.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using MessageTemplates.Structure;
16 |
17 | namespace MessageTemplates.Core
18 | {
19 | ///
20 | /// Determine how, when destructuring, a supplied value is represented
21 | /// as a complex log event property.
22 | ///
23 | public interface IDestructuringPolicy
24 | {
25 | ///
26 | /// If supported, destructure the provided value.
27 | ///
28 | /// The value to destructure.
29 | /// Recursively apply policies to destructure additional values.
30 | /// The destructured value, or null.
31 | /// True if the value could be destructured under this policy.
32 | bool TryDestructure(object value, IMessageTemplatePropertyValueFactory propertyValueFactory, out TemplatePropertyValue result);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Parsing/Alignment.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MessageTemplates.Parsing
16 | {
17 | ///
18 | /// A structure representing the alignment settings to apply when rendering a property.
19 | ///
20 | public struct Alignment
21 | {
22 | ///
23 | /// Initializes a new instance of .
24 | ///
25 | /// The text alignment direction.
26 | /// The width of the text, in characters.
27 | public Alignment(AlignmentDirection direction, int width)
28 | {
29 | Direction = direction;
30 | Width = width;
31 | }
32 |
33 | ///
34 | /// The text alignment direction.
35 | ///
36 | public AlignmentDirection Direction { get; }
37 |
38 | ///
39 | /// The width of the text.
40 | ///
41 | public int Width { get; }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/MessageTemplates/Core/ILogEventPropertyFactory.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using MessageTemplates.Structure;
16 |
17 | namespace MessageTemplates.Core
18 | {
19 | ///
20 | /// Creates log event properties from regular .NET objects, applying policies as
21 | /// required.
22 | ///
23 | public interface ILogEventPropertyFactory
24 | {
25 | ///
26 | /// Construct a with the specified name and value.
27 | ///
28 | /// The name of the property.
29 | /// The value of the property.
30 | /// If true, and the value is a non-primitive, non-array type,
31 | /// then the value will be converted to a structure; otherwise, unknown types will
32 | /// be converted to scalars, which are generally stored as strings.
33 | ///
34 | TemplateProperty CreateProperty(string name, object value, bool destructureObjects = false);
35 | }
36 | }
--------------------------------------------------------------------------------
/src/MessageTemplates/Parameters/GetablePropertyFinder.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | #if REFLECTION_API_EVOLVED // https://blogs.msdn.microsoft.com/dotnet/2012/08/28/evolving-the-reflection-api/
16 | using System;
17 | using System.Collections.Generic;
18 | using System.Linq;
19 | using System.Reflection;
20 |
21 | namespace MessageTemplates.Parameters
22 | {
23 | static class GetablePropertyFinder
24 | {
25 | internal static IEnumerable GetPropertiesRecursive(this Type type)
26 | {
27 | var seenNames = new HashSet();
28 |
29 | var currentTypeInfo = type.GetTypeInfo();
30 |
31 | while (currentTypeInfo.AsType() != typeof(object))
32 | {
33 | var unseenProperties = currentTypeInfo.DeclaredProperties.Where(p => p.CanRead &&
34 | p.GetMethod.IsPublic && !p.GetMethod.IsStatic &&
35 | (p.Name != "Item" || p.GetIndexParameters().Length == 0) && !seenNames.Contains(p.Name));
36 |
37 | foreach (var propertyInfo in unseenProperties)
38 | {
39 | seenNames.Add(propertyInfo.Name);
40 | yield return propertyInfo;
41 | }
42 |
43 | currentTypeInfo = currentTypeInfo.BaseType.GetTypeInfo();
44 | }
45 | }
46 | }
47 | }
48 | #endif
--------------------------------------------------------------------------------
/src/MessageTemplates/Core/TemplatePropertyList.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using MessageTemplates.Structure;
5 |
6 | namespace MessageTemplates.Core
7 | {
8 | ///
9 | ///
10 | ///
11 | public class TemplatePropertyList : IEnumerable
12 | {
13 | private readonly TemplateProperty[] result;
14 |
15 | ///
16 | ///
17 | ///
18 | ///
19 | public TemplatePropertyList(TemplateProperty[] result)
20 | {
21 | if (result == null) throw new ArgumentNullException(nameof(result));
22 | this.result = result;
23 | }
24 |
25 | ///
26 | ///
27 | ///
28 | public int Length => result.Length;
29 |
30 | ///
31 | ///
32 | ///
33 | public int Count => result.Length;
34 |
35 | ///
36 | ///
37 | ///
38 | ///
39 | ///
40 | public TemplateProperty this[int index] => result[index];
41 |
42 | /// Returns an enumerator that iterates through a collection.
43 | /// An object that can be used to iterate
44 | /// through the collection.
45 | public IEnumerator GetEnumerator()
46 | {
47 | return ((IEnumerable) result).GetEnumerator();
48 | }
49 |
50 | /// Returns an enumerator that iterates through a collection.
51 | /// An object that can be used to iterate through the collection.
52 | IEnumerator IEnumerable.GetEnumerator()
53 | {
54 | return GetEnumerator();
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/src/MessageTemplates/Policies/ProjectedDestructuringPolicy.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2013-2015 Serilog Contributors
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System;
16 | using MessageTemplates.Core;
17 | using MessageTemplates.Structure;
18 |
19 | namespace MessageTemplates.Policies
20 | {
21 | class ProjectedDestructuringPolicy : IDestructuringPolicy
22 | {
23 | readonly Func _canApply;
24 | readonly Func