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

RegistryLayoutRenderer: Support for layouts, RegistryView (32, 64 bit) and all root key names (HKCU/HKLM etc) #925

Merged
merged 31 commits into from
Jan 25, 2016

Conversation

Niklas-Peter
Copy link
Contributor

  • Layouts for Key, Value, DefaultValue
  • Added View
  • Root key names like "HKLM" are now allowed

Please check the code, especially your conventions for exception handling. ParseKey() and this.DefaultValue.Render() could throw an exception. I do not know, how you would like to handle it.

Obviously Microsoft.Win32 is a problem. Maybe you can add appropriate pre compiler conditions to only activate it on supporting platforms.

@304NotModified
Copy link
Member

What do you mean with

Obviously Microsoft.Win32 is a problem

I'm on mobile now, so I cannot easily see the code. But did you excluded the code for Silverlight? (see appveyor error)

@Niklas-Peter
Copy link
Contributor Author

Silverlight has already been excluded in the old version and still is. So it should not be the problem. However the enum RegistryView is not supported in .NET 3.5, so I think, that it is the problem.
Furthermore I had accidentally removed using NLog.Internal, so that the extension metod for Exception was missing. I fixed it.

However some unit tests fail, but I am not quite sure, whether the unit tests themselves are correct. Why do you use @ and additionally escape with \\ (see RegistryNamedValueTest and RegistryUnnamedValueTest)? Why do you expect, that defaultValue=C\\:temp results in C:temp (see RegistyDefaultValueTest_with_colon)?

@304NotModified
Copy link
Member

Why do you expect, that defaultValue=C:temp results in C:temp

We need to escape the : (because separator) and also we need to escape the \ (because of control characters like \n) in C#, so \\:. This one is correct.

Why do you use @ and additionally escape with \

I think because Layout is interpreted as string and not an verbatim string. Not sure if this make sense..

Just checked the code. The \ seems interpreted by the parser, because also we like to support \n in the xml Config (and so \ needs to be escaped to \\


this.subKey = this.key.Substring(pos + 1).Replace('/', '\\');
Copy link
Member

Choose a reason for hiding this comment

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

I think this removal makes it non-backwards-compatible

This one needs comments.

We can make it also conditional with an property (default of course backwardscomp)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why? I just moved it to ParseKey().

@Niklas-Peter
Copy link
Contributor Author

The reason, why the 3 tests fail, is, that I changed "string" to Layout for Key, Value and DefaultValue. Obviously \\ is treated different, when it is a Layout. I do not know enough about the inner workings of NLog, so I can not fix it. Either you know what to do or we have to change it back to a string, but actually the layouts were the main reason for creating this pull request.

@304NotModified
Copy link
Member

We can fix it :)

But not now. On holiday with limited Internet / pc. I will look at it this weekend.

@304NotModified
Copy link
Member

I'm looking at this now :)

@304NotModified
Copy link
Member

Looked at this:

  • note: registryValue can be null in registryValue = registryKey.GetValue(renderedValue);
  • code for handling the Exception should be
       catch (Exception ex)
            {

                if (ex.MustBeRethrown())
                {
                    throw;
                }
               InternalLogger.Error(ex.ToString());
            }
  • I changed the parsekey for the slashes to:
            if (pos >= 0)
            {
                hiveName = key.Substring(0, pos);

                var subKey1 = key.Substring(pos + 1).Replace('/', '\\');

                //remove duplicates
                subKey1 = subKey1.Replace(@"\\", @"\");
                subKey1 = subKey1.TrimStart('\\');
                this.subKey = subKey1;
            }

@304NotModified
Copy link
Member

I will add some tests to the RegistryTests in master, because i'm curious how key was working with forwardsslashes (and that we don't break it)

@Niklas-Peter
Copy link
Contributor Author

note: registryValue can be null in registryValue = registryKey.GetValue(renderedValue);

From MS documentation:

Retrieves the value associated with the specified name. Returns null if the name/value pair does not exist in the registry.

I do not see the problem.

@304NotModified
Copy link
Member

Retrieves the value associated with the specified name. Returns null if the name/value pair does not exist in the registry

It's a false positive of Resharper :) Can you add this a comment in the code?

@Niklas-Peter
Copy link
Contributor Author

Actually I had already done so, see line 142.

@304NotModified
Copy link
Member

I see the confusion. It about openSubKey. - my mistake. Posted the wrong code.

https://msdn.microsoft.com/en-us/library/z9f66s0a(v=vs.110).aspx

The subkey requested, or null if the operation failed.

So Reshaper was right :)

@304NotModified
Copy link
Member

I added some more tests in #937.

if you like, you can merge them for locally testing:

Git commands:

git remote add upstream https://github.com/NLog/NLog.git
git fetch upstream
git checkout patch-1
git merge upstream/master

edit: well, merge is not necessary, only if you would like to debug locally at your site.

@304NotModified 304NotModified self-assigned this Oct 11, 2015
@304NotModified
Copy link
Member

TODO:

@304NotModified
Copy link
Member

Hi @Niklas-Peter ,

The slash isssue has been resolved, see 118b8a5

The parse of the inner layout fixed is fixed in #975

Can you merge with master and fix the last issues?

@Niklas-Peter
Copy link
Contributor Author

I will do that anytime, but currently I do not even have time to fix these small issues ...

@304NotModified
Copy link
Member

No problem. Any idea when you have time?

The release of 4.2 is nearby, but we can postpone this feature to 4.3

@304NotModified
Copy link
Member

If you like help this, just add me as a contributor and let me know. I like to have the feature implemented :)

@304NotModified
Copy link
Member

We need unit test for this.

@Niklas-Peter
Copy link
Contributor Author

I added you as a collaborator to Niklas-Peter/NLog. Is that, what you intended?

@304NotModified
Copy link
Member

Yes, collaborator I meant :)

- Layouts for Key, Value, DefaultValue
- Added View
- Root key names like "HKLM" are now allowed
Note: I can not find any use for an not set "Value". If Value is null, you would always get the DefaultValue. Maybe you should make it a required parameter?
Please check the code, especially your conventions for exception handling. ParseKey() and this.DefaultValue.Render() could throw an exception. I do not know, how you would like to handle it.
@304NotModified 304NotModified modified the milestones: 4.4, 4.3 Jan 22, 2016
@304NotModified
Copy link
Member

Full change:

  • the following root keys are now allowed
    • HKEY_LOCAL_MACHINE
    • HKLM
    • HKEY_CURRENT_USER
    • HKCU
    • HKEY_CLASSES_ROOT
    • HKEY_USERS
    • HKEY_CURRENT_CONFIG
    • HKEY_DYN_DATA
    • HKEY_PERFORMANCE_DATA
  • Key, Value, DefaultValue are now layoutable.
  • added option RequireEscapingSlashesInDefaultValue for backwardscompat. Default true. In the old situation C:\temp was OK, it should now be C:\\temp, if RequireEscapingSlashesInDefaultValue=false, because DefaultValue is now a Layout.
  • added option RegistryView. (not for .Net 3.5). Gets or sets the registry view (see: https://msdn.microsoft.com/de-de/library/microsoft.win32.registryview.aspx. Allowed values: Registry32, Registry64, Default

@304NotModified
Copy link
Member

@bhaeussermann / @page-not-found / @UgurAldanmaz can someone review this one?

I like to include this in NLog 4.3

Thanks!

@304NotModified 304NotModified removed their assignment Jan 24, 2016
@304NotModified 304NotModified added the enhancement Improvement on existing feature label Jan 24, 2016
@304NotModified 304NotModified changed the title Support for layouts, RegistryView and root key names RegistryLayoutRenderer: Support for layouts, RegistryView (32, 64 bit) and all root key names (HKCU/HKLM etc) Jan 24, 2016
</nlog>");

LogManager.GetLogger("d").Debug("zzz");
AssertDebugLastMessage("debug", "reg32");
Copy link
Contributor

Choose a reason for hiding this comment

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

Can these tests not use AssertLayoutRendererOutput() like the other tests?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see there are old tests which also use this approach; can be changed to use AssertLayoutRendererOutput()?

Copy link
Member

Choose a reason for hiding this comment

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

Good idea :)

Copy link
Member

Choose a reason for hiding this comment

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

I found a difference. AssertLayoutRendererOutput only renders the layout renderer, while these test cases really writes to the registry. So keep it like this

@bhaeussermann
Copy link
Contributor

I checked the code. I cannot find any problems. Just two minor remarks on the tests.

@304NotModified
Copy link
Member

Thanks @bhaeussermann!

@304NotModified 304NotModified self-assigned this Jan 24, 2016
@304NotModified
Copy link
Member

Merge after successful builds

304NotModified added a commit that referenced this pull request Jan 25, 2016
RegistryLayoutRenderer: Support for layouts, RegistryView (32, 64 bit) and all root key names (HKCU/HKLM etc)
@304NotModified 304NotModified merged commit db70244 into NLog:master Jan 25, 2016
@304NotModified
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement on existing feature feature XSD change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants