Logging fields with type long using logback-ecs-encoder

I'm adding request/response logging to my Spring Boot application and I want to add, for example, the field http.request.body.bytes which is of type long.

Currently we are adding the values we want to in MDC, logging and then immediately removing it from MDC

MDC.put("http.request.body.bytes", "2513");
log.info("Http request");
MDC.remove("http.request.body.bytes");

Are we misusing the purpose of MDC class? How do I do this and get it in number type which is the correct type according to ECS field documentation?

The MDC is usually used to pass information down the call-stack as "implicit arguments" to be attached to every log message. E.g. usually things like the username for a request are put into the MDC, so that all logs for that request processing include the username.

Your use-case seems more of a workaround for adding additional fields in addition to the message to the logs. So if possible you should look into structured logging instead, which also gives you the option to preserve the types of values you add. The MDC API works only with strings.