Skip to content

Commit 25c6509

Browse files
authored
Merge pull request #78 from serilog/dev
3.4.1 Release
2 parents 0ee44d1 + 56f298e commit 25c6509

File tree

157 files changed

+7361
-7637
lines changed

Some content is hidden

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

157 files changed

+7361
-7637
lines changed

Build.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ echo "build: Build started"
33
Push-Location $PSScriptRoot
44

55
if(Test-Path .\artifacts) {
6-
echo "build: Cleaning .\artifacts"
7-
Remove-Item .\artifacts -Force -Recurse
6+
echo "build: Cleaning ./artifacts"
7+
Remove-Item ./artifacts -Force -Recurse
88
}
99

1010
& dotnet restore --no-cache
@@ -18,24 +18,24 @@ $buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($c
1818
echo "build: Package version suffix is $suffix"
1919
echo "build: Build version suffix is $buildSuffix"
2020

21-
foreach ($src in ls src/*) {
21+
foreach ($src in gci src/*) {
2222
Push-Location $src
2323

2424
echo "build: Packaging project in $src"
2525

2626
& dotnet build -c Release --version-suffix=$buildSuffix
2727

2828
if($suffix) {
29-
& dotnet pack -c Release --include-source --no-build -o ..\..\artifacts --version-suffix=$suffix
29+
& dotnet pack -c Release --include-source --no-build -o ../../artifacts --version-suffix=$suffix
3030
} else {
31-
& dotnet pack -c Release --include-source --no-build -o ..\..\artifacts
31+
& dotnet pack -c Release --include-source --no-build -o ../../artifacts
3232
}
3333
if($LASTEXITCODE -ne 0) { exit 1 }
3434

3535
Pop-Location
3636
}
3737

38-
foreach ($test in ls test/*.Tests) {
38+
foreach ($test in gci test/*.Tests) {
3939
Push-Location $test
4040

4141
echo "build: Testing project in $test"

Directory.Build.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<?xml version="1.0" encoding="utf-8"?>
21
<Project>
32
<PropertyGroup>
43
<Nullable>enable</Nullable>
@@ -7,5 +6,7 @@
76
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
87
<SignAssembly>true</SignAssembly>
98
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
9+
<CheckEolTargetFramework>false</CheckEolTargetFramework>
10+
<ImplicitUsings>enable</ImplicitUsings>
1011
</PropertyGroup>
11-
</Project>
12+
</Project>

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)
279279

280280
This expression takes advantage of `LastIndexOf()` returning -1 when no `.` character appears in `SourceContext`, to yield a `startIndex` of 0 in that case.
281281

282+
**Write not-referenced context properties (only if there are any):**
283+
284+
```
285+
{#if rest(true) <> {}} <Context: {rest(true)}>{#end}
286+
```
287+
282288
**Access a property with a non-identifier name:**
283289

284290
```

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
version: '{build}'
22
skip_tags: true
3-
image: Visual Studio 2019
3+
image: Visual Studio 2022
44
build_script:
5-
- ps: ./Build.ps1
5+
- pwsh: ./Build.ps1
66
artifacts:
77
- path: artifacts/Serilog.*.nupkg
88
deploy:

example/Sample/Program.cs

Lines changed: 93 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,110 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using Serilog;
1+
using Serilog;
42
using Serilog.Debugging;
53
using Serilog.Templates;
64
using Serilog.Templates.Themes;
75

8-
namespace Sample
6+
namespace Sample;
7+
8+
// ReSharper disable once ClassNeverInstantiated.Global
9+
public class Program
910
{
10-
// ReSharper disable once ClassNeverInstantiated.Global
11-
public class Program
11+
public static void Main()
1212
{
13-
public static void Main()
14-
{
15-
SelfLog.Enable(Console.Error);
16-
17-
TextFormattingExample1();
18-
JsonFormattingExample();
19-
PipelineComponentExample();
20-
TextFormattingExample2();
21-
}
22-
23-
static void TextFormattingExample1()
24-
{
25-
using var log = new LoggerConfiguration()
26-
.Enrich.WithProperty("Application", "Sample")
27-
.WriteTo.Console(new ExpressionTemplate(
28-
"[{@t:HH:mm:ss} {@l:u3}" +
29-
"{#if SourceContext is not null} ({Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}){#end}] " +
30-
"{@m} (first item is {coalesce(Items[0], '<empty>')}) {rest()}\n{@x}",
31-
theme: TemplateTheme.Code))
32-
.CreateLogger();
13+
SelfLog.Enable(Console.Error);
3314

34-
log.Information("Running {Example}", nameof(TextFormattingExample1));
35-
36-
log.ForContext<Program>()
37-
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
38-
39-
log.ForContext<Program>()
40-
.Information("Cart contains {@Items}", new[] { "Apricots" });
41-
}
42-
43-
static void JsonFormattingExample()
44-
{
45-
using var log = new LoggerConfiguration()
46-
.Enrich.WithProperty("Application", "Example")
47-
.WriteTo.Console(new ExpressionTemplate(
48-
"{ {@t: UtcDateTime(@t), @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n"))
49-
.CreateLogger();
15+
TextFormattingExample1();
16+
JsonFormattingExample();
17+
PipelineComponentExample();
18+
TextFormattingExample2();
19+
}
5020

51-
log.Information("Running {Example}", nameof(JsonFormattingExample));
21+
static void TextFormattingExample1()
22+
{
23+
using var log = new LoggerConfiguration()
24+
.Enrich.WithProperty("Application", "Sample")
25+
.WriteTo.Console(new ExpressionTemplate(
26+
"[{@t:HH:mm:ss} {@l:u3}" +
27+
"{#if SourceContext is not null} ({Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}){#end}] " +
28+
"{@m} (first item is {coalesce(Items[0], '<empty>')}) {rest()}\n{@x}",
29+
theme: TemplateTheme.Code))
30+
.CreateLogger();
31+
32+
log.Information("Running {Example}", nameof(TextFormattingExample1));
33+
34+
log.ForContext<Program>()
35+
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
36+
37+
log.ForContext<Program>()
38+
.Information("Cart contains {@Items}", new[] { "Apricots" });
39+
}
5240

53-
log.ForContext<Program>()
54-
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
41+
static void JsonFormattingExample()
42+
{
43+
using var log = new LoggerConfiguration()
44+
.Enrich.WithProperty("Application", "Example")
45+
.WriteTo.Console(new ExpressionTemplate(
46+
"{ {@t: UtcDateTime(@t), @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n"))
47+
.CreateLogger();
5548

56-
log.ForContext<Program>()
57-
.Warning("Cart is empty");
58-
}
49+
log.Information("Running {Example}", nameof(JsonFormattingExample));
5950

60-
static void PipelineComponentExample()
61-
{
62-
using var log = new LoggerConfiguration()
63-
.Enrich.WithProperty("Application", "Example")
64-
.Enrich.WithComputed("FirstItem", "coalesce(Items[0], '<empty>')")
65-
.Enrich.WithComputed("SourceContext", "coalesce(Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), '<no source>')")
66-
.Filter.ByIncludingOnly("Items is null or Items[?] like 'C%'")
67-
.WriteTo.Console(outputTemplate:
68-
"[{Timestamp:HH:mm:ss} {Level:u3} ({SourceContext})] {Message:lj} (first item is {FirstItem}){NewLine}{Exception}")
69-
.CreateLogger();
51+
log.ForContext<Program>()
52+
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
7053

71-
log.Information("Running {Example}", nameof(PipelineComponentExample));
54+
log.ForContext<Program>()
55+
.Warning("Cart is empty");
56+
}
7257

73-
log.ForContext<Program>()
74-
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
58+
static void PipelineComponentExample()
59+
{
60+
using var log = new LoggerConfiguration()
61+
.Enrich.WithProperty("Application", "Example")
62+
.Enrich.WithComputed("FirstItem", "coalesce(Items[0], '<empty>')")
63+
.Enrich.WithComputed("SourceContext", "coalesce(Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), '<no source>')")
64+
.Filter.ByIncludingOnly("Items is null or Items[?] like 'C%'")
65+
.WriteTo.Console(outputTemplate:
66+
"[{Timestamp:HH:mm:ss} {Level:u3} ({SourceContext})] {Message:lj} (first item is {FirstItem}){NewLine}{Exception}")
67+
.CreateLogger();
68+
69+
log.Information("Running {Example}", nameof(PipelineComponentExample));
70+
71+
log.ForContext<Program>()
72+
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });
73+
74+
log.ForContext<Program>()
75+
.Information("Cart contains {@Items}", new[] { "Apricots" });
76+
}
7577

76-
log.ForContext<Program>()
77-
.Information("Cart contains {@Items}", new[] { "Apricots" });
78-
}
78+
static void TextFormattingExample2()
79+
{
80+
// Emulates `Microsoft.Extensions.Logging`'s `ConsoleLogger`.
7981

80-
static void TextFormattingExample2()
82+
var melon = new TemplateTheme(TemplateTheme.Literate, new Dictionary<TemplateThemeStyle, string>
8183
{
82-
// Emulates `Microsoft.Extensions.Logging`'s `ConsoleLogger`.
83-
84-
var melon = new TemplateTheme(TemplateTheme.Literate, new Dictionary<TemplateThemeStyle, string>
85-
{
86-
// `Information` is dark green in MEL.
87-
[TemplateThemeStyle.LevelInformation] = "\x1b[38;5;34m",
88-
[TemplateThemeStyle.String] = "\x1b[38;5;159m",
89-
[TemplateThemeStyle.Number] = "\x1b[38;5;159m"
90-
});
91-
92-
using var log = new LoggerConfiguration()
93-
.WriteTo.Console(new ExpressionTemplate(
94-
"{@l:w4}: {SourceContext}\n" +
95-
"{#if Scope is not null}" +
96-
" {#each s in Scope}=> {s}{#delimit} {#end}\n" +
97-
"{#end}" +
98-
" {@m}\n" +
99-
"{@x}",
100-
theme: melon))
101-
.CreateLogger();
102-
103-
var program = log.ForContext<Program>();
104-
program.Information("Host listening at {ListenUri}", "https://hello-world.local");
105-
106-
program
107-
.ForContext("Scope", new[] {"Main", "TextFormattingExample2()"})
108-
.Information("HTTP {Method} {Path} responded {StatusCode} in {Elapsed:0.000} ms", "GET", "/api/hello", 200, 1.23);
109-
110-
program.Warning("We've reached the end of the line");
111-
}
84+
// `Information` is dark green in MEL.
85+
[TemplateThemeStyle.LevelInformation] = "\x1b[38;5;34m",
86+
[TemplateThemeStyle.String] = "\x1b[38;5;159m",
87+
[TemplateThemeStyle.Number] = "\x1b[38;5;159m"
88+
});
89+
90+
using var log = new LoggerConfiguration()
91+
.WriteTo.Console(new ExpressionTemplate(
92+
"{@l:w4}: {SourceContext}\n" +
93+
"{#if Scope is not null}" +
94+
" {#each s in Scope}=> {s}{#delimit} {#end}\n" +
95+
"{#end}" +
96+
" {@m}\n" +
97+
"{@x}",
98+
theme: melon))
99+
.CreateLogger();
100+
101+
var program = log.ForContext<Program>();
102+
program.Information("Host listening at {ListenUri}", "https://hello-world.local");
103+
104+
program
105+
.ForContext("Scope", new[] {"Main", "TextFormattingExample2()"})
106+
.Information("HTTP {Method} {Path} responded {StatusCode} in {Elapsed:0.000} ms", "GET", "/api/hello", 200, 1.23);
107+
108+
program.Warning("We've reached the end of the line");
112109
}
113-
}
110+
}

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>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<OutputType>Exe</OutputType>
66
</PropertyGroup>
77

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,25 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using System;
15+
namespace Serilog.Expressions.Ast;
1616

17-
namespace Serilog.Expressions.Ast
17+
class AccessorExpression : Expression
1818
{
19-
class AccessorExpression : Expression
19+
public AccessorExpression(Expression receiver, string memberName)
2020
{
21-
public AccessorExpression(Expression receiver, string memberName)
22-
{
23-
MemberName = memberName ?? throw new ArgumentNullException(nameof(memberName));
24-
Receiver = receiver;
25-
}
21+
MemberName = memberName ?? throw new ArgumentNullException(nameof(memberName));
22+
Receiver = receiver;
23+
}
2624

27-
public string MemberName { get; }
25+
public string MemberName { get; }
2826

29-
public Expression Receiver { get; }
27+
public Expression Receiver { get; }
3028

31-
public override string ToString()
32-
{
33-
if (SerilogExpression.IsValidIdentifier(MemberName))
34-
return Receiver + "." + MemberName;
29+
public override string ToString()
30+
{
31+
if (SerilogExpression.IsValidIdentifier(MemberName))
32+
return Receiver + "." + MemberName;
3533

36-
return $"{Receiver}['{SerilogExpression.EscapeStringContent(MemberName)}']";
37-
}
34+
return $"{Receiver}['{SerilogExpression.EscapeStringContent(MemberName)}']";
3835
}
39-
}
36+
}

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

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,28 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using System;
15+
namespace Serilog.Expressions.Ast;
1616

17-
namespace Serilog.Expressions.Ast
17+
class AmbientNameExpression : Expression
1818
{
19-
class AmbientNameExpression : Expression
20-
{
21-
readonly bool _requiresEscape;
19+
readonly bool _requiresEscape;
2220

23-
public AmbientNameExpression(string name, bool isBuiltIn)
24-
{
25-
PropertyName = name ?? throw new ArgumentNullException(nameof(name));
26-
IsBuiltIn = isBuiltIn;
27-
_requiresEscape = !SerilogExpression.IsValidIdentifier(name);
28-
}
21+
public AmbientNameExpression(string name, bool isBuiltIn)
22+
{
23+
PropertyName = name ?? throw new ArgumentNullException(nameof(name));
24+
IsBuiltIn = isBuiltIn;
25+
_requiresEscape = !SerilogExpression.IsValidIdentifier(name);
26+
}
2927

30-
public string PropertyName { get; }
28+
public string PropertyName { get; }
3129

32-
public bool IsBuiltIn { get; }
30+
public bool IsBuiltIn { get; }
3331

34-
public override string ToString()
35-
{
36-
if (_requiresEscape)
37-
return $"@Properties['{SerilogExpression.EscapeStringContent(PropertyName)}']";
32+
public override string ToString()
33+
{
34+
if (_requiresEscape)
35+
return $"@Properties['{SerilogExpression.EscapeStringContent(PropertyName)}']";
3836

39-
return (IsBuiltIn ? "@" : "") + PropertyName;
40-
}
37+
return (IsBuiltIn ? "@" : "") + PropertyName;
4138
}
42-
}
39+
}

0 commit comments

Comments
 (0)