C
C#2y ago
LPeter1997

❔ ✅ Singleton, thread-safe logger with absolutely 0 runtime performance penalty when turned off

I've got an application that lazily discovers its state-space, as it needs it. Carrying through a logger object these state objects seems way too extensive (hence some singleton would be ideal instead of DI), as I'd only like to look at a few isolated cases, I really don't need logging all the time. When I do want to look at a run, I don't mind the performance-penalty, but I can't allow even an empty vcall wasted when I turn it off (99.9% of the time). Are there existing solutions for weird cases like this?
15 Replies
LPeter1997
LPeter19972y ago
Ah also I'm totally fine with turning off the logger at build-time, but I'd like to avoid tying it to a macro at invocation-site each time like
#if LOG
// ...
#endif
#if LOG
// ...
#endif
sibber
sibber2y ago
there's an ifdef attribute that removes the call as well i forgot what it was called its what the Debug class uses
Tvde1
Tvde12y ago
I think Nick Chapsas did a video on it the other day
LPeter1997
LPeter19972y ago
Oh the [Conditional("...")] you mean?
Tvde1
Tvde12y ago
ye
LPeter1997
LPeter19972y ago
Thanks, that's a really good idea Ah lol I even have that in the project, why didn't I think of it Thanks anyway guys !close
Accord
Accord2y ago
Closed!
Tvde1
Tvde12y ago
catOK
arion
arion17mo ago
i must have missed that, if u find the vid again mind linking it to me? AAmaze
Tvde1
Tvde117mo ago
I can't find it, title must've been clickbait so you can't use "Condition attribute". It was a video by a microsoft employee
arion
arion17mo ago
awh
Tvde1
Tvde117mo ago
Hassan Habib
YouTube
Enabling/Disabling Routines in C#
In this session, I'm going to walk you through a quick demo of leveraging [Conditional] capability in C# to enable and disable blocks of code in your program seamlessly and natively. Read more about Conditional here: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.conditionalattribute?view=net-7.0 Here's the code I used for thi...
Tvde1
Tvde117mo ago
terrible SEO haha
arion
arion17mo ago
wow, i learnt something new today, [Conditional()] and #undef I've actually watched vids from that youtuber a few times now, figured i'd drop a sub now, small world though, the man only has around 12k subs I love Nick's work too
Accord
Accord17mo 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.