Skip to content

Have more robust log4net loading #1837

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

Merged
merged 7 commits into from
Nov 20, 2018

Conversation

fredericDelaporte
Copy link
Member

@fredericDelaporte fredericDelaporte commented Sep 4, 2018

In some circumstances, it appears the log4net loading mechanism may fail with null reference exceptions, which does not help understanding what is the trouble. (See #1836.)

Moreover, log4net loading is triggered after some probing logic which is in no way involved in how it is actually loaded, creating a possibility the probing considers it loadable while it is not.

Fixes #1836

private static readonly Func<Assembly, string, object> GetLoggerByNameDelegate;
private static readonly Func<System.Type, object> GetLoggerByTypeDelegate;

static Log4NetLoggerFactory()
{
LogManagerType = System.Type.GetType("log4net.LogManager, log4net", true);
GetLoggerByNameDelegate = GetGetLoggerByNameMethodCall();
GetLoggerByTypeDelegate = GetGetLoggerMethodCall<System.Type>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method calls following the get type are using the type without checking if it was found, so better not allow GetType to yield null.

Reuse the logic from ReflectHelper, and mutualise it for all log4net
type loadings
Copy link
Member

@hazzik hazzik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stupid mobile version makes all comments pending requiring a "submit"

@hazzik
Copy link
Member

hazzik commented Sep 12, 2018

Wouldn't it be enough to just Assembly.Load("log4net")? I don't have a windows machine handy to check the scenario with log4net being in the GAC (my laptop has died of old age).

@fredericDelaporte
Copy link
Member Author

The method is not even documented for usage with short names. With the GAC tendency to reject short names unless they are already loaded in the application domain, I would not rely on this.

Since the code from ReflectionHelper is the one used for loading reflection based drivers, I think it is quite proven it works well in many situation, and so I would rather avoid deviating from it /simplifying it.

@hazzik
Copy link
Member

hazzik commented Sep 16, 2018

With the GAC tendency to reject short names unless they are already loaded in the application domain

But this is anyway what happens here. Isn't it?

@fredericDelaporte
Copy link
Member Author

fredericDelaporte commented Sep 17, 2018

It happens with the Type.GetType call, but then the ReflectHelper logic fallback on searching among already loaded assemblies any loosely matching one for loading the type from it, and I guess that is the part doing the trick for the case reported here. The last fallback, attempting to explicitly load the assembly with a short name, is likely to be useless in the case reported here. But maybe it still helps in some other cases.
Once again here, I am a bit conservative, preferring to stick to code which works since years without known complains, rather than trying to simplify it without being sure it won't harm any case.

@hazzik
Copy link
Member

hazzik commented Sep 17, 2018

code which works since years without known complains

The "logic fallback on searching among already loaded assemblies any loosely matching one for loading the type from it" has been added a few month back in 5.2

@fredericDelaporte
Copy link
Member Author

By yourself, granted, but the Assembly.Load logic was already there, and that is the one we are currently discussing, aren't we? There is no comment to tell in which case it may help, but does it harms to keep it? Less code is always better, provided we are sure removing it will not break some cases. There, I am not sure.

@wensaint
Copy link

@fredericDelaporte could you send a fixed release version, i failed to build the dll.
thanks,
my email is wensaint@qq.com

@fredericDelaporte
Copy link
Member Author

Expands the checks of this PR, find the "NHibernate (Release Package)" one and click on "Details". Teamcity may ask you for a login but an anonymous option is normally available. Then browse to the artifacts tab of the build and take what you need.

But the current release build was not having artifacts, I do not know why. I have relaunched it, wait for it to complete if it is not finished yet.

@fredericDelaporte
Copy link
Member Author

There was some discussion in this PR about whether the loading code could be simplified or not. I was, and still am, for keeping this code as near as possible to the loading logic of ReflectHelper (which we cannot use directly due to otherwise creating a circular reference trouble), even if some of it is maybe not useful in all circumstances.
There are just too many possible cases (in GAC or not in GAC, netfx or netcoreapp or mono, directly referenced by the application or just sitting somewhere in a loadable path or in the GAC, strong signed application or not, running in special hosts like Sharepoint, ...) for being sure to have checked them all.

So,

As proposed on the development group, and since no continued grounded disagreement has been expressed there since one week after raised concerns being addressed, I intend to merge this PR next week (likely on Wednesday).

(I am issuing this notice on PR issued before starting to apply this handling of merges. I will not do it for new PR.)

@hazzik
Copy link
Member

hazzik commented Oct 30, 2018

As I still disagree with this change - does it count as "continuous disagreement"?

@fredericDelaporte
Copy link
Member Author

Of course, but I would like to better understand what cause you to still disagree. I believe to have explained why this change was the way it was, where you have expressed concerns. May you tell what is wrong with those explanations?

hazzik
hazzik previously approved these changes Nov 18, 2018
Copy link
Member

@hazzik hazzik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved by now just to unblock 5.2. However, I still think that the code needs to be simplified and not related on the code borrowed from ReflectHelper. Also, exception flow logic has to be removed.

Copy link
Member Author

@fredericDelaporte fredericDelaporte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact changes are to be done.

Co-Authored-By: fredericDelaporte <12201973+fredericDelaporte@users.noreply.github.com>
@fredericDelaporte fredericDelaporte merged commit 94bcb21 into nhibernate:master Nov 20, 2018
@fredericDelaporte fredericDelaporte deleted the log4net branch November 20, 2018 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants