C
C#9mo 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
Sossenbinder9mo 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
Simo9mo 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
Sossenbinder9mo ago
Effectively only allocating a single string and defering allocation until later I see 👍
Simo
Simo9mo 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
Sossenbinder9mo ago
Net core 2 or net 2? Just trying to evaluate how bad off your are catHeyHello
Simo
Simo9mo ago
.net 2, old old school
Sossenbinder
Sossenbinder9mo ago
Damn
Simo
Simo9mo ago
an assembly running in SQL server that everyone is scared to touch so here I am, up to my neck
Sossenbinder
Sossenbinder9mo ago
Old codebase really isn't an understatement in this case
Simo
Simo9mo 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
Sossenbinder9mo ago
I see, good luck with that 👍
Simo
Simo9mo ago
hehe thanks
Jimmacle
Jimmacle9mo 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
Sossenbinder9mo ago
Indeed
Accord
Accord9mo 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.