Write custom logs to a file on Orchestrator server

How to write logs to a file and customize/filter information which will be written

Bellow are some steps that can be applied to write logs to a file, choose specific logs layout  and how to filter logs before writing:
 
1. Write logs to a file

To write robot logs into a file on Orchestrator server a new target needs to be added which will create a file on daily basis with the short date as filename: 


<target xsi:type=“File” name=“executionFile” filename=“C:\Logs${shortdate}.txt” layout="{time} {level} {message}" concurrentWrites="true" /&gt;</b><br><br><img alt="" src="https://uipath-survey.secure.force.com/CaseView/servlet/rtaImage?eid=ka01Q0000019RtM&amp;feoid=00N1Q00000Tffpw&amp;refid=0EM1Q0000024eR6"><br><br>Add&nbsp; a new&nbsp; Rule to send the logs to file:<br style="box-sizing: border-box;color: rgb(62, 62, 60);font-family: &quot;Salesforce Sans&quot;, Arial, sans-serif;font-size: 13px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration-style: initial;text-decoration-color: initial;"> <br style="box-sizing: border-box;color: rgb(62, 62, 60);font-family: &quot;Salesforce Sans&quot;, Arial, sans-serif;font-size: 13px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration-style: initial;text-decoration-color: initial;"> <b style="box-sizing: border-box;font-weight: var(--lwc-fontWeightBold,700);color: rgb(62, 62, 60);font-family: &quot;Salesforce Sans&quot;, Arial, sans-serif;font-size: 13px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration-style: initial;text-decoration-color: initial;">&lt;logger name="Quartz.*" minlevel="Info" writeTo="executionFile" final="true" /&gt;</b><br><br><img alt="" src="https://uipath-survey.secure.force.com/CaseView/servlet/rtaImage?eid=ka01Q0000019RtM&amp;feoid=00N1Q00000Tffpw&amp;refid=0EM1Q0000024eRa"><br><br><b>2. Adding more information to custom logs&nbsp;</b><br><br><span style="color: rgb(62, 62, 60);font-family: &quot;Salesforce Sans&quot;, Arial, sans-serif;font-size: 13px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration-style: initial;text-decoration-color: initial;display: inline !important;float: none;" uipath_custom_id="16">In case additional logs layout are needed in a custom file than the default/usual ones in the web.config (layout="{time} {level} {message}") more information can be added from the existing parameters  that are written to database in the web.config file: 

Image_2020-03-31_15-36-47.png



For example,  to have additionally the machine name, the process name and the robot name, in the target definition for the File add in the layout the following parameters  {machinename} {event-properties:item=processName} {event-properties:item=robotName}</b>, it will look as bellow:&nbsp;<br style="box-sizing: border-box;color: rgb(62, 62, 60);font-family: &quot;Salesforce Sans&quot;, Arial, sans-serif;font-size: 13px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration-style: initial;text-decoration-color: initial;"> <br style="box-sizing: border-box;color: rgb(62, 62, 60);font-family: &quot;Salesforce Sans&quot;, Arial, sans-serif;font-size: 13px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration-style: initial;text-decoration-color: initial;"> <span style="color: rgb(62, 62, 60);font-family: &quot;Salesforce Sans&quot;, Arial, sans-serif;font-size: 13px;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;white-space: normal;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;background-color: rgb(255, 255, 255);text-decoration-style: initial;text-decoration-color: initial;display: inline !important;float: none;" uipath_custom_id="17">&lt;target xsi:type="File" name="executionFile" filename="D:\UiPath\Logs\text1.txt" layout="{time} {level} {message} {machinename} {event-properties:item=processName} ${event-properties:item=robotName}" concurrentWrites=“true” archiveAboveSize=“104857600” maxArchiveFiles=“10”  />



Image_2020-03-31_15-41-44.png

3. Filtering the logs before writing them to file/Elasticsearch/other targets 

There are some filters which can be applied  to ignore logging specific messages/logs. Some examples :

 

<rules>
    <logger name="*" writeTo="file">
        <filters>
            <when condition="length('${message}') > 100" action="Ignore" />
            <when condition="equals('${logger}','MyApps.SomeClass')" action="Ignore" />
            <when condition="(level >= LogLevel.Debug and contains('${message}','PleaseDontLogThis')) or level==LogLevel.Warn" action="Ignore" />
            <when condition="not starts-with('${message}','PleaseLogThis')" action="Ignore" />
        </filters>
    </logger>
</rules>


All the filters can be found on the official Nlog Github repository 

Bellow is an example of filter that will not log the messages bigger than 100 characters or logs that contain "Foo" string to the database target (if sensitive data is required to be filtered before sending)

  <logger name="Robot.*" writeTo="database" >
              <filters defaultAction='Log'>
            <when condition="length('${message}') > 100" action="Ignore" />
            <whenContains layout="${message}" substring="Foo" action="Ignore" />
        </filters>
        </logger>
Image_2020-04-01_17-55-39.png