public static ILogger WithCtx()

in src/Middleware/Grpc/Server/CtxLogger.cs [160:198]


    public static ILogger WithCtx(this ILogger logger, ServerCallContext context,
        [CallerFilePath] string callerFilePath = "",
        [CallerLineNumber] int callerLineNumber = 0,
        [CallerMemberName] string callerMemberName = "")
    {
        // Extract JSON string from request headers
        var ctxLogJson = context.RequestHeaders.GetValue("ctxlog-data");

        // Check if json is null or empty
        if (string.IsNullOrEmpty(ctxLogJson))
        {
            return logger;
        }

        // Deserialize the JSON string back to a dictionary with type information
        var dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(ctxLogJson, new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Auto
        });

        // Add dictionary properties to the log context
        if (dictionary != null)
        {
            foreach (var kvp in dictionary)
            {
                logger = logger.ForContext(kvp.Key, kvp.Value, destructureObjects: true);
            }
        }

        // Add caller information to the log context
        var location = new
        {
            function = callerMemberName,
            file = callerFilePath,
            line = callerLineNumber
        };

        return logger.ForContext("location", location, true);
    }