Redirecting log to json file using NLog - nlog

In my organization we use UIPath and Robots logging is based on NLog. The default nlog configuration looks like:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="WorkflowLoggingDirectory" value="${specialfolder:folder=LocalApplicationData}/UiPath/Logs" />
<rules>
<logger name="WorkflowLogging" writeTo="WorkflowLogFiles" final="true" />
<logger minLevel="Info" writeTo="EventLog" />
</rules>
<targets>
<target type="File" name="WorkflowLogFiles"
fileName="${WorkflowLoggingDirectory}/${shortdate}_Execution.log"
layout="${time} ${level} ${message}"
keepFileOpen="true"
openFileCacheTimeout="5"
concurrentWrites="true"
encoding="utf-8"
writeBom="true" />
<target type="EventLog" name="EventLog"
layout="${processname} ${assembly-version} ${newline}${message}"
source="UiPath" log="Application" />
</targets>
</nlog>
It works but we decided to redirect logs also to json file. Now NLog.config file looks like:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="WorkflowLoggingDirectory" value="${specialfolder:folder=LocalApplicationData}/UiPath/Logs" />
<rules>
<logger name="WorkflowLogging" writeTo="WorkflowLogFiles" final="true" />
<logger name="ToJson" writeTo="jsonFile" />
<logger minLevel="Info" writeTo="EventLog" />
</rules>
<targets>
<target type="File" name="WorkflowLogFiles"
fileName="${WorkflowLoggingDirectory}/${shortdate}_Execution.log"
layout="${time} ${level} ${message}"
keepFileOpen="true"
openFileCacheTimeout="5"
concurrentWrites="true"
encoding="utf-8"
writeBom="true" />
<target type="EventLog" name="EventLog"
layout="${processname} ${assembly-version} ${newline}${message}"
source="UiPath" log="Application" />
<target type="File" name="jsonFile"
fileName="${WorkflowLoggingDirectory}/log.json"
layout="${longdate} ${level:upperCase=true} ${message}"
keepFileOpen="true"
concurrentWrites="true" />
</targets>
</nlog>
Now Robot doesn't log to any target. Any idea why?

Related

NLog - create log file at specific directory in linux

I've a .net core application on linux server.also in application i used nlog for logging. my application path on linux is /var/www-ninja/html/finance.api.gurukul.ninja. but with use of nlog i want to store logs in other linux directory. which is like /var/log/api/ninja/finance. so can I store logs in that directory. how can i do that ? for more details
nlog.production.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Info"
internalLogFile="c:\temp\internal-nlog.txt">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="NLog.Extensions.Logging"/>
<add assembly="NLog"/>
</extensions>
<variable name="ExceptionLayout" value="${longdate} [${processid}] ${uppercase:${level}} ${logger:shortName=true} ${environment-user} ${local-ip} ${aspnet-request-url} ${aspnet-request-method} ${message}${exception:format=tostring,Stacktrace}"/>
<variable name="CommonLayout" value="${longdate} [${processid}] ${uppercase:${level}} ${logger:shortName=true} ${environment-user} ${local-ip} ${message} "/>
<variable name ="logDir" value="/var/log/api/ninja/finance" />
<targets async="true">
<target xsi:type="File" name="file" layout="${CommonLayout}" fileName="${logDir}\log-${shortdate}.log" />
<target name="fileAsException"
xsi:type="FilteringWrapper"
condition="length('${exception}')>0">
<target xsi:type="File"
fileName="${logDir}\log-${shortdate}.log"
layout="${ExceptionLayout}" />
</target>
</targets>
<rules>
<logger name="*" writeTo="file,fileAsException"/>
<logger name="Microsoft.*" maxlevel="Info" final="true" />
</rules>
</nlog>
Make sure to use Unix-path, so stop using backslash \
Ex. instead of ${logDir}\log-${shortdate}.log then it should be ${logDir}/log-${shortdate}.log.
If still having issues then try to activate the NLog InternalLogger and check the output https://github.com/NLog/NLog/wiki/Internal-Logging

Keep last 7 days in nlog

I want to keep last 7 days of files using nlog, but for some reason the following configuration in target=LogToShare is keeping only the latest file:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="WorkflowLoggingDirectory" value="${specialfolder:folder=LocalApplicationData}/Logs" />
<variable name="LogSharedFolder" value="\\myserver\logs" />
<rules>
<logger name="WorkflowLogging" writeTo="LogToShare" />
<logger name="WorkflowLogging" writeTo="WorkflowLogFiles" final="true" />
<logger minLevel="Info" writeTo="EventLog" />
</rules>
<targets>
<target type="File"
name="LogToShare"
layout="${time} ${level} ${message}"
fileName="${LogSharedFolder}\${machinename}_${shortdate}.log"
archiveFileName="${LogSharedFolder}\${machinename}_${shortdate}.{#}.log"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="7"
keepFileOpen="true"
openFileCacheTimeout="5"
concurrentWrites="true"
encoding="utf-8"
writeBom="true" />
<target type="File" name="WorkflowLogFiles" fileName="${WorkflowLoggingDirectory}/${shortdate}_Execution.log" layout="${time} ${level} ${message}" keepFileOpen="true" openFileCacheTimeout="5" concurrentWrites="true" encoding="utf-8" writeBom="true" />
<target type="EventLog" name="EventLog" layout="${processname} ${assembly-version} ${newline}${message}" source="UiPath" log="Application" />
</targets>
</nlog>
The files in \\myserver\logs that are kept are only the files from the current day:
machine1_2020-08-09.log
machine2_2020-08-09.log
machine3_2020-08-09.log
machine4_2020-08-09.log
Tomorrow, the files that I will get are:
machine1_2020-08-10.log
machine2_2020-08-10.log
machine3_2020-08-10.log
machine4_2020-08-10.log
And the files from 2020-08-09 will be removed.
I'm using NLog v4.3.9. How can I keep the last 7 days of log?
When using NLog 4.5 (or newer) then you can do this:
<target type="File"
name="LogToShare"
layout="${time} ${level} ${message}"
fileName="${LogSharedFolder}/${machinename}_${shortdate}.log"
maxArchiveFiles="7"
keepFileOpen="true"
openFileCacheTimeout="5"
concurrentWrites="true"
encoding="utf-8"
writeBom="true" />
When using older NLog versions, then you can try this:
<target type="File"
name="LogToShare"
layout="${time} ${level} ${message}"
fileName="${LogSharedFolder}/${machinename}_${date:format=yyyy-MM-dd}.log"
archiveFileName="${LogSharedFolder}/${machinename}_{#}.log"
archiveDateFormat="yyyy-MM-dd"
archiveNumbering="Date"
archiveEvery="Year"
maxArchiveFiles="7"
keepFileOpen="true"
openFileCacheTimeout="5"
concurrentWrites="true"
encoding="utf-8"
writeBom="true" />
Explanation of why the extra parameters are needed:
archiveFileName - Using {#} allows the archive cleanup to generate proper file wildcard.
archiveDateFormat - Must match the ${date:format=} of the fileName (So remember to correct both date-formats, if change is needed)
archiveNumbering=Date - Configures the archive cleanup to support parsing of filenames as dates.
archiveEvery=Year - Activates the archive cleanup, but also the archive file operation. Because the configured fileName automatically ensures the archive file operation, then we don't want any additional archive operations (Ex. avoiding generating extra empty files at midnight).
maxArchiveFiles - How many archive files to keep around.

How to prevent a HttpClientFactory to log using NLOG.Web

I have started using the Asp.Net Core 2.1 HttpClientFactory
services.AddHttpClient();
Now I get these huge logs from the HttpClient, every request logs Info and Trace level logs.
2018-11-08 14:34:59.6753|::1||INFO |8.11.7.0|12 |Dto|Log.RequestPipelineStart|Start processing HTTP request GET ...
I have been trying to throw them in a black hole:
<logger name="System.Net.Http.HttpClient*" minlevel ="Trace" writeTo="blackhole" final="true" />
But that does not help. Any suggestions?
Full Nlog:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
internalLogToConsoleError="true"
internalLogLevel="Warn"
internalLogFile="logs\internal-nlog.txt"
throwExceptions="true"
autoReload="true">
<!-- Load the ASP.NET Core plugin -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets async="true">
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="logs\${machinename}-all.log"
layout="${longdate}|${aspnet-request-ip}|${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${event-properties:item=EventId.Id}|${logger}|${aspnet-mvc-controller}|${aspnet-Request-Method}|${message}|${exception}"
maxArchiveFiles="7"
archiveFileName="logs\archive\${machinename}-all.{#}.zip"
archiveNumbering="Date"
archiveEvery="Day"
archiveDateFormat="yyyyMMdd"
enableArchiveFileCompression ="true"
/>
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-trace" fileName="logs\${machinename}-trace.log"
layout="${longdate}|${aspnet-request-ip}|${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${pad:padding=-3:${threadid}}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True:cleanNamesOfAnonymousDelegates=True:cleanNamesOfAsyncContinuations=True}|${message}|${exception}"
maxArchiveFiles="14"
archiveFileName="logs\archive\${machinename}-trace.{#}.zip"
archiveNumbering="Date"
archiveEvery="Saturday"
archiveDateFormat="yyyyMMdd"
enableArchiveFileCompression ="true"
/>
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-error" fileName="logs\${machinename}-error.log"
layout="${longdate}|${aspnet-request-ip}||${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${pad:padding=-3:${threadid}}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True:cleanNamesOfAnonymousDelegates=True:cleanNamesOfAsyncContinuations=True}|${message}|${exception:format=tostring}"
maxArchiveFiles="100"
archiveFileName="logs\archive\${machinename}-error.{#}.zip"
archiveNumbering="Date"
archiveEvery="Day"
archiveDateFormat="yyyyMMdd"
enableArchiveFileCompression ="true"
/>
<target xsi:type="File" name="ownFile-info" fileName="logs\${machinename}-info.log"
layout="${longdate}|${aspnet-request-ip}|${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True:cleanNamesOfAnonymousDelegates=True:cleanNamesOfAsyncContinuations=True}|${message}|${exception}"
maxArchiveFiles="36"
archiveFileName="logs\archive\${machinename}-info.{#}.zip"
archiveNumbering="Date"
archiveEvery="Month"
archiveDateFormat="yyyyMMdd"
enableArchiveFileCompression ="true"
/>
<target xsi:type="ColoredConsole"
name="ColoredConsole"
layout="${aspnet-User-Identity}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|${message} ${exception}"
header="${longdate}"
footer="${longdate}"
useDefaultRowHighlightingRules="True"
detectConsoleAvailable="True">
</target>
<target xsi:type="LimitingWrapper"
name="MailTarget"
messageLimit="1"
interval="00:15">
<target xsi:type="Mail"
layout="${longdate}|${aspnet-request-ip}||${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${pad:padding=-3:${threadid}}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True}|${message}|${exception:format=tostring}"
subject ="Error in Testmij"
to="info#datec.nl"
from="testmijonline#testmij.nl"
body="${longdate}|${aspnet-request-ip}||${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${pad:padding=-3:${threadid}}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True}|${message}|${exception:format=tostring}"
smtpUserName ="mail#smtp.datec.nl"
enableSsl ="false"
smtpPassword ="7dnV&q#D3s"
smtpAuthentication ="Basic"
smtpServer ="smtp.datec.nl "
smtpPort ="25"
/>
</target>
<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile"/>
<!--Skip Microsoft logs and so log only own logs-->
<logger name="DTOWEB.Models.HttpClientExtensions" writeTo="blackhole" final="true" />
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="System.Net.Http.HttpClient*" minlevel ="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Error" writeTo="ownFile-error" />
<logger name="*" minlevel="Trace" writeTo="ownFile-trace" />
<logger name="*" minlevel="Info" writeTo="ownFile-info" />
<logger name="*" minlevel="Trace" writeTo="ColoredConsole" />
<logger name ="*" minlevel="Error" writeTo ="MailTarget"/>
</rules>
</nlog>
Answered, see comment of Rolf Kristensen
When you run a debug session in localhost, Visual Studio does not deploy all files to the bin directory. So the changed Nlog.config was not deployed.

Connect Azure app service Logs to ELK

I need my azure app service's logs and error logs to be visualized using ELK stack. How to connect these logstash.?. How can I ship my app service logs to logstash.?
use NLog (https://nlog-project.org/)
<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="internal.nlog"
throwExceptions="true"
throwConfigExceptions="true"
internalLogLevel="Debug">
<extensions>
<add assembly="NLog.StructuredLogging.Json" />
</extensions>
<targets>
<target xsi:type="Console" name="console" layout="${structuredlogging.json}" />
<!--<target name="logstash" xsi:type="BufferingWrapper" flushTimeout="5000">-->
<target name="logstash" xsi:type="Network" layout="${structuredlogging.json}" address="elasticsearch:5443" />
<!--</target>-->
</targets>
<rules>
<logger name="*" writeTo="logstash" />
<logger name="*" minlevel="Trace" writeTo="console" />
</rules>
</nlog>

NLog: Does the FormControl target really exist?

According to the documentation, NLog offers a FormControl target that will write log messages into the Text property of a control on a Windows Form. However, when I add a FormControl target to my configuration, I get an exception telling me that no target exists named "FormControl". I did download the NLog.Windows.Forms package and include a reference to the DLL in my project.
Here's the configuration:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true">
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
<target xsi:type="File" name="FileTarget" fileName="${basedir}/NLogger_4_1_2.log"
layout="${date} ${uppercase:${level}} ${message}" />
-->
<target name="AsyncTarget" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
<target xsi:type="File" name="FileTarget1" fileName="${basedir}/NLogger_4_1_2.log"
layout="${date} ${uppercase:${level}} ${message}" />
</target>
<target xsi:type="File" name="ReportTarget" fileName="${basedir}/NLogger_4_1_2_report.log"
layout="${date} ${uppercase:${level}} ${message}" />
<target xsi:type="FormControl"
name="FormControlTarget"
layout="${message}"
append="true"
controlName="TextBox1"
formName="Form1" />
</targets>
<rules>
<logger name="FileLogger" minlevel="Trace"
writeTo="AsyncTarget" />
<logger name="ReportLogger" minlevel="Trace"
writeTo="ReportTarget" />
<logger name="FormLogger" minlevel="Trace"
writeTo="FormControlTarget" />
</rules>
</nlog>
I found this question in 2022 trying to get this working in VS2022. The answer is you need the NLog.Windows.Forms NuGet package installed.
And it is recommended to update NLog.config to include NLog.Windows.Forms-assembly in <extensions>:
<?xml version="1.0" encoding="utf-8" ?>
<nlog>
<extensions>
<add assembly="NLog.Windows.Forms"/>
</extensions>
...
</nlog>

Resources