❔ DIY logging methods
I'm working on an old old codebase and I've noticed it has effectively implemented its own logging class, to append timestamps and so on.
The code has
all through it, and if debugging is turned off, the Log method just does nothing with the string. - this does get called maybe 800,000 times in a day, but mostly with the logging turned off.
Am I right in saying that the calling code will allocate all the strings every time, even when the debug is turned off internally? i.e. it would be more efficient to not call debug in the first place, so we dont allocate that string 800,000 times day?
15 Replies
This one will allocate the string, yes
It's common to use string formatting for this purpose
E.g. _logger.LogInformation("Item with {id} was added", id);
Since the replacement would then only take place when the log level is right
This is a .net 2 dll which I believe I can migrate to .net 4 now, but I want to fix a bunch of key issues before that upgrade
Effectively only allocating a single string and defering allocation until later
I see 👍
that is neat, I would normally use constants for logging in my .net core projects, but that is a benefit I hadn't realised
ok thank you, I will currently just not call the debug log as I'm trapped in .net 2 right now
thanks !
Net core 2 or net 2?
Just trying to evaluate how bad off your are
.net 2, old old school
Damn
an assembly running in SQL server that everyone is scared to touch
so here I am, up to my neck
Old codebase really isn't an understatement in this case
yeah
but I beleive newer SQL server versions support .net 4.x framework in this context, so big scope to optimise the crap out of this sloppily written DLL
fix the logging, string interp etc etc
I see, good luck with that 👍
hehe thanks
it's also used because it supports structured logging
as in, log messages are represented as a template and parameters so you have more flexibility in aggregating logs in other services etc.
Indeed
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.