Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit c32e76f

Browse files
committed
Added validation to verify format string named parameter count
[Fixes #413] LogValues should throw if too many/too few arguments in format string
1 parent b494969 commit c32e76f

File tree

6 files changed

+317
-15
lines changed

6 files changed

+317
-15
lines changed

src/Microsoft.Extensions.Logging.Abstractions/LoggerMessage.cs

+28-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using Microsoft.Extensions.Logging.Abstractions;
78
using Microsoft.Extensions.Logging.Internal;
89

910
namespace Microsoft.Extensions.Logging
@@ -20,7 +21,9 @@ public static class LoggerMessage
2021
/// <returns>A delegate which when invoked creates a log scope.</returns>
2122
public static Func<ILogger, IDisposable> DefineScope(string formatString)
2223
{
23-
var logValues = new LogValues(new LogValuesFormatter(formatString));
24+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 0);
25+
26+
var logValues = new LogValues(formatter);
2427

2528
return logger => logger.BeginScope(logValues);
2629
}
@@ -33,7 +36,7 @@ public static Func<ILogger, IDisposable> DefineScope(string formatString)
3336
/// <returns>A delegate which when invoked creates a log scope.</returns>
3437
public static Func<ILogger, T1, IDisposable> DefineScope<T1>(string formatString)
3538
{
36-
var formatter = new LogValuesFormatter(formatString);
39+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 1);
3740

3841
return (logger, arg1) => logger.BeginScope(new LogValues<T1>(formatter, arg1));
3942
}
@@ -47,7 +50,7 @@ public static Func<ILogger, T1, IDisposable> DefineScope<T1>(string formatString
4750
/// <returns>A delegate which when invoked creates a log scope.</returns>
4851
public static Func<ILogger, T1, T2, IDisposable> DefineScope<T1, T2>(string formatString)
4952
{
50-
var formatter = new LogValuesFormatter(formatString);
53+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 2);
5154

5255
return (logger, arg1, arg2) => logger.BeginScope(new LogValues<T1, T2>(formatter, arg1, arg2));
5356
}
@@ -62,7 +65,7 @@ public static Func<ILogger, T1, T2, IDisposable> DefineScope<T1, T2>(string form
6265
/// <returns>A delegate which when invoked creates a log scope.</returns>
6366
public static Func<ILogger, T1, T2, T3, IDisposable> DefineScope<T1, T2, T3>(string formatString)
6467
{
65-
var formatter = new LogValuesFormatter(formatString);
68+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 3);
6669

6770
return (logger, arg1, arg2, arg3) => logger.BeginScope(new LogValues<T1, T2, T3>(formatter, arg1, arg2, arg3));
6871
}
@@ -76,7 +79,7 @@ public static Func<ILogger, T1, T2, T3, IDisposable> DefineScope<T1, T2, T3>(str
7679
/// <returns>A delegate which when invoked creates a log message.</returns>
7780
public static Action<ILogger, Exception> Define(LogLevel logLevel, EventId eventId, string formatString)
7881
{
79-
var formatter = new LogValuesFormatter(formatString);
82+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 0);
8083

8184
return (logger, exception) =>
8285
{
@@ -97,7 +100,7 @@ public static Action<ILogger, Exception> Define(LogLevel logLevel, EventId event
97100
/// <returns>A delegate which when invoked creates a log message.</returns>
98101
public static Action<ILogger, T1, Exception> Define<T1>(LogLevel logLevel, EventId eventId, string formatString)
99102
{
100-
var formatter = new LogValuesFormatter(formatString);
103+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 1);
101104

102105
return (logger, arg1, exception) =>
103106
{
@@ -119,7 +122,7 @@ public static Action<ILogger, T1, Exception> Define<T1>(LogLevel logLevel, Event
119122
/// <returns>A delegate which when invoked creates a log message.</returns>
120123
public static Action<ILogger, T1, T2, Exception> Define<T1, T2>(LogLevel logLevel, EventId eventId, string formatString)
121124
{
122-
var formatter = new LogValuesFormatter(formatString);
125+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 2);
123126

124127
return (logger, arg1, arg2, exception) =>
125128
{
@@ -142,7 +145,7 @@ public static Action<ILogger, T1, T2, Exception> Define<T1, T2>(LogLevel logLeve
142145
/// <returns>A delegate which when invoked creates a log message.</returns>
143146
public static Action<ILogger, T1, T2, T3, Exception> Define<T1, T2, T3>(LogLevel logLevel, EventId eventId, string formatString)
144147
{
145-
var formatter = new LogValuesFormatter(formatString);
148+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 3);
146149

147150
return (logger, arg1, arg2, arg3, exception) =>
148151
{
@@ -166,7 +169,7 @@ public static Action<ILogger, T1, T2, T3, Exception> Define<T1, T2, T3>(LogLevel
166169
/// <returns>A delegate which when invoked creates a log message.</returns>
167170
public static Action<ILogger, T1, T2, T3, T4, Exception> Define<T1, T2, T3, T4>(LogLevel logLevel, EventId eventId, string formatString)
168171
{
169-
var formatter = new LogValuesFormatter(formatString);
172+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 4);
170173

171174
return (logger, arg1, arg2, arg3, arg4, exception) =>
172175
{
@@ -191,7 +194,7 @@ public static Action<ILogger, T1, T2, T3, T4, Exception> Define<T1, T2, T3, T4>(
191194
/// <returns>A delegate which when invoked creates a log message.</returns>
192195
public static Action<ILogger, T1, T2, T3, T4, T5, Exception> Define<T1, T2, T3, T4, T5>(LogLevel logLevel, EventId eventId, string formatString)
193196
{
194-
var formatter = new LogValuesFormatter(formatString);
197+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 5);
195198

196199
return (logger, arg1, arg2, arg3, arg4, arg5, exception) =>
197200
{
@@ -217,7 +220,7 @@ public static Action<ILogger, T1, T2, T3, T4, T5, Exception> Define<T1, T2, T3,
217220
/// <returns>A delegate which when invoked creates a log message.</returns>
218221
public static Action<ILogger, T1, T2, T3, T4, T5, T6, Exception> Define<T1, T2, T3, T4, T5, T6>(LogLevel logLevel, EventId eventId, string formatString)
219222
{
220-
var formatter = new LogValuesFormatter(formatString);
223+
var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 6);
221224

222225
return (logger, arg1, arg2, arg3, arg4, arg5, arg6, exception) =>
223226
{
@@ -228,6 +231,20 @@ public static Action<ILogger, T1, T2, T3, T4, T5, T6, Exception> Define<T1, T2,
228231
};
229232
}
230233

234+
private static LogValuesFormatter CreateLogValuesFormatter(string formatString, int expectedNamedParameterCount)
235+
{
236+
var logValuesFormatter = new LogValuesFormatter(formatString);
237+
238+
var actualCount = logValuesFormatter.ValueNames.Count;
239+
if (actualCount != expectedNamedParameterCount)
240+
{
241+
throw new ArgumentException(
242+
Resource.FormatUnexpectedNumberOfNamedParameters(formatString, expectedNamedParameterCount, actualCount));
243+
}
244+
245+
return logValuesFormatter;
246+
}
247+
231248
private class LogValues : IReadOnlyList<KeyValuePair<string, object>>
232249
{
233250
public static Func<object, Exception, string> Callback = (state, exception) => ((LogValues)state)._formatter.Format(((LogValues)state).ToArray());

src/Microsoft.Extensions.Logging.Abstractions/Properties/Resource.Designer.cs

+46
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
<data name="UnexpectedNumberOfNamedParameters" xml:space="preserve">
121+
<value>The format string '{0}' does not have the expected number of named parameters. Expected {1} parameter(s) but found {2} parameter(s).</value>
122+
</data>
123+
</root>

src/Microsoft.Extensions.Logging.Abstractions/project.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
"dependencies": {
2222
"System.Collections": "4.0.11-*",
2323
"System.Collections.Concurrent": "4.0.12-*",
24-
"System.Linq": "4.1.0-*",
24+
"System.Diagnostics.Debug": "4.0.11-*",
2525
"System.Globalization": "4.0.11-*",
26+
"System.Linq": "4.1.0-*",
2627
"System.Reflection": "4.1.0-*",
2728
"System.Resources.ResourceManager": "4.0.1-*",
2829
"System.Runtime.Extensions": "4.1.0-*",
@@ -37,8 +38,9 @@
3738
},
3839
"System.Collections": "4.0.10",
3940
"System.Collections.Concurrent": "4.0.10",
40-
"System.Linq": "4.0.0",
41+
"System.Diagnostics.Debug": "4.0.10",
4142
"System.Globalization": "4.0.10",
43+
"System.Linq": "4.0.0",
4244
"System.Reflection": "4.0.10",
4345
"System.Resources.ResourceManager": "4.0.0",
4446
"System.Runtime.Extensions": "4.0.10",

src/Microsoft.Extensions.Logging.Filter/Microsoft.Extensions.Logging.Filter.xproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
<ProjectGuid>0d190ee0-e305-403d-ac01-dee71d8dbdb5</ProjectGuid>
1010
<RootNamespace>Microsoft.Extensions.Logging.Filter</RootNamespace>
1111
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
12-
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\</OutputPath>
1313
</PropertyGroup>
14-
1514
<PropertyGroup>
1615
<SchemaVersion>2.0</SchemaVersion>
1716
</PropertyGroup>

0 commit comments

Comments
 (0)