spring-net icon indicating copy to clipboard operation
spring-net copied to clipboard

Log messages use concatenated strings

Open nzp opened this issue 8 months ago • 5 comments

It seems that Spring.NET universally logs messages using string concatenation and interpolation instead of log formatting templates. This leads to problems when applications use structured logging in general, and in particular when raw message templates are logged.

For example, if we have an NLog layout (snippet) such as this:

{
    "name": "message",
    "layout": "${message}"
},
{
    "name": "messageTemplate",
    "layout": "${message:raw=true}"
}

A resulting log entry would be:

{
  "time": "2025-03-17 15:55:21.5886",
  "level": "DEBUG",
  "logger": "Spring.Objects.Factory.Support.DefaultListableObjectFactory",
  "message": "Invoking IObjectPostProcessors before initialization of object 'System.Data.OracleClient'",
  "messageTemplate": "Invoking IObjectPostProcessors before initialization of object 'System.Data.OracleClient'"
}

instead of the expected:

{
  "time": "2025-03-17 15:55:21.5886",
  "level": "DEBUG",
  "logger": "Spring.Objects.Factory.Support.DefaultListableObjectFactory",
  "message": "Invoking IObjectPostProcessors before initialization of object 'System.Data.OracleClient'",
  "messageTemplate": "Invoking IObjectPostProcessors before initialization of object '{name}'"
}

This makes template logging unusable for Spring logs (one reason to do this would be to easily categorize messages produces by a particular logging call), but, more importantly, could lead to unexpected sensitive data leakages (e.g. database connection string being logged unsanitized via the logged template because the client app does not expect actual values popping up here).

nzp avatar Mar 26 '25 23:03 nzp

This could be quite the undertaking, but PRs are always welcome 👍🏻

lahma avatar Mar 27 '25 05:03 lahma

This could be quite the undertaking, but PRs are always welcome 👍🏻

I can imagine it would be quite some work to do. No promises, but would you be open to partial fixes? Maybe we (I'm writing on behalf of my workplace) could identify the most problematic spots and fix those if nothing else.

nzp avatar Mar 27 '25 11:03 nzp

Partial is always better than nothing, logging fixes can be incremental. As Spring.NET currently uses Common.Logging, will that be a problem?

lahma avatar Mar 27 '25 11:03 lahma

As Spring.NET currently uses Common.Logging, will that be a problem?

It's on my mind, but I'm not sure ATM, especially in our case as it's not really clear to me to what extent Common.Logging plays nice with NLog 5, it would need to be tested. We'll see what can be done.

nzp avatar Mar 27 '25 11:03 nzp

I can take look how switching to Microsoft logging abstraction would work when I have the time, Common.Logging is generally a bit of a dead end.

lahma avatar Mar 27 '25 12:03 lahma

@nzp main branch now uses MS logging abstractions for logging, so should be good to go for any logging template improvements. Might still be some rough edges and improvements needed though. LogManager.LoggerFactory is your entry point.

lahma avatar Mar 28 '25 18:03 lahma

Awesome! Thank you very much for acting on this so quickly! We'll prepare a PR for the template issues, at least the ones that bother us most, as soon as possible.

nzp avatar Mar 31 '25 23:03 nzp