Skip to content

Commit c6192d6

Browse files
authored
Merge pull request #41 from serilog/dev
3.0.0 Release
2 parents e63a0d5 + f0fcfdc commit c6192d6

File tree

124 files changed

+5344
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+5344
-229
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ _Serilog.Expressions_ adds a number of expression-based overloads and helper met
8282
## Formatting with `ExpressionTemplate`
8383

8484
_Serilog.Expressions_ includes the `ExpressionTemplate` class for text formatting. `ExpressionTemplate` implements `ITextFormatter`, so
85-
it works with any text-based Serilog sink:
85+
it works with any text-based Serilog sink, including `Console`, `File`, `Debug`, and `Email`:
8686

8787
```csharp
8888
// using Serilog.Templates;
@@ -96,7 +96,19 @@ Log.Logger = new LoggerConfiguration()
9696
// [21:21:40 INF (Sample.Program)] Cart contains ["Tea","Coffee"] (first item is Tea)
9797
```
9898

99-
Note the use of `{Cart[0]}`: "holes" in expression templates can include any valid expression over properties from the event.
99+
Templates are based on .NET format strings, and support standard padding, alignment, and format specifiers.
100+
101+
Along with standard properties for the event timestamp (`@t`), level (`@l`) and so on, "holes" in expression templates can include complex
102+
expressions over the first-class properties of the event, like `{SourceContex}` and `{Cart[0]}` in the example..
103+
104+
Templates support customizable color themes when used with the `Console` sink:
105+
106+
```csharp
107+
.WriteTo.Console(new ExpressionTemplate(
108+
"[{@t:HH:mm:ss} {@l:u3}] {@m}\n{@x}", theme: TemplateTheme.Code))
109+
```
110+
111+
![Screenshot showing colored terminal output](https://raw.githubusercontent.com/serilog/serilog-expressions/dev/assets/screenshot.png)
100112

101113
Newline-delimited JSON (for example, replicating the [CLEF format](https://github.com/serilog/serilog-formatting-compact)) can be generated
102114
using object literals:
@@ -112,7 +124,7 @@ using object literals:
112124

113125
The following properties are available in expressions:
114126

115-
* **All first-class properties of the event** — no special syntax: `SourceContext` and `Cart` are used in the formatting examples above
127+
* **All first-class properties of the event** - no special syntax: `SourceContext` and `Cart` are used in the formatting examples above
116128
* `@t` - the event's timestamp, as a `DateTimeOffset`
117129
* `@m` - the rendered message
118130
* `@mt` - the raw message template
@@ -339,8 +351,9 @@ convert the result to plain-old-.NET-types like `string`, `bool`, `Dictionary<K,
339351
User-defined functions can be plugged in by implementing static methods that:
340352

341353
* Return `LogEventPropertyValue?`,
342-
* Have arguments of type `LogEventPropertyValue?`, and
343-
* If the `ci` modifier is supported, accept a `StringComparison` in the first argument position.
354+
* Have arguments of type `LogEventPropertyValue?`,
355+
* If the `ci` modifier is supported, accept a `StringComparison`, and
356+
* If culture-specific formatting or comparisons are used, accepts an `IFormatProvider`.
344357

345358
For example:
346359

@@ -370,3 +383,8 @@ var myFunctions = new StaticMemberNameResolver(typeof(MyFunctions));
370383
var expr = SerilogExpression.Compile("IsHello(User.Name)", new[] { myFunctions });
371384
// Filter events based on whether `User.Name` is `'Hello'` :-)
372385
```
386+
387+
## Acknowledgements
388+
389+
Includes the parser combinator implementation from [Superpower](https://github.com/datalust/superpower), copyright Datalust,
390+
Superpower Contributors, and Sprache Contributors; licensed under the Apache License, 2.0.

assets/screenshot.png

56.6 KB
Loading

example/Sample/Program.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using Serilog;
34
using Serilog.Debugging;
45
using Serilog.Templates;
6+
using Serilog.Templates.Themes;
57

68
namespace Sample
79
{
@@ -24,7 +26,8 @@ static void TextFormattingExample1()
2426
.WriteTo.Console(new ExpressionTemplate(
2527
"[{@t:HH:mm:ss} {@l:u3}" +
2628
"{#if SourceContext is not null} ({Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}){#end}] " +
27-
"{@m} (first item is {coalesce(Items[0], '<empty>')})\n{@x}"))
29+
"{@m} (first item is {coalesce(Items[0], '<empty>')})\n{@x}",
30+
theme: TemplateTheme.Code))
2831
.CreateLogger();
2932

3033
log.Information("Running {Example}", nameof(TextFormattingExample1));
@@ -41,7 +44,7 @@ static void JsonFormattingExample()
4144
using var log = new LoggerConfiguration()
4245
.Enrich.WithProperty("Application", "Example")
4346
.WriteTo.Console(new ExpressionTemplate(
44-
"{ {@t, @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n"))
47+
"{ {@t: UtcDateTime(@t), @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n"))
4548
.CreateLogger();
4649

4750
log.Information("Running {Example}", nameof(JsonFormattingExample));
@@ -75,23 +78,35 @@ static void PipelineComponentExample()
7578

7679
static void TextFormattingExample2()
7780
{
81+
// Emulates `Microsoft.Extensions.Logging`'s `ConsoleLogger`.
82+
83+
var melon = new TemplateTheme(TemplateTheme.Literate, new Dictionary<TemplateThemeStyle, string>
84+
{
85+
// `Information` is dark green in MEL.
86+
[TemplateThemeStyle.LevelInformation] = "\x1b[38;5;34m",
87+
[TemplateThemeStyle.String] = "\x1b[38;5;159m",
88+
[TemplateThemeStyle.Number] = "\x1b[38;5;159m"
89+
});
90+
7891
using var log = new LoggerConfiguration()
7992
.WriteTo.Console(new ExpressionTemplate(
8093
"{@l:w4}: {SourceContext}\n" +
8194
"{#if Scope is not null}" +
8295
" {#each s in Scope}=> {s}{#delimit} {#end}\n" +
8396
"{#end}" +
8497
" {@m}\n" +
85-
"{@x}"))
98+
"{@x}",
99+
theme: melon))
86100
.CreateLogger();
87101

88102
var program = log.ForContext<Program>();
89-
program.Information("Starting up");
103+
program.Information("Host listening at {ListenUri}", "https://hello-world.local");
90104

91-
// Emulate data produced by the Serilog.AspNetCore integration
92-
var scoped = program.ForContext("Scope", new[] {"Main", "TextFormattingExample2()"});
105+
program
106+
.ForContext("Scope", new[] {"Main", "TextFormattingExample2()"})
107+
.Information("HTTP {Method} {Path} responded {StatusCode} in {Elapsed:0.000} ms", "GET", "/api/hello", 200, 1.23);
93108

94-
scoped.Information("Hello, world!");
109+
program.Warning("We've reached the end of the line");
95110
}
96111
}
97112
}

example/Sample/Sample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
<OutputType>Exe</OutputType>
66
</PropertyGroup>
77

serilog-expressions.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
22
<s:Boolean x:Key="/Default/UserDictionary/Words/=Acerola/@EntryIndexedValue">True</s:Boolean>
33
<s:Boolean x:Key="/Default/UserDictionary/Words/=Comparand/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/UserDictionary/Words/=delim/@EntryIndexedValue">True</s:Boolean>
45
<s:Boolean x:Key="/Default/UserDictionary/Words/=Enricher/@EntryIndexedValue">True</s:Boolean>
56
<s:Boolean x:Key="/Default/UserDictionary/Words/=Evaluatable/@EntryIndexedValue">True</s:Boolean>
67
<s:Boolean x:Key="/Default/UserDictionary/Words/=Existentials/@EntryIndexedValue">True</s:Boolean>
78
<s:Boolean x:Key="/Default/UserDictionary/Words/=formattable/@EntryIndexedValue">True</s:Boolean>
89
<s:Boolean x:Key="/Default/UserDictionary/Words/=nblumhardt/@EntryIndexedValue">True</s:Boolean>
10+
<s:Boolean x:Key="/Default/UserDictionary/Words/=ogham/@EntryIndexedValue">True</s:Boolean>
911
<s:Boolean x:Key="/Default/UserDictionary/Words/=Reorderable/@EntryIndexedValue">True</s:Boolean>
1012
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean>
1113
<s:Boolean x:Key="/Default/UserDictionary/Words/=Subproperties/@EntryIndexedValue">True</s:Boolean>

src/Serilog.Expressions/Expressions/Ast/AccessorExpression.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// Copyright © 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;
216

317
namespace Serilog.Expressions.Ast
418
{

src/Serilog.Expressions/Expressions/Ast/AmbientNameExpression.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1-
using System;
1+
// Copyright © 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;
216

317
namespace Serilog.Expressions.Ast
418
{
519
class AmbientNameExpression : Expression
620
{
721
readonly bool _requiresEscape;
822

9-
public AmbientNameExpression(string Name, bool isBuiltIn)
23+
public AmbientNameExpression(string name, bool isBuiltIn)
1024
{
11-
PropertyName = Name ?? throw new ArgumentNullException(nameof(Name));
25+
PropertyName = name ?? throw new ArgumentNullException(nameof(name));
1226
IsBuiltIn = isBuiltIn;
13-
_requiresEscape = !SerilogExpression.IsValidIdentifier(Name);
27+
_requiresEscape = !SerilogExpression.IsValidIdentifier(name);
1428
}
1529

1630
public string PropertyName { get; }

src/Serilog.Expressions/Expressions/Ast/ArrayExpression.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// Copyright © 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;
216
using System.Linq;
317

418
namespace Serilog.Expressions.Ast

src/Serilog.Expressions/Expressions/Ast/CallExpression.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// Copyright © 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;
216
using System.Linq;
317

418
namespace Serilog.Expressions.Ast

src/Serilog.Expressions/Expressions/Ast/ConstantExpression.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// Copyright © 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;
216
using System.Globalization;
317
using Serilog.Events;
418

@@ -28,7 +42,7 @@ public override string ToString()
2842
case IFormattable formattable:
2943
return formattable.ToString(null, CultureInfo.InvariantCulture);
3044
default:
31-
return (sv.Value ?? "null").ToString();
45+
return (sv.Value ?? "null").ToString() ?? "<ToString() returned null>";
3246
}
3347
}
3448

src/Serilog.Expressions/Expressions/Ast/Element.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
namespace Serilog.Expressions.Ast
1+
// Copyright © 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 Serilog.Expressions.Ast
216
{
317
abstract class Element
418
{

src/Serilog.Expressions/Expressions/Ast/Expression.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
namespace Serilog.Expressions.Ast
1+
// Copyright © 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 Serilog.Expressions.Ast
216
{
317
abstract class Expression
418
{

src/Serilog.Expressions/Expressions/Ast/IndexOfMatchExpression.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright © 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+
115
using System;
216
using System.Text.RegularExpressions;
317

src/Serilog.Expressions/Expressions/Ast/IndexerExpression.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright © 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+
115
namespace Serilog.Expressions.Ast
216
{
317
class IndexerExpression : Expression
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
namespace Serilog.Expressions.Ast
1+
// Copyright © 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 Serilog.Expressions.Ast
216
{
317
enum IndexerWildcard { Undefined, Any, All }
418
}

src/Serilog.Expressions/Expressions/Ast/IndexerWildcardExpression.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// Copyright © 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;
216

317
namespace Serilog.Expressions.Ast
418
{

0 commit comments

Comments
 (0)