Skip to content

file scoped namespace and target typed new #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
Expand All @@ -7,5 +6,7 @@
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
</Project>
189 changes: 93 additions & 96 deletions example/Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,113 +1,110 @@
using System;
using System.Collections.Generic;
using Serilog;
using Serilog;
using Serilog.Debugging;
using Serilog.Templates;
using Serilog.Templates.Themes;

namespace Sample
namespace Sample;

// ReSharper disable once ClassNeverInstantiated.Global
public class Program
{
// ReSharper disable once ClassNeverInstantiated.Global
public class Program
public static void Main()
{
public static void Main()
{
SelfLog.Enable(Console.Error);

TextFormattingExample1();
JsonFormattingExample();
PipelineComponentExample();
TextFormattingExample2();
}

static void TextFormattingExample1()
{
using var log = new LoggerConfiguration()
.Enrich.WithProperty("Application", "Sample")
.WriteTo.Console(new ExpressionTemplate(
"[{@t:HH:mm:ss} {@l:u3}" +
"{#if SourceContext is not null} ({Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}){#end}] " +
"{@m} (first item is {coalesce(Items[0], '<empty>')}) {rest()}\n{@x}",
theme: TemplateTheme.Code))
.CreateLogger();
SelfLog.Enable(Console.Error);

log.Information("Running {Example}", nameof(TextFormattingExample1));

log.ForContext<Program>()
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });

log.ForContext<Program>()
.Information("Cart contains {@Items}", new[] { "Apricots" });
}

static void JsonFormattingExample()
{
using var log = new LoggerConfiguration()
.Enrich.WithProperty("Application", "Example")
.WriteTo.Console(new ExpressionTemplate(
"{ {@t: UtcDateTime(@t), @mt, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n"))
.CreateLogger();
TextFormattingExample1();
JsonFormattingExample();
PipelineComponentExample();
TextFormattingExample2();
}

log.Information("Running {Example}", nameof(JsonFormattingExample));
static void TextFormattingExample1()
{
using var log = new LoggerConfiguration()
.Enrich.WithProperty("Application", "Sample")
.WriteTo.Console(new ExpressionTemplate(
"[{@t:HH:mm:ss} {@l:u3}" +
"{#if SourceContext is not null} ({Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}){#end}] " +
"{@m} (first item is {coalesce(Items[0], '<empty>')}) {rest()}\n{@x}",
theme: TemplateTheme.Code))
.CreateLogger();

log.Information("Running {Example}", nameof(TextFormattingExample1));

log.ForContext<Program>()
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });

log.ForContext<Program>()
.Information("Cart contains {@Items}", new[] { "Apricots" });
}

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

log.ForContext<Program>()
.Warning("Cart is empty");
}
log.Information("Running {Example}", nameof(JsonFormattingExample));

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

log.Information("Running {Example}", nameof(PipelineComponentExample));
log.ForContext<Program>()
.Warning("Cart is empty");
}

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

log.Information("Running {Example}", nameof(PipelineComponentExample));

log.ForContext<Program>()
.Information("Cart contains {@Items}", new[] { "Tea", "Coffee" });

log.ForContext<Program>()
.Information("Cart contains {@Items}", new[] { "Apricots" });
}

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

static void TextFormattingExample2()
var melon = new TemplateTheme(TemplateTheme.Literate, new Dictionary<TemplateThemeStyle, string>
{
// Emulates `Microsoft.Extensions.Logging`'s `ConsoleLogger`.

var melon = new TemplateTheme(TemplateTheme.Literate, new Dictionary<TemplateThemeStyle, string>
{
// `Information` is dark green in MEL.
[TemplateThemeStyle.LevelInformation] = "\x1b[38;5;34m",
[TemplateThemeStyle.String] = "\x1b[38;5;159m",
[TemplateThemeStyle.Number] = "\x1b[38;5;159m"
});

using var log = new LoggerConfiguration()
.WriteTo.Console(new ExpressionTemplate(
"{@l:w4}: {SourceContext}\n" +
"{#if Scope is not null}" +
" {#each s in Scope}=> {s}{#delimit} {#end}\n" +
"{#end}" +
" {@m}\n" +
"{@x}",
theme: melon))
.CreateLogger();

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

program
.ForContext("Scope", new[] {"Main", "TextFormattingExample2()"})
.Information("HTTP {Method} {Path} responded {StatusCode} in {Elapsed:0.000} ms", "GET", "/api/hello", 200, 1.23);

program.Warning("We've reached the end of the line");
}
// `Information` is dark green in MEL.
[TemplateThemeStyle.LevelInformation] = "\x1b[38;5;34m",
[TemplateThemeStyle.String] = "\x1b[38;5;159m",
[TemplateThemeStyle.Number] = "\x1b[38;5;159m"
});

using var log = new LoggerConfiguration()
.WriteTo.Console(new ExpressionTemplate(
"{@l:w4}: {SourceContext}\n" +
"{#if Scope is not null}" +
" {#each s in Scope}=> {s}{#delimit} {#end}\n" +
"{#end}" +
" {@m}\n" +
"{@x}",
theme: melon))
.CreateLogger();

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

program
.ForContext("Scope", new[] {"Main", "TextFormattingExample2()"})
.Information("HTTP {Method} {Path} responded {StatusCode} in {Elapsed:0.000} ms", "GET", "/api/hello", 200, 1.23);

program.Warning("We've reached the end of the line");
}
}
}
31 changes: 14 additions & 17 deletions src/Serilog.Expressions/Expressions/Ast/AccessorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
namespace Serilog.Expressions.Ast;

namespace Serilog.Expressions.Ast
class AccessorExpression : Expression
{
class AccessorExpression : Expression
public AccessorExpression(Expression receiver, string memberName)
{
public AccessorExpression(Expression receiver, string memberName)
{
MemberName = memberName ?? throw new ArgumentNullException(nameof(memberName));
Receiver = receiver;
}
MemberName = memberName ?? throw new ArgumentNullException(nameof(memberName));
Receiver = receiver;
}

public string MemberName { get; }
public string MemberName { get; }

public Expression Receiver { get; }
public Expression Receiver { get; }

public override string ToString()
{
if (SerilogExpression.IsValidIdentifier(MemberName))
return Receiver + "." + MemberName;
public override string ToString()
{
if (SerilogExpression.IsValidIdentifier(MemberName))
return Receiver + "." + MemberName;

return $"{Receiver}['{SerilogExpression.EscapeStringContent(MemberName)}']";
}
return $"{Receiver}['{SerilogExpression.EscapeStringContent(MemberName)}']";
}
}
}
37 changes: 17 additions & 20 deletions src/Serilog.Expressions/Expressions/Ast/AmbientNameExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,28 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
namespace Serilog.Expressions.Ast;

namespace Serilog.Expressions.Ast
class AmbientNameExpression : Expression
{
class AmbientNameExpression : Expression
{
readonly bool _requiresEscape;
readonly bool _requiresEscape;

public AmbientNameExpression(string name, bool isBuiltIn)
{
PropertyName = name ?? throw new ArgumentNullException(nameof(name));
IsBuiltIn = isBuiltIn;
_requiresEscape = !SerilogExpression.IsValidIdentifier(name);
}
public AmbientNameExpression(string name, bool isBuiltIn)
{
PropertyName = name ?? throw new ArgumentNullException(nameof(name));
IsBuiltIn = isBuiltIn;
_requiresEscape = !SerilogExpression.IsValidIdentifier(name);
}

public string PropertyName { get; }
public string PropertyName { get; }

public bool IsBuiltIn { get; }
public bool IsBuiltIn { get; }

public override string ToString()
{
if (_requiresEscape)
return $"@Properties['{SerilogExpression.EscapeStringContent(PropertyName)}']";
public override string ToString()
{
if (_requiresEscape)
return $"@Properties['{SerilogExpression.EscapeStringContent(PropertyName)}']";

return (IsBuiltIn ? "@" : "") + PropertyName;
}
return (IsBuiltIn ? "@" : "") + PropertyName;
}
}
}
24 changes: 10 additions & 14 deletions src/Serilog.Expressions/Expressions/Ast/ArrayExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Linq;
namespace Serilog.Expressions.Ast;

namespace Serilog.Expressions.Ast
class ArrayExpression : Expression
{
class ArrayExpression : Expression
public ArrayExpression(Element[] elements)
{
public ArrayExpression(Element[] elements)
{
Elements = elements ?? throw new ArgumentNullException(nameof(elements));
}
Elements = elements ?? throw new ArgumentNullException(nameof(elements));
}

public Element[] Elements { get; }
public Element[] Elements { get; }

public override string ToString()
{
return "[" + string.Join(", ", Elements.Select(o => o.ToString())) + "]";
}
public override string ToString()
{
return "[" + string.Join(", ", Elements.Select(o => o.ToString())) + "]";
}
}
}
Loading