Nlog - conditional layout based on message - nlog

I want nlog to output newline when message is empty, when message is not empty, I want output layout.
I've tried following approach, but it doesn't work:
<variable name="DefaultLayout" value="${time} ${level:uppercase=true}: ${message}"/>
<variable name="Main" value="${when:when='${message}'=='':inner=${newline}:else=${DefaultLayout}}"/>
......
<target xsi:type="ColoredConsole" name="console" layout="${Main}" />
In this configuration it prints new line when message is empty, but when message is not empty, it doesn't print this message, only 16:06:34.7274 INFO
How to fix config to have desired effect?

You need to escape the colon (:) in the variable DefaultLayout with a backslash (\)

Related

uncomment xml tags on centOS 7.x

Is there a command (using sed or any other tool) to remove XML comment (which has a comment string also) on some XML tags? I am using centos 7.4 and 7.9. The XML structure is like this:
<root>
<!-- Some string here to inform
<SomeTagHere />
<SomeTagHere2> </SomeTagHere2>
-->
</root>
I tried many different sed commands but none of them worked and I couldn't find a proper regex for this. What I want to is release tags inside comment like this:
<root>
<SomeTagHere />
<SomeTagHere2> </SomeTagHere2>
</root>

Optional spaces between multiple layout renderers?

I think I must be missing something extremely obvious here but furious doc reading and googling has failed me. I have the following layout for a file name:
<target name="asyncLogFile" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard" >
<target name="mainLogFile" xsi:type="File" fileName="logs/${mdlc:item=GameServer:whenEmpty=Main} ${mdlc:item=Controller}.log"
layout="${longdate} ${level:uppercase=true} | ${logger} | ${message} ${exception:format=toString,Data:maxInnerExceptionLevel=3}" />
</target>
The MDLC items can both be null, in the games of the GameServer it gets replaced with "Main" and the Controller doesn't matter (If the GameServer is null then the Controller will be null).
However this means that if GameServer is null I end up with
Main .log Because of the space I have between the two mdlc entries. How can I make that space optional as part of the GameServer mldc entry!? I have looked at :pad to add a space after the but that doesn't seem to work. I have tried concatenating the mdlc's but that doesn't work.
I was expecting something simple you could add as an ambient property like :suffix but nothing like that exists.
Again I think I must be missing something basic here but it eludes me.
Same ugly, but faster because of less allocation:
<variable name="GameServerLogFile" value="${when:when='${mdlc:item=GameServer}'=='':inner=Main:else=${mdlc:item=GameServer} ${mdlc:item=Controller}}" />
<targets>
<target name="mainLogFile" xsi:type="File" fileName="logs/${GameServerLogFile}.log" />
</targets>
Better to compare against empty-string '', than to call length()-method.
Ugly but this works
<target name="mainLogFile" xsi:type="File" fileName="logs/${when:when=length('${mdlc:item=GameServer}') > 0:inner=${mdlc:item=GameServer} ${mdlc:item=Controller}:else=Main}.log"

NLog - File Rotation - Sequence Issue

I am using Nlog for logs and rotating file on daily basis. Log files are read and processed further by another system to move to Splunk.
Here I am looking to find out what steps nlog takes to rotate file because it is failing my integration with other systems. It works fine with other log framework like log4net/logback. If that's case, I may need to switch to other framework.
Ideally steps should be - Approach 1
1. Open file - abc.txt
2. add log line 1, log line 2.. log line n.
3. 12.00 AM - file rotation - Rename file abc.txt => 2020-05-13 abc.txt
4. create new file - abc.txt 5. add log line a1, log line a2.. log line an.
Another approach may be - Approach 2
1. open file - abc.txt
2. add log line 1, log line 2.. log line n.
3. 12.00 AM - file rotation - create new file 2020-05-13 abc.txt
4. move contents from abc.txt to new file 2020-05-13 abc.txt
5. Update file offset/pointer to move to new location i.e. start of the file (as contents are removed from this file)
6. add log line a1, log line a2.. log line an - at the begining location (offset/file pointer)
Can anybody please confirm which approach or sequence of steps are exactly performed by Nlog for file rotation.
[UPDATES]
Below is how my config looks like-
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="logDir" value="C:\logs">
<targets>
<target name="logFile" xsi:type="File" fileName="${logDir}\API_${machinename}.json"
archiveFileName="${logDir}\{#}_API_${machinename}.json" archiveNumbering="Date"
archiveDateFormat="yyyy-MM-dd" archiveEvery="Day">
<layout xsi:type="JsonLayout" includeAllProperties="true">
<attribute name="time" layout="${date:format=yyyy-MM-ddTHH\:mm\:ss.fffzzz}"/>
<attribute name="level" layout="${level:upperCase=true}"/>
<attribute name="message" layout="${message}"/>
<attribute name="exception" layout="${exception:format=#}"/>
</layout>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logFile" />
</rules>
</nlog>

Unexpected long/integer value at the start of the logged message

I'm facing an issue with log4j2, that prints a value as first part of a logged message.
I'm using the following configuration and it works as expected except for the un-wanted value:
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="info">
<Appenders>
<RollingFile name="myappender-app" fileName="C:/Programs/apache-tomcat-8.5.29/logs/app.log" filePattern="C:/Programs/apache-tomcat-8.5.29/logs/%d_app.log">
<PatternLayout>
<pattern>[%t]%5level %d{ISO8601}[%X{mdc-uuid} %X{sessionId}] %c{2} - %m%n%r%throwable{5}</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.mypack.app" level="debug" additivity="false">
<appender-ref ref="myappender-app" level="debug" />
</Logger>
</Loggers>
</Configuration>
The result is something like:
[https-jsse-nio-18443-exec-6] INFO 2018-04-02T10:37:24,344[ ] filters.UUIDFilter - First logged message
39736[https-jsse-nio-18443-exec-6] INFO 2018-04-02T10:37:24,596[localhost-1522658244353 ] controller.MyAppController - Second Message
39988[https-jsse-nio-18443-exec-6] INFO 2018-04-02T10:37:24,602[localhost-1522658244353 ].....
39994[https-jsse-nio-18443-exec-6]ERROR 2018-04-02T10:37:54,697[localhost-1522658244353 ]....
other messages
The first message is correct while the others have those values [39736,39988,..] at the start of each line.
I'm assuming is something related to the rolling policy, but I could not see it in other examples using the same configuration.
Does anyone knows which is the problem and how to solve it ?
Problem is %n%r%throwable{5} in below line -
<pattern>[%t]%5level %d{ISO8601}[%X{mdc-uuid} %X{sessionId}] %c{2} - %m%n%r%throwable{5}</pattern>
%n refers to new line character.
%r refers to number of milliseconds elapsed since the JVM was started until the creation of the logging event.
%throwable{5} refers to exception stack trace.
Because your pattern contains %n so it will create a new line in logs and then %r will log number of milliseconds (that unwanted value). Since there is no exception so %throwable{5} is printing nothing.
To resolve it, simply write %n at the end of the pattern so that a new log statement will get printed in next line. You can also remove %r if you do not want to print that unwanted value at all.

shell script sed replace

I have this file config.xml
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev#callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>PASSWORD</enter>
<content src="index.html" />
<access origin="*" />
I tried to do it with sed without success.
I need to do this:
$./script.sh config.xml NEWPASSWORD
to get:
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev#callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>NEWPASSWORD</enter>
<content src="index.html" />
<access origin="*" />
Using backreference:
sed "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
^\( *<enter>\): search for lines starting with any number of spaces followed by <enter>. Matching characters are captured with escaped parentheses.
\([^>]*\)<: following characters up top next < are captured in a second group.
\1$2<: in the substitution string, characters from first group are output(\1) followed by the second parameter value passed to the script, ($2, the new password value)
The command is applied to $1, the file passed as first parameter to the script (the file name).
To edit the file in place, use the -i flag:
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
The good result is:
$cat script.sh
#!/bin/sh
file=$1
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
Then:
$./script.sh config.xml NEWPASSWORD
Many thanks to everyone, especially to Kenavoz.

Resources