NLog Archive configuration - nlog

In some of my programs I have a 30 day rotation as expected with multiple entries from several months, but only 30 log files. In other programs, I have only one day's worth of log files, but still only 30 log files total. What I want to have is just the log file entries from the last 30 days with 30 log files. I guess I don't know what I am missing.
Yesterday one of my log files was overwritten when the program started back up so I lost the data that would tell me what happened. Then my second question is, does archiving just remove the files that don't fit the pattern or does it actually take the log files and put them somewhere? What really is archiving? Here is my nlog.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">
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<variable name="LogDir" value="${specialfolder:folder=MyDocuments}/MyApp/Log"/>
<variable name="LogDay" value="${date:format=dd}"/>
<targets async="true">
<!-- add your targets here -->
<target name="LogTarget1"
xsi:type="File"
fileName="${LogDay}.log"
encoding="utf-8"
maxArchiveFiles="30"
archiveNumbering="Sequence"
archiveAboveSize="52428800"
archiveFileName="${LogDay}.{#######}.log"
layout="${date:format=MM/dd/yyyy HH\:mm\:ss}|${level:uppercase=true}|${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Trace" writeTo="LogTarget1" />
</rules>
</nlog>

This is a shortcoming of NLog which isn't documented well.
When files should be deleted (e.g. max archives files) then the log files and archive files can't be in the same folder.
So one fix for this config would be to change the archifeFilePath to
archiveFileName="archive/${LogDay}.{#######}.log"
See also this issue on GitHub

Related

Nlog - Can I set an action when nlog is done writing to a file?

My current .net core nlog WebAPI config. I'm using:
<targets async="true">
Asynchronous target wrapper allows the logger code to execute more
quickly, by queueing messages and processing them in a separate
thread.
https://github.com/nlog/NLog/wiki/AsyncWrapper-target#async-attribute-and-asyncwrapper
<targets async="true">
<target name="jsonfile" xsi:type="File" fileName="/MyAPI${date:format=yyyy-MM-dd HH-mm-ss}.json" >
<layout xsi:type="JsonLayout">
<attribute name="Date" layout="${date:format=O}" />
</layout>
</target>
</targets>
This creates files every second:
MyAPI_2022-12-08 23-06-28.json
How how can I let nlog do something (f.ex. change the name or move it to another folder) when nlog is done writing to the file? I have a system (Splunk Universal Forwarder) that picks up the logs and I don't want it to pick up the file while it's being written to.
Thanks! :-)

How to compress all files with IIS

IIS compression has been enabled:
The following is the httpCompression tag of web.config:
<httpCompression
directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"
minFileSizeForComp="1"
staticCompressionIgnoreHitFrequency="true"
dynamicCompressionIgnoreHitFrequency="true">
<dynamicTypes>
<add mimeType="*/*" enabled="true" />
</dynamicTypes>
<staticTypes>
<add mimeType="*/*" enabled="true" />
</staticTypes>
</httpCompression>
I see only CSS and JavaScript files are compressed when running the web app:
Unfortunately, other files are not compressed:
I do not seee "IIS Temporary Compressed Files" in "C:\inetpub\temp".
Could anyone provide a tip on how to diagnose this?
Update[2020-08-13]
Configuration Editor on Windows Server 2016:
Update[2020-08-13]
Per #Kul-Tigin, Dynamic Content Compression needs to be installed:
There's no such a setting named dynamicCompressionIgnoreHitFrequency, remove it.
1 (in bytes) for minFileSizeForComp is a little bit harsh. Compressing small files just decreases the response size.
Leave it 2700 as default.
Unlike setting values on attributes (like you did in staticCompressionIgnoreHitFrequency="true"), adding a setting as node won't override inherited ones.
Before adding, removing possibly inherited corresponding setting or clear all the inherited ones is a good practice to prevent errors (especially silent ones).
Otherwise an error may occur or worse a silent error may break your setting.
100 (in MB) for per application pools space limit may be insufficient for your needs. If I recall correctly most of your files are webassembly files of megabaytes.
Since you want all files to be compressed if possible; specify a value big enough.
Big as if possible; the sum of the uncompressed lengths of all your files. Say 2048 MB.
With this way you can't reach the disk space limit so none of your compressed caches are not deleted due to lack of space.
As long as the original file has not changed, the compressed caches survive and be delivered.
I have 3 years old compressed cache files stored on my servers, ready to be delivered BTW.
So, give the following a try.
If it does not work, please provide more information about request headers and the files that are delivered as uncompressed.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpCompression
sendCacheHeaders="false"
directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"
doDiskSpaceLimiting="true"
maxDiskSpaceUsage="2048"
minFileSizeForComp="2700"
noCompressionForHttp10="true"
noCompressionForProxies="true"
noCompressionForRange="true"
staticCompressionIgnoreHitFrequency="true"
staticCompressionDisableCpuUsage="100"
staticCompressionEnableCpuUsage="50"
dynamicCompressionDisableCpuUsage="90"
dynamicCompressionEnableCpuUsage="50"
dynamicCompressionBufferLimit="65536">
<dynamicTypes>
<clear />
<add mimeType="*/*" enabled="true" />
</dynamicTypes>
<staticTypes>
<clear />
<add mimeType="*/*" enabled="true" />
</staticTypes>
</httpCompression>
</system.webServer>
</configuration>

Logging into multiple tables using NLog

I am in search of method or ways to log data into multiple tables using NLog. I have more than one log tables with different structures in my case. And, I would like to log two different datas in these two tables.
Please let me know if there is a way to configure NLog to handle two different tables while logging.
You need two database targets, one for each table.
e.g.
<targets>
<target name="table1" xsi:type="Database"
... />
<target name="table2" xsi:type="Database"
... />
</targets>
<rules>
<logger name="logger1" minlevel="Info" writeTo="table1" />
<logger name="logger2" minlevel="Info" writeTo="table2" />
</rules>

NLog standard and custom targets give this is an invalid xsi type message

In an NLog.config file I had to add a new rule to log errors to the event log.
There was already a line specifying an error target:
<logger name="*" minlevel="Error" writeTo="AzureTableStorage" />
So I just added eventlog to the writeTo parameter.
<logger name="*" minlevel="Error" writeTo="AzureTableStorage,eventlog" />
And here is eventlog
<target xsi:type="EventLog"
name="eventlog"
source="MyApp"
layout="${message}${newline}${exception:format=ToString}"/>
However some of the other targets (AzureTableStorage, trace, loggly) all have a blue underline on them and the tooltip says
This is an invalid xsi:type http://www.nlog-project.org/schemas/NLog.trace
In another project the same targets do not have this error.
What do I need to do to remove this error?
This is a error from the XSD, which should be seen as warning. The XSD is generated with all the possible targets (in the NLog main package) and thus doesn't have the custom targets.
These kind of errors could be ignored and NLog won't stop working if the XML config contains these kind of "errors".

How do I enable logging in RhinoETL process?

I have nearly completed my first ETL process that uses Rhino ETL, and I've been able to figure out the way to use the API by referring to the tests.
Great. I have data moving through the pipeline and being written to the db.
However I can't seem to figure out how to enable logging.
the log4net assemblies are there, the log4net object is being created
the WithLoggingMixin class seems to be doing its thing (although I must admit I'm a bit fuzzy on exactly what that is)
in the log4net.config file I have a follingFileAppender set up, and it contains the following:
But no log file is created. When I make a call to Debug() in my code it doesn't do anything because log.IsDebugEnabled is false.
What am I missing?
In Rhino Etl 1.2.3, I was able to get logging to the console by adding the following to items the configuration section of the program's app.config file:
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
<arg key="level" value="DEBUG" />
<arg key="showLogName" value="true" />
<arg key="showDataTime" value="true" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
To log to destinations other than the console, the Common.Logging documentation has information on how to wire up log4net.
Okay. I dug through the [log4net documentation][1] and figured out a way to do it.
First of all I moved the log4net config into the App.config file (in a log4net section) and then executed
log4net.Config.XmlConfigurator.Configure();
during initialization. Now it works.
[1]: http://logging.apache.org/log4net/release/manual/configuration.html#.config Files "Apache log4net documentation"

Resources