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

Null processing for ${aspnet-session:Variable=User.Name:EvaluateAsNestedProperties=true} #69

Closed
worldofsites opened this issue Nov 9, 2016 · 9 comments · Fixed by #78
Labels
Milestone

Comments

@worldofsites
Copy link

Session["user"] = new UserInfo { Name= "johnDoe", Id = 100};
layout="${aspnet-session:Variable=User.Name:EvaluateAsNestedProperties=true}"

I think there need to null check for Session["user"].
If object is not null, then try to read property of that object, else return.

From NLog.Web.Internal => PropertyReader => method GetPropertyInfo:
return value.GetType().GetProperty(propertyName); // "value" can be null here.
Thanks.

@UgurAldanmaz
Copy link

Thanks for your proposal.

Did you try it with null value? If yes, what is the result?

@worldofsites
Copy link
Author

worldofsites commented Nov 9, 2016

Yes, sure, I tried with null value. And exception is:
System.NullReferenceException in NLog.Web.Internal.PropertyReader.GetPropertyInfo(Object value, String propertyName)
Also I tried that hack in config:
${when:when='${aspnet-session:Variable=User}' != null:inner='${aspnet-session:Variable=User.Name:EvaluateAsNestedProperties=true}'}
But to no avail

@UgurAldanmaz
Copy link

So null checking would be useful. But we need to check the side affects.

What do you tkink @304NotModified?

@304NotModified
Copy link
Member

I think this is the best approach:

I think there need to null check for Session["user"].
If object is not null, then try to read property of that object, else return.

@UgurAldanmaz
Copy link

I just opened a bug fix issue for this, #70

@worldofsites,
By the way, you are trying to read User.Name

${when:when='${aspnet-session:Variable=User}' != null:inner='${aspnet-session:Variable=User.Name:EvaluateAsNestedProperties=true}'}

but it is actually UserInfo

Session["user"] = new UserInfo { Name= "johnDoe", Id = 100};

Is it just a typo in the issue or could it be the reason?

Thanks for reporting!

@worldofsites
Copy link
Author

worldofsites commented Nov 9, 2016

@UgurAldanmaz
Key in session = "user", so => in config 'Variable' should "User".
As in https://github.com/NLog/NLog/wiki/AspNetSession-Layout-Renderer
And also it's just example. I tried with the real data with another session keys.

@UgurAldanmaz
Copy link

You are right. Sorry for my oversight.

@worldofsites
Copy link
Author

worldofsites commented Nov 9, 2016

@UgurAldanmaz @304NotModified
Also, about "Variable" values:
Please check nullable context places in "...ValueLayout.." classes, for example AspNetRequestValueLayoutRenderer.cs
var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
should be
var httpRequest = HttpContextAccessor?.HttpContext?.TryGetRequest();
like in AspNetRequestUrlRenderer.cs
Because all elements of the chain can be null. I tried that and can confirm.
Thanks.

@304NotModified
Copy link
Member

PS it's OK for me to move to VS2015 (C# 6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants