-
Notifications
You must be signed in to change notification settings - Fork 251
Add Conditional attribute to LogDebug and LogTrace #520
Comments
Calling |
I'm not sure if I understood correctly, what other options I have? All I need is not decorating my code with I believe if you decorate code here with proof of conceptif you will remove the #define MLF
void Main()
{
Log(new Address { State = "FL", City = "Boca Raton" });
}
[Conditional("MLF")]
private void Log(Address address)
{
Console.WriteLine(JsonConvert.SerializeObject(address));
}
public class Address
{
public Address()
{
Console.WriteLine("Initializing address");
}
public string City { get; set; }
public string State { get; set; }
} |
You can add your custom behavior to your programs by adding extension methods that use the |
Sure thing both of those concepts have different parts but this doesn't mean that they don't have common area. Therefore please re-consider to tie LOGGING with DEFINITIONS (not even need to be DEBUG and/or TRACE if you don't want to, simply LOGDEBUG, LOGTRACE, LOGINFORMATION etc. can serve the purpose) This would give us great benefit of eliminating debug code run on production settings as a native way. By the way, thank you for mentioning to implement it as extension method. It solves my need but still I believe native way would have more benefits. 👍
|
+1 on this, at the very least for LogTrace. For performance-sensitive applications, the overhead of repeatedly checking whether the loglevel is enabled can be unacceptable, adding |
Another option is to add separate methods, i.e. |
@roji having separate methods wouldn't be nice. It must be tied with |
@cilerler, what I'm proposing is indeed tied with the The problem is that in many cases it makes sense to log some debug- (and even trace-) level messages even in release builds, just not necessarily all debug/trace messages. |
My bad, didn't get your point at first time, makes sense. 👍 Thank you for the explanation. |
I have to say I agree with @BrennanConroy. Conflating compile time constructs with the logging system isn't correct. You can already do what you want today just not using extension methods, I don't see why we should bake those in. |
@davidfowl (and @BrennanConroy) I don't think anyone's conflating things - I think we fully understand the difference between This is simply about permitting developers to avoid the performance penalty of checking if an event should be logged, for performance-sensitive applications. It makes sense for trace and debug log events to always get logged in debug builds (since performance doesn't matter and we're debugging), but in release the cost may be too high. Can this be implemented via external wrappers rather than baked into the library? Sure. But then the same question can be asked of extensions methods such as LogDebug(), which could be left out (this is just an absurd example). |
I absolutely disagree. One of the features of the logging system is being able to change the level as the application is running. Compiling away log statements is anti this. I think what you want it's a different function altogether and should not be in the core, at least not yet.
I think it should be, at least to begin with. The bar is higher for things going into the main library. If it turns out people want to compile away their statements as a mainline scenario we can look at it. For now I'd suggest making another library with ConditonalBlah log methods. |
Sure, until one starts to remove However, since conditional wrappers for logging can be implemented externally, at least it's possible to leave these out to some external library. |
That's why you use the IsEnabled check. We do this ourselves in hot paths. That's why it should always be there, just switched off until necessary. Having it compiled away takes away some usefulness. There might be scenarios where that's warranted but it's not the mainline use case. |
As I wrote above, the whole point here is that calling IsEnabled is too much of a performance hit in certain scenarios. Is this something that happens in mainstream code? No. But if you're trying to make .NET a high-performance platform, then these things matter. Again, I find it really fine to say that this kind of performance-sensitive app is very rare, and therefore this feature doesn't belong in the main library (especially since it can be implemented outside). But it's important to acknowledge that scenarios that warrant this do exist. |
Show me the numbers. |
In, say, a low-latency algo-trading application, whatever the cost of branching on whether logging is enabled or not, that's one branch that can and should be eliminated. And that's regardless of much that check actually costs. |
I personally use the same construct as provided by NLog, i.e. We have some very high perf code, including code-generated code that we lace with these variants of NLog code, and we teach developers to always carefully think when calling a logging function about "where" they are in the code:
The effects of these logging functions/constructs are multi-fold:
Another niche side-effect is that VS/R# know how to mark the In theory there's no-one/nothing limiting me from making these as extension methods and putting the |
We are closing this issue because no further action is planned for this issue. If you still have any issues or questions, please log a new issue with any additional details that you have. |
LogDebug and LogTrace should be decorated with
Conditional
attribute and tied with defined DEBUG & TRACE in order to not evaluate expensive objects on production.Reproduce steps
project.json
appsettings.json
AddressRepository.cs
Actual result
Evaluates
JsonConvert.SerializeObject(address)
Expected result
It should not evaluate
JsonConvert.SerializeObject(address)
unless DEBUG and/or TRACE is set on appsettings.json.The text was updated successfully, but these errors were encountered: