Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal logger calls impact when it is disabled #56

Closed
luigiberrettini opened this issue Jan 11, 2017 · 24 comments
Closed

Internal logger calls impact when it is disabled #56

luigiberrettini opened this issue Jan 11, 2017 · 24 comments
Assignees
Milestone

Comments

@luigiberrettini
Copy link
Owner

Provide a way to disable internal logger calls and message creation when the internal logger / that level of internal logging is disabled.

@luigiberrettini luigiberrettini modified the milestone: 3.1 Jan 11, 2017
@304NotModified
Copy link
Contributor

Is this NLog's internal logger? Are there performance issues?

@luigiberrettini
Copy link
Owner Author

I would like to try adding extension methods accepting a lambda so that string concatenation and possibly other statements only get executed when the logger is enabled.

@304NotModified
Copy link
Contributor

Recent NLog releases have InternalLogger.Log with parameters, e.g InternalLogger.Debug("hello {0}", foo).

But the most efficient is to first check InternalLogger.IsDebugEnabled.

@JonPaz
Copy link

JonPaz commented Jan 13, 2017

I agree with @luigiberrettini, having a wrapper that handles the level enabled detection and only invokes the supplied action as necessary would be great. Perhaps this could get rolled back into NLog itself?

@304NotModified
Copy link
Contributor

Perhaps this could get rolled back into NLog itself?

sounds good to me. That was also the reason to react here ;)

@JonPaz
Copy link

JonPaz commented Jan 13, 2017

Thanks @304NotModified,

I'll look at raising a ticket on the main NLog project as well (if one doesn't already exist) and if I get time do a branch for Targets.Syslog.

Out of interest (and I haven't yet looked at the NLog backlog), do you know of any plans to roll this type of functionality into the NLog:ILogger interface etc? SLF4J has this type of interface and it is rather useful ;-)

Cheers,

Jon.

@304NotModified
Copy link
Contributor

, do you know of any plans to roll this type of functionality into the NLog:ILogger interface etc?

not for NLog 4 as that will be a breaking change.

@luigiberrettini
Copy link
Owner Author

If @JonPaz is going to create a PR for NLog I will then upgrade the NLog dependency in this target.
Why adding methods with lambda parameters to the ILogger interface is a breaking change @304NotModified?

@304NotModified
Copy link
Contributor

Why adding methods with lambda parameters to the ILogger interface is a breaking change @304NotModified?

because customers could have written their own logger class which implements ILogger

But to be clear, do you like to add this to ILogger (Logger class) or the InternalLogger. The first is already supporting lamda's with the delegate LogMessageGenerator, see http://nlog-project.org/documentation/v4.4.0/html/M_NLog_ILogger_Debug.htm

@luigiberrettini
Copy link
Owner Author

It is needed for the InternalLogger but I honestly don't know if the InternalLogger implements ILogger or is a standalone class with its own behavior.
Anyway we will sort it out looking at the code thanks!

@304NotModified
Copy link
Contributor

It is needed for the InternalLogger but I honestly don't know if the InternalLogger implements ILogger

It doesn't as ILogger contains a lot of methods and InternalLogger is a static class

@304NotModified
Copy link
Contributor

anyway PR accepted, unit tests are mandatory ;)

@luigiberrettini
Copy link
Owner Author

@JonPaz IMHO you only have to overload the Log and Write functions with wrappers that accept a lambda and in their body call it and pass the resulting string to the currently implemented versions of those methods.

@JonPaz
Copy link

JonPaz commented Jan 15, 2017

@304NotModified - I wouldn't miss those out!

@304NotModified
Copy link
Contributor

created a PR already (NLog/NLog#1919), as I will try to release 4.4.2 soon

@JonPaz
Copy link

JonPaz commented Jan 16, 2017

Great stuff @304NotModified , you're faster than I am ;-)

@luigiberrettini luigiberrettini self-assigned this Jan 24, 2017
@JonPaz
Copy link

JonPaz commented Jan 29, 2017

@luigiberrettini - Just a quick message to say that we're now using this target in our production system and it is working nicely, thanks for all of your hard work!!! I'll have a look at getting it running with .Net core at some point in the next few weeks.

sdadek-CKPL added a commit to sdadek-CKPL/NLog.Targets.Syslog that referenced this issue Feb 20, 2017
* Handle non-ThreadAgnostic layouts (luigiberrettini#79 closes luigiberrettini#75)

* Fix entry assembly fallback detection (luigiberrettini#80 closes luigiberrettini#76)

* Decrease internal logger calls performance hit (luigiberrettini#81 closes luigiberrettini#56)

By using InternalLogger lambda parameters, strings will be built only if the lambda is called i.e. only if that level of the internal logger is enabled

* Fix: use RFC 5424 config values instead of hardcoded ones
@snakefoot
Copy link
Contributor

@luigiberrettini I guess the lambda allocation is faster than the string-format, but an even faster alternative is:

InternalLogger.Debug("Successfully sent message '{0}'", logEventMsgSet);

Then there is no allocations at all.

@304NotModified
Copy link
Contributor

304NotModified commented Feb 23, 2017

@snakefoot it allocates a string? ;)

@snakefoot
Copy link
Contributor

snakefoot commented Feb 24, 2017

InternalLogger.Debug("Successfully sent message '{0}'", logEventMsgSet);

it allocates a string? ;)

@304NotModified Yes :) but it is a one time interned string-allocation, so it is a one time penalty, while the lambda-capture-local-context-allocation is for every log-event.

@304NotModified
Copy link
Contributor

You're right @snakefoot,

But I have my doubts if that has any impact in performance in practice (the lambda)

@snakefoot
Copy link
Contributor

snakefoot commented Feb 24, 2017

But I have my doubts if that has any impact in performance in practice (the lambda)

The lambda allocations has a cost, and will put a strain on the GC0-collections, but yes they are cheaper than performing a string.Format(). I was just referring to the PR-title "Internal logger calls should not impact when it is not enabled". And my suggestion would lower the impact even further, so it just becomes a stack-allocation.

@304NotModified
Copy link
Contributor

And my suggestion would lower the impact even further, so it just becomes a stack-allocation.

Yes that's true!

@luigiberrettini
Copy link
Owner Author

If that formatting signature is already available I will give it a try, thanks @snakefoot!

@luigiberrettini luigiberrettini changed the title Internal logger calls should not impact when it is not enabled Internal logger calls should not impact if it is disabled May 12, 2018
@luigiberrettini luigiberrettini changed the title Internal logger calls should not impact if it is disabled Internal logger calls impact when it is disabled Nov 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants