Giter Club home page Giter Club logo

masking.serilog's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

masking.serilog's Issues

Issue with Refit Exception

GIVEN an (internal or external) API
AND this API returns an exception on a certain endpoint
GIVEN a console application with using refit to call this API and this certain endpoint
AND Serilog is used for logging
AND Exception details on Serilog is enabled
AND this library is used for masking logs
THEN catching an exception from this certain endpoint
AND logging this exception

This will give an out of memory exception, because the masking library is trying to log the RefitSettings from the exception to the Exception details in Serilog. Because this RefitSettings contains circular references this keeps on hogging memory until the machines goes out of memory.

In this case it only happens on Refit, but I can imagine other libraries in combination with masking that can cause this.

Failed to deconstruct when using record with inheritence

Synopsis

The masking serilog failed to deconstruct an instance of record which inheriting from another record. Any code calling ILogger interface will make the program stops working completely. Below is the debug messaging log:

2021-05-11T09:06:59.7952117Z The property accessor System.Reflection.GenericParameterAttributes GenericParameterAttributes threw exception System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidOperationException: Method may only be called on a Type for which Type.IsGenericParameter is true.
   at System.RuntimeType.get_GenericParameterAttributes()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at System.Reflection.PropertyInfo.GetValue(Object obj)
   at Masking.Serilog.ByMasking.DestructureByMaskingPolicy.SafeGetPropertyValue(Object o, PropertyInfo pi)
2021-05-11T09:06:59.7975937Z Maximum destructuring depth reached.

Tech detail:

  • dotnet 5
  • Masking.Serilog 1.0.13

Example code

using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Masking.Serilog;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;

Host.CreateDefaultBuilder(args)
    .ConfigureLogging((context, logBuilder) =>
    {
        var logConfig = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .ReadFrom.Configuration(context.Configuration)
            .WriteTo.Console()
            .Destructure.ByMaskingProperties(opt =>
            {
                opt.PropertyNames.Add(nameof(MailConfig.Password));
                opt.Mask = "***";
                opt.ExcludeStaticProperties = true;
            });
        var logger = logConfig.CreateLogger();
        logBuilder.ClearProviders().AddSerilog(logger);
    })
    .ConfigureServices(services => services.AddHostedService<Dummy>())
    .Build().Run();

class Dummy : IHostedService
{
    private readonly ILogger<Dummy> _log;
    public Dummy(ILogger<Dummy> log) => _log = log;

    public Task StartAsync(CancellationToken cancellationToken)
    {
        // enable for error tracing
        Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
        var config = new MailConfig
        {
            Username = "user",
            Password = "secret",
            ServerAddress = "localhost",
            Port = 25,
            UseSecureMode = false
        };
        _log.LogInformation("Dumping my {@Config}", config);
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

record ConnectionConfig
{
    public int Port { get; set; }
    public bool UseSecureMode { get; set; }
}

record MailConfig : ConnectionConfig
{
    public string Username { get; set; } = string.Empty;
    public string Password { get; set; } = string.Empty;
    public string ServerAddress { get; set; } = string.Empty;
}

Workaround

Use ordinal class instead, change ConnectionConfig and MailConfig to use class instead of record.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.