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

OS-dependend directory separator LayoutRender #3791

Closed
igabas opened this issue Feb 4, 2020 · 7 comments · Fixed by #3793
Closed

OS-dependend directory separator LayoutRender #3791

igabas opened this issue Feb 4, 2020 · 7 comments · Fixed by #3793
Labels
Milestone

Comments

@igabas
Copy link

igabas commented Feb 4, 2020

Problem

When using FileTarget, properties such as archiveFileName and fileName must be generated according to the target OS (another directory delimiter "/" or "") for proper interpretation.
At the moment, to solve this problem, it is necessary to dynamically download the version of nlog-config with the correct paths depending on the OS or other manipulations.

For example (identical configs with only other "log-dir" variable):
nlog.win.config

<nlog>
 <variable name="log-dir" value="${basedir}\log\"/>
  <targets>
 <target xsi:type="File" fileName="${log-dir}logs.log"
..
/>
  </targets>
...
</nlog>

nlog.unix.config

<nlog>
 <variable name="log-dir" value="${basedir}/log/"/>
  <targets>
 <target xsi:type="File" fileName="${log-dir}logs.log"
..
/>
  </targets>
...
</nlog>

Solution example

It would be great if there was a pre-defined layout render (directory path separator or OSVersion marker) to solve this problem.
Version:
For example:

[LayoutRenderer("ds")]
public class DirectorySeparatorLayoutRenderer : LayoutRenderer
{
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            builder.Append(Path.DirectorySeparatorChar);
        }
}

nlog.config

<nlog>
 <variable name="log-dir" value="${basedir}${ds}log"/>
  <targets>
 <target xsi:type="File" fileName="${log-dir}${ds}logs.log"
..
/>
  </targets>
...
</nlog>
@welcome
Copy link

welcome bot commented Feb 4, 2020

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

@304NotModified
Copy link
Member

Can't you use forward slashes in Windows? AFAIK I n windows you could use both.

But have to agree we should add this feature

@304NotModified
Copy link
Member

Anyway, you could also add the DS in your application with a few lines of code
https://github.com/nlog/nlog/wiki/How-to-write-a-custom-layout-renderer

@304NotModified 304NotModified changed the title OS-dependend LayoutRender OS-dependend directory separator LayoutRender Feb 4, 2020
@igabas
Copy link
Author

igabas commented Feb 4, 2020

Can't you use forward slashes in Windows? AFAIK I n windows you could use both.

Yes, its work on windows. But if make a mistake with separator for linux (use windows notation '') env its create log files in current app directory with incorrect names.

@snakefoot
Copy link
Contributor

snakefoot commented Feb 4, 2020

${ds} should be ${directory-seperator}. But I also don't understand why / is not used (Can always create a NLog config variable with shortname locally).

Alternative one can setup LogDir using GDC at runtime.

@304NotModified 304NotModified linked a pull request Feb 4, 2020 that will close this issue
@304NotModified
Copy link
Member

I made it as ${dir-separator}, as all NLog "directory" renderers are named with "dir".

@304NotModified 304NotModified added this to the 4.7 milestone Feb 4, 2020
@304NotModified
Copy link
Member

fixed by #3793!

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