I'm trying to add logging to a WebAPI in .NET core via NLog, but can't seem to get Linux to let me write log files to var/log/my-app
. I can't find any documentation other than the extension for SysLog but this doesn't fit my needs. The goal is to define a folder named after the app, and store the internal, all, and own files there like so:
var
|-- log
|-- my-app
|-- internal.txt
|-- nlog-all.log
|-- nlog-own.log
I could be missing docs or something else, but I haven't found how to specify more than just writing to the system logs via SysLog. How should one go about doing this with NLog in a Linux environment like Ubuntu?
The NLog InternalLogger is mostly for diagnostics and troubleshooting, so the logic for dynamic control of logging path is rather limited for simplicity and reduced chance of errors.
You can right now use the following keywords:
See also: https://github.com/NLog/NLog/wiki/Internal-Logging
In your case then I would probably consider perform a token-search-replace on deployment of NLog.config
(Make a search for XML-transformations on deployment).
Alternative just assign the application-name at startup like this:
var logDir = "var/log/my-app";
NLog.GlobalDiagnosticsContext.Set("LogDir", logDir);
NLog.Common.InternalLogger.LogFile = System.IO.Path.Combine(logDir, "internal.txt");
With this 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"
throwConfigExceptions="true"
internalLogLevel="info">
<targets>
<target xsi:type="File" name="allfile" fileName="${gdc:LogDir}/nlog-all.log" />
<target xsi:type="File" name="ownfile" fileName="${gdc:LogDir}/nlog-own.log" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole -->
<logger name="*" minlevel="Trace" writeTo="ownfile" />
</rules>
</nlog>