I've been trying to configure a default logging directory when an environment variable is empty. The default value for the project needs to be c:\logs\ - note the trailing slash.
In the target I've been setting the filename to various things, but they all don't work:
filename="${environment:whenEmpty=c\:\log\:LOG_DIR}logfilename.log" - escaping is wrongfilename="${environment:whenEmpty='c\:\log\':LOG_DIR}logfilename.log" - outputs the single quotes<variable name="logDirectory" value="c\:\logs\"/> andfilename="${environment:whenEmpty=${logDirectory}:LOG_DIR}logfilename.log" - throws an exception about Variable not being supplied as a parameter to environmentfilename="${environment:whenEmpty='${logDirectory}':LOG_DIR}logfilename.log" - outputs the single quotesThis is on .NET core 1.1 and version 4.4.1 of NLog, so it may just be that I'm on an old version and the project should be upgraded.
Can what I'm trying to do be done?
I think this should work ${whenEmpty:whenEmpty=${literal:text=c:\logs\}:inner=${environment:LOG_DIR}}
Thanks for the speedy reply. I had thought about using the literal:text renderer but assumed it would have the same problems.
I tried your suggestion and I get nothing from the ${whenEmpty} renderer and looking at the internal log I get this line:
Warn Exception in layout renderer. Exception: System.NullReferenceException: Object reference not set to an instance of an object.
I'm wondering if the trailing slash escapes the closing brace and so it messes up the parsing. Could that be the case and, if so, is there a way I can escape the trailing slash?
could you please post the full stacktrace? A NullReferenceException is always a bug
I'm not sure how much use it will be, but this is the stack trace I can see in the internal log:
2018-08-12 21:19:36.8965 Warn Exception in layout renderer. Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at NLog.LayoutRenderers.Wrappers.WhenEmptyLayoutRendererWrapper.RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target)
at NLog.LayoutRenderers.Wrappers.WrapperLayoutRendererBuilderBase.Append(StringBuilder builder, LogEventInfo logEvent)
at NLog.LayoutRenderers.LayoutRenderer.RenderAppendBuilder(LogEventInfo logEvent, StringBuilder builder)
That's with this filename property:
fileName="${whenEmpty:whenEmpty=${literal:text=c:\logs\}:inner=${environment:LOG_DIR}}api-${shortdate}.log"
The same thing happens if I escape the :
Thanks again for looking at it!
Thanks!
the \ should be escaped with another \\, but still it's an issue because the \ will also escape the :.
I think the best solution for now is to create a variable and use ${var:myDir}
I'm sorry, haven't found time to find the solution for this yet. Maybe this is a good question for stackoverflow
Hello @pablinos!
The problem is still relevant? I could not reproduce the error in myself.
Looking through this, I think the specific error I was receiving from trying the double backslash was fixed in #2908, but I don't think there is a fix to the problem I have. Using colons isn't possible according to the comments on #3158 and it's stated in #2908 that it wouldn't fix this issue. Maybe it needs to be something like c\:\\log\\
I'll have another stab at it and see if I can get it to work.
Yes - c\:\\log\\
Try it.
<variable name="default-path"
value="${whenEmpty:whenEmpty=${literal:text=c\:\\logs123\\logs345\\}:inner=${environment:LOGDIR}}"/>
```