Skip to content

Commit f3be624

Browse files
authored
Merge pull request #79 from serilog/cleanup
file scoped namespace and target typed new
2 parents 94645ab + 56435a3 commit f3be624

File tree

150 files changed

+7303
-7639
lines changed

Some content is hidden

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

150 files changed

+7303
-7639
lines changed

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>

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+
}

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+
}

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

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

15-
using System;
16-
using System.Linq;
15+
namespace Serilog.Expressions.Ast;
1716

18-
namespace Serilog.Expressions.Ast
17+
class ArrayExpression : Expression
1918
{
20-
class ArrayExpression : Expression
19+
public ArrayExpression(Element[] elements)
2120
{
22-
public ArrayExpression(Element[] elements)
23-
{
24-
Elements = elements ?? throw new ArgumentNullException(nameof(elements));
25-
}
21+
Elements = elements ?? throw new ArgumentNullException(nameof(elements));
22+
}
2623

27-
public Element[] Elements { get; }
24+
public Element[] Elements { get; }
2825

29-
public override string ToString()
30-
{
31-
return "[" + string.Join(", ", Elements.Select(o => o.ToString())) + "]";
32-
}
26+
public override string ToString()
27+
{
28+
return "[" + string.Join(", ", Elements.Select(o => o.ToString())) + "]";
3329
}
34-
}
30+
}

0 commit comments

Comments
 (0)