C
C#14mo ago
Simo

❔ 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
Debug.Log("blah blah" + whatever + "blah blah" + whatever);
Debug.Log("blah blah" + whatever + "blah blah" + whatever);
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
Sossenbinder
Sossenbinder14mo ago
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
Simo
SimoOP14mo ago
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
Sossenbinder
Sossenbinder14mo ago
Effectively only allocating a single string and defering allocation until later I see 👍
Simo
SimoOP14mo ago
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 !
Sossenbinder
Sossenbinder14mo ago
Net core 2 or net 2? Just trying to evaluate how bad off your are catHeyHello
Simo
SimoOP14mo ago
.net 2, old old school
Sossenbinder
Sossenbinder14mo ago
Damn
Simo
SimoOP14mo ago
an assembly running in SQL server that everyone is scared to touch so here I am, up to my neck
Sossenbinder
Sossenbinder14mo ago
Old codebase really isn't an understatement in this case
Simo
SimoOP14mo ago
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
Sossenbinder
Sossenbinder14mo ago
I see, good luck with that 👍
Simo
SimoOP14mo ago
hehe thanks
Jimmacle
Jimmacle14mo ago
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.
Sossenbinder
Sossenbinder14mo ago
Indeed
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server