Skip to content

Cache trimmed keys for @ and $ #216

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 4 commits into from
Mar 10, 2023
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
17 changes: 15 additions & 2 deletions src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@
using FrameworkLogger = Microsoft.Extensions.Logging.ILogger;
using System.Reflection;
using Serilog.Debugging;
using System.Collections.Concurrent;

namespace Serilog.Extensions.Logging;

class SerilogLogger : FrameworkLogger
{
internal static readonly ConcurrentDictionary<string, string> DestructureDictionary = new();
internal static readonly ConcurrentDictionary<string, string> StringifyDictionary = new();

internal static string GetKeyWithoutFirstSymbol(ConcurrentDictionary<string, string> source, string key)
{
if (source.TryGetValue(key, out var value))
return value;
if (source.Count < 1000)
return source.GetOrAdd(key, k => k.Substring(1));
return key.Substring(1);
}

readonly SerilogLoggerProvider _provider;
readonly ILogger _logger;

Expand Down Expand Up @@ -88,12 +101,12 @@ void Write<TState>(LogEventLevel level, EventId eventId, TState state, Exception
}
else if (property.Key.StartsWith("@"))
{
if (logger.BindProperty(property.Key.Substring(1), property.Value, true, out var destructured))
if (logger.BindProperty(GetKeyWithoutFirstSymbol(DestructureDictionary, property.Key), property.Value, true, out var destructured))
properties.Add(destructured);
}
else if (property.Key.StartsWith("$"))
{
if (logger.BindProperty(property.Key.Substring(1), property.Value?.ToString(), true, out var stringified))
if (logger.BindProperty(GetKeyWithoutFirstSymbol(StringifyDictionary, property.Key), property.Value?.ToString(), true, out var stringified))
properties.Add(stringified);
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Serilog.Core;
using Serilog.Events;

Expand Down Expand Up @@ -57,13 +59,12 @@ void AddProperty(KeyValuePair<string, object> stateProperty)

if (key.StartsWith("@"))
{
key = key.Substring(1);
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.DestructureDictionary, key);
destructureObject = true;
}

if (key.StartsWith("$"))
else if (key.StartsWith("$"))
{
key = key.Substring(1);
key = SerilogLogger.GetKeyWithoutFirstSymbol(SerilogLogger.StringifyDictionary, key);
value = value?.ToString();
}

Expand Down
65 changes: 65 additions & 0 deletions test/Serilog.Extensions.Logging.Benchmarks/LogEventBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2019 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.Extensions.Logging;
using Xunit;
using IMelLogger = Microsoft.Extensions.Logging.ILogger;

#pragma warning disable xUnit1013 // Public method should be marked as test

namespace Serilog.Extensions.Logging.Benchmarks
{
[MemoryDiagnoser]
public class LogEventBenchmark
{
private class Person
{
public string? Name { get; set; }
public int Age { get; set; }
public override string ToString() => "Fixed text";
}

readonly IMelLogger _melLogger;
readonly Person _bob, _alice;

public LogEventBenchmark()
{
var underlyingLogger = new LoggerConfiguration().CreateLogger();
_melLogger = new SerilogLoggerProvider(underlyingLogger).CreateLogger(GetType().FullName!);
_bob = new Person { Name = "Bob", Age = 42 };
_alice = new Person { Name = "Alice", Age = 42 };
}

[Fact]
public void Benchmark()
{
BenchmarkRunner.Run<LogEventBenchmark>();
}

[Benchmark]
public void LogInformation()
{
_melLogger.LogInformation("Hi {@User} from {$Me}", _bob, _alice);
}

[Benchmark]
public void LogInformationScoped()
{
using (var scope = _melLogger.BeginScope("Hi {@User} from {$Me}", _bob, _alice))
_melLogger.LogInformation("Hi");
}
}
}